Python 为什么dictionary元素的长度为1,而该位置的字符串应返回9?
所以我试图从一个字符串中减去一定量,使两个字符串的长度相等。问题是当我试图找到元素1的长度时,它存储为0000 0000,返回1。此外,代码是从文件中获取的,但唯一相关的元素是1。以下是相关代码:Python 为什么dictionary元素的长度为1,而该位置的字符串应返回9?,python,string,dictionary,comparison,string-comparison,Python,String,Dictionary,Comparison,String Comparison,所以我试图从一个字符串中减去一定量,使两个字符串的长度相等。问题是当我试图找到元素1的长度时,它存储为0000 0000,返回1。此外,代码是从文件中获取的,但唯一相关的元素是1。以下是相关代码: import re insSpec2 = {} z = 0 bool(z) TestString = "0100 0001" while z == 0: f = open("Specifier", 'r') for word in f:
import re
insSpec2 = {}
z = 0
bool(z)
TestString = "0100 0001"
while z == 0:
f = open("Specifier", 'r')
for word in f:
i = 0
insSpec2[i] = re.split('r|a', word)
print(insSpec2[i])
insLength = len(insSpec2[i])
testLength = len(TestString)
if testLength > insLength:
diff = testLength-insLength
else:
diff = insLength-testLength
print(insLength)#returns 1
print(testLength, insLength) #returns 9 1
print(diff)#returns 8
print(insSpec2[0].__len__()) #returns 1
if insLength < testLength:
changeVar = 8 - insLength
while TestString != insSpec2:
#print(testLength)
newStr = TestString[:-diff]
#print(newStr)
elif insSpec2[i] == TestString:
print("Found Match", insSpec2[i])
z = 1
break
i += 1
重新导入
insSpec2={}
z=0
布尔(z)
TestString=“0100 0001”
当z==0时:
f=开放(“说明符”,“r”)
对于f中的单词:
i=0
insSpec2[i]=重新拆分('r | a',单词)
打印(insSpec2[i])
insLength=len(insSpec2[i])
testLength=len(TestString)
如果testLength>insLength:
diff=测试长度insLength
其他:
diff=insLength测试长度
打印(insLength)#返回1
打印(testLength,insLength)#返回9 1
打印(差异)#返回8
打印(insSpec2[0]。_len__())#返回1
如果insLength这是因为re.split
返回一个列表而不是字符串,insSpec[i]=['0000']。它的长度为1,因此如何获取字符串长度?insSpec[I]包含regexp split的结果。因此,如果字符串实际上尚未拆分,则字符串长度为len(insSpec[i][0])
。如果有,则该表达式返回第一个子字符串的长度。