从具有特定条件的列表列表创建字典
Create a dictionary from a list of lists with certain criteria
我有一个列表列表如下:
l1=[['a1','a2','a3'],['b1','b2'],['c1','a1']]
我想要一个字典列表如下:
[{"start":"a1","end":"ä2"},
{"start":"a2","end":"a3"},
{"start":"a3","end":"a1"},
{"start":"b1","end":"b2"},
{"start":"c1","end":"a1"}
]
我尝试了下面的代码并得到了索引越界异常:
for val in list_listedges:
for i in range(0,len(val)):
dict_edges["start"]=val[i]
dict_edges["end"]=val[i+1]
我正在寻找一个有效的解决方案或上述代码的增强,它会产生相同的结果 result.Also,3 不是固定的 number.It 可能是 4 或 5 also.In案例,我需要所有的元素相互配对
两个问题:
val[i+1]
不会循环到开头,使用val[(i+1) % len(val)]
- 您每次都覆盖同一个字典,而不是附加到字典列表中。
result = []
for val in list_edges:
for i, start in enumerate(val, 1):
result.append({"start": start, "end": val[i % len(val)]})
我没有在范围内循环,而是使用 enumerate()
获取索引和列表元素,并指定起始索引 1
以自动加 1。
这不会产生与您显示的完全相同的结果。它还会将 {"start": "b2", "end": "b1"}
和 {"start": "a1", "end": "c1"}
放入结果列表中,因为它将所有列表都包装回开头。如果应该区别对待 2 元素列表,您可以在代码中为此添加一个特殊检查。
你确定这不是 IndentationError? for 循环在 list_listedges
之后缩进,这不是正确的语法,无论如何,我会这样做:
# just a function for the for loop in the next function, ignore it, though it is fine if you copy it :)
def weave(val1, val2): # unneeded for use but u can use this in other scripts, too
if len(val1) != len(val2):
import sys
sys.stderr.write("length of value 1 and value 2 are not the same, must be the same\n\n[your OS]: FIX IT OR ELSE SATAN WILL INVITE YOU TO HELL PERSONALLY!!!\n\n(BTW your gf is cheating on you, she is going out with me)\n\n[REPL]: ")
sys.exit(256)
w = [] # the returned value
for i, x in enumerate(val2):
w += [(val1[i], x)]
return w
# pay attention to THIS:
def listToDict(LIST, *indexers):
DICT = {}
for i, a in weave(LIST, indexers):
DICT = {**DICT, a: i}
return DICT
print(weave([1, 3, 7, 5], ["1", "3", "7", "5"]))
print(listToDict([1, 3, 7, 5], "1", "3", "7", "5"))
您可以完全避免索引,方法是使用 itertools.combinations()
函数生成每个子列表中点的所有可能配对,如下所示:
from itertools import combinations
l1 = [['a1','a2','a3'], ['b1','b2'], ['c1','a1']]
dicts = [{'start': start, 'end': end}
for points in l1
for start, end in combinations(points, 2)]
from pprint import pprint
pprint(dicts, sort_dicts=False)
输出:
[{'start': 'a1', 'end': 'a2'},
{'start': 'a1', 'end': 'a3'},
{'start': 'a2', 'end': 'a3'},
{'start': 'b1', 'end': 'b2'},
{'start': 'c1', 'end': 'a1'}]
我有一个列表列表如下:
l1=[['a1','a2','a3'],['b1','b2'],['c1','a1']]
我想要一个字典列表如下:
[{"start":"a1","end":"ä2"},
{"start":"a2","end":"a3"},
{"start":"a3","end":"a1"},
{"start":"b1","end":"b2"},
{"start":"c1","end":"a1"}
]
我尝试了下面的代码并得到了索引越界异常:
for val in list_listedges:
for i in range(0,len(val)):
dict_edges["start"]=val[i]
dict_edges["end"]=val[i+1]
我正在寻找一个有效的解决方案或上述代码的增强,它会产生相同的结果 result.Also,3 不是固定的 number.It 可能是 4 或 5 also.In案例,我需要所有的元素相互配对
两个问题:
val[i+1]
不会循环到开头,使用val[(i+1) % len(val)]
- 您每次都覆盖同一个字典,而不是附加到字典列表中。
result = []
for val in list_edges:
for i, start in enumerate(val, 1):
result.append({"start": start, "end": val[i % len(val)]})
我没有在范围内循环,而是使用 enumerate()
获取索引和列表元素,并指定起始索引 1
以自动加 1。
这不会产生与您显示的完全相同的结果。它还会将 {"start": "b2", "end": "b1"}
和 {"start": "a1", "end": "c1"}
放入结果列表中,因为它将所有列表都包装回开头。如果应该区别对待 2 元素列表,您可以在代码中为此添加一个特殊检查。
你确定这不是 IndentationError? for 循环在 list_listedges
之后缩进,这不是正确的语法,无论如何,我会这样做:
# just a function for the for loop in the next function, ignore it, though it is fine if you copy it :)
def weave(val1, val2): # unneeded for use but u can use this in other scripts, too
if len(val1) != len(val2):
import sys
sys.stderr.write("length of value 1 and value 2 are not the same, must be the same\n\n[your OS]: FIX IT OR ELSE SATAN WILL INVITE YOU TO HELL PERSONALLY!!!\n\n(BTW your gf is cheating on you, she is going out with me)\n\n[REPL]: ")
sys.exit(256)
w = [] # the returned value
for i, x in enumerate(val2):
w += [(val1[i], x)]
return w
# pay attention to THIS:
def listToDict(LIST, *indexers):
DICT = {}
for i, a in weave(LIST, indexers):
DICT = {**DICT, a: i}
return DICT
print(weave([1, 3, 7, 5], ["1", "3", "7", "5"]))
print(listToDict([1, 3, 7, 5], "1", "3", "7", "5"))
您可以完全避免索引,方法是使用 itertools.combinations()
函数生成每个子列表中点的所有可能配对,如下所示:
from itertools import combinations
l1 = [['a1','a2','a3'], ['b1','b2'], ['c1','a1']]
dicts = [{'start': start, 'end': end}
for points in l1
for start, end in combinations(points, 2)]
from pprint import pprint
pprint(dicts, sort_dicts=False)
输出:
[{'start': 'a1', 'end': 'a2'},
{'start': 'a1', 'end': 'a3'},
{'start': 'a2', 'end': 'a3'},
{'start': 'b1', 'end': 'b2'},
{'start': 'c1', 'end': 'a1'}]