【问题标题】:Getting different errors while trying to solve converting strings to integers in a list尝试解决将字符串转换为列表中的整数时出现不同的错误
【发布时间】:2020-10-22 17:12:39
【问题描述】:

我有以下列表和字符串:

befcodes = ["A1","A2","A3","A4","A5","A6","A7","A8","A9","10","11","12","13","14","15","16","17","18","19","20"]
telegram = "$00;02;A1;00000000*49"

我的代码现在将字符串中的 A1 更改 20 次,如下所示:

allbefcodes = []
for i in befcodes:
    dif_tele = telegram.replace("A1", i)
    allbefcodes.append(dif_tele)
print (allbefcodes) 

输出以下列表:

['$00;02;A1;00000000*49', '$00;02;A2;00000000*49', '$00;02;A3;00000000*49', '$00;02;A4;00000000*49', '$00;02;A5;00000000*49', '$00;02;A6;00000000*49', '$00;02;A7;00000000*49', '$00;02;A8;00000000*49', '$00;02;A9;00000000*49', '$00;02;10;00000000*49', '$00;02;11;00000000*49', '$00;02;12;00000000*49', '$00;02;13;00000000*49', '$00;02;14;00000000*49', '$00;02;15;00000000*49', '$00;02;16;00000000*49', '$00;02;17;00000000*49', '$00;02;18;00000000*49', '$00;02;19;00000000*49', '$00;02;20;00000000*49']

现在我想使用 XOR 运算符来获得每个电报的二进制校验和(=allbefcodes 的每个字符),我这样做了:

result = functools.reduce(operator.xor,(ord(n) for n in (allbefcodes[0])[1:18]))  
print (f'{result:08b}')

这对allbefcodes[0] 确实有效,我得到了输出01001001。但我现在想通过循环对allbefcodes 的所有字符执行此操作,但我会遇到不同的错误。这是我迄今为止尝试过的:

for x in allbefcodes:
    result = functools.reduce(operator.xor,(ord(n) for n in (allbefcodes[x])[1:18]))  
    print (f'{result:08b}')

出现错误TypeError: list indices must be integers or slices, not str。我试图这样解决它:

for x in allbefcodes:
    allbefcodes = (int(a) for a in allbefcodes)
    result = functools.reduce(operator.xor,(ord(n) for n in (allbefcodes[x])[1:18]))  # "
    print (f'{result:08b}')

但是这里出现了错误TypeError: 'generator' object is not subscriptable。所以我接下来尝试了这个:

allbefcodes = []
for i in befcodes:
    dif_tele = telegram.replace("A1", i)
    allbefcodes.append(dif_tele)
allbefcodes = (int(a) for a in allbefcodes)     #Tried to solve it with this line
print (allbefcodes)    

for x in allbefcodes:
    result = functools.reduce(operator.xor,(ord(n) for n in (allbefcodes[x])[1:18]))  # "(allbefcodes[0])" statt telegram
    print (f'{result:08b}')

但是现在出现了错误ValueError: invalid literal for int() with base 10: '$00;02;A1;00000000*49'。做(float(int(a)) for a in allbefcodes) 不会改变这一点。 我的最后一次尝试是将(allbefcodes[x]) 更改为(allbefcodes[0:]) 并将allbefcodes = (int(a) for a in allbefcodes) 排除在外,如下所示:

allbefcodes = []
for i in befcodes:
    dif_tele = telegram.replace("A1", i)
    allbefcodes.append(dif_tele)
print (allbefcodes)    

for x in allbefcodes:
    result = functools.reduce(operator.xor,(ord(n) for n in (allbefcodes[0:])[1:18]))  # "(allbefcodes[0])" statt telegram
    print (f'{result:08b}')

但是现在我得到了TypeError: ord() expected a character, but string of length 21 found,这让我再次回到需要整数而不是字符串的问题上,我已经尝试过解决这个问题......我真的不知道该怎么做了,真的很感激一些帮助!

【问题讨论】:

  • 您发现 TypeError: 'generator' object is not subscriptable 的错误是因为您使用了由括号分隔的理解,它创建了一个生成器。最后一行 TypeError: ord() expected a character, but string of length 21 found 已经发生,因为您正在迭代一个列表,其中每个条目都是 allbefcodes 中的一个元素,这是一个 string of length 21

标签: python list error-handling xor


【解决方案1】:

我想通了! 我只需要像这样用x 替换(allbefcodes[0:])

allbefcodes = []
for i in befehlscodes:
    dif_tele = telegram.replace("A1", i)
    allbefcodes.append(dif_tele)
print (allbefcodes)
   
for x in allbefcodes:
    result = functools.reduce(operator.xor,(ord(n) for n in x[1:18])) 

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 2020-11-25
    • 2022-09-22
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多