【发布时间】:2021-04-21 18:35:48
【问题描述】:
所以我试图做一些从字符串中减去一定数量的东西,以使 2 个字符串的长度相等,问题是当我尝试找到存储为 0000 0000 的元素 1 的长度时,它返回 1。此外,代码是从文件中获取的,但唯一相关的元素是 1。这是有问题的代码:
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
【问题讨论】:
标签: python string dictionary comparison string-comparison