[英]Using elements from a list with many levels to create a dictionary
我有以下代码:
population = [[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [0]],
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [3], [3]]]
def CreateDictionary(population):
d=dict()
for ind in range (0, len(population)):
g = ','.join(str(ind[0][1]) for ind in population)
f = ','.join(str(ind[1][1]) for ind in population)
d[g] = f
return (d)
我得到的结果是:
{'[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1],
[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0]': '[1],[3],[4],[3]'}
我想做的是使用列表中每个部分的第一个和第二个元素创建字典(第三个元素可以忽略),并将一个分配给另一个,例如:
d={'0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1': 1,
'0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1': 3} and so on...
第一个元素(二进制序列)可以是文本,没关系,但是第二个元素必须采用我可以用来计算其他内容的格式。 由于我试图以此列表形式进行数学运算,因此它也不起作用。
我以其他方式尝试了许多事情,但是在尝试运行代码时遇到了一些错误,这是我可以使其运行的唯一方法,但它甚至与我想要的都不相近。 关于如何处理这个问题有任何想法吗?
谢谢!
如果您要使用它,请使用dictionary comprehension
。 要将列表转换为字符串,请在将每个元素映射到字符串后使用join()
函数。
population = [[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [0]],
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [3], [3]]]
d = {", ".join(map(str,i[0])):i[1][0] for i in population}
print(d)
输出:
{'0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0': 4, '1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0': 3, '0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1': 3, '0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1': 1}
替代解决方案:我们可以将列表转换为以10为底的数字(从二进制数开始):
def return_decimal(lst):
return int(''.join(map(str,lst)),2)
population = [[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [0]],
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [3], [3]]]
d = dict((return_decimal(i[0]),i[1][0]) for i in population)
返回值:
{40152: 3, 11745: 1, 27876: 4, 14769: 3}
查找值:
find = [0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1] # 11745
d.get(return_decimal(find))
返回值:
1
我真的看不到像这样创建的dict
的用途(索引变得很痛苦)。 我真的建议仅保留x
和y
单独列表(请参阅我的答案)。 但是你可以创建一个dict
通过传递的元组x
和y
到dict
构造函数:
x, y, *_ = zip(*population)
x = [', '.join(map(str, z)) for z in x]
y = [z[0] for z in y]
d = dict(zip(x, y))
d
{'0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1': 1,
'0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1': 3,
'0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0': 4,
'1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0': 3}
您可以尝试dict理解:
population_dict = {" ".join(str(x) for x in item[0]): int(item[1][0]) for item in population}
此dict理解与:
population_dict={}
for item in population:
population_dict[" ".join(str(x) for x in item[0])] = int(item[1][0])
print(population_dict)
输出:
{'0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 1': 3, '0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1': 1, '1 0 0 1 1 1 0 0 1 1 0 1 1 0 0 0': 3, '0 1 1 0 1 1 0 0 1 1 1 0 0 1 0 0': 4}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.