[英](Python) Turning items in a list into lists
I want to store items in my list as list themselves (ie each binary bit will be an index in the new list) but I can't seem to make that happen: 我想将列表中的项目本身存储为列表(即每个二进制位将是新列表中的索引),但我似乎无法实现:
encoded = []
for value in redChannelData:
encoded1 = bin(value)[2:]
encoded.append(encoded1)
redchannelbinarylist = [[] for binary in encoded]
print(redchannelbinarylist)
print(encoded)
Output 输出量
['101110', '110001', '110010', '110011', '110101', '110101', '110110', '111000', '111011', '111011', '111100', '111101', '111110', '111110', '1000000', '1000000', '1000001']
I want something like this: 我想要这样的东西:
[[1, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1], ...]
Try doing this: 尝试这样做:
[list(map(int, x)) for x in a]
Here is what happens (from inside out): 这是发生的(从内到外):
list(map(int, x))
converts a list of '0' and '1' into a list of their integer equivalents. list(map(int, x))
将'0'和'1'的列表转换为等效的整数列表。 The list
here is to get a list instead of map
result. 此处的list
用于获取列表而不是map
结果。 try doing this: 尝试这样做:
a = ['101110', '110001', '110010', '110011', '110101', '110101', '110110',...]
result = []
for i in range(len(a)):
ls = []
for k in range(len(a[i])):
ls.append(a[i][k])
result.append(ls)
print(result)
this method is a bit longer, but the logic is easy to understand. 这种方法要长一些,但是逻辑很容易理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.