简体   繁体   中英

nested comprehension list: variable doesn't exist even though inside loop

input = [str(bin(ord(char)))[i] for i in range(len(str(bin(ord(char)))), 1, -1) for char in input]

This is my code, the purpose is to format an input string into an array in the format of bits in little endian order.

As far as I understand it the most right for statement should be the most outer loop, but the 2nd char used in range creation is not defined at that point.

Is my assumption just wrong about an interpretation as an outer loop or am I doing something wrong?

I want to loop through all the characters in input and then loop through all the bits backwards per character.

It looks like you are trying to reverse a bit-string,

this snippet reverses the bit string

["".join(reversed(bin(ord(char))[2:])) for char in input]

keep in mind that the first 2 characters of a binary string are '0b' , and they are not a part of the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM