微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试解决将字符串转换为列表中的整数时遇到不同的错误

如何解决尝试解决将字符串转换为列表中的整数时遇到不同的错误

我有以下列表和字符串:

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,这使我重新需要整数而不是字符串,我已经尝试解决了...我真的不知道该怎么办了,非常感谢您的帮助!

解决方法

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

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])) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。