从字典中的列表中获取值并将其保存在列表列表中

时间:2018-06-07 12:04:47

标签: python dictionary

我有一个字典A,其中包含day作为键,以及字符串列表作为值示例:

A = {"day1": ['A', 'B', 'C']}.

如何创建包含将建立列表的唯一关系的列表B?

B = [['A','B'], ['A','C'], ['B','C']]

2 个答案:

答案 0 :(得分:3)

>>> import itertools
>>> A = {"day1": ['A', 'B', 'C']}
>>> seq = A["day1"]
>>> list(itertools.combinations(seq, r=2))
[('A', 'B'), ('A', 'C'), ('B', 'C')]

答案 1 :(得分:0)

A = {"day1": ['A', 'B', 'C']}
B = [['A','B'], ['A','C'], ['B','C']]

vals = A['day1']
connections = []
for val in vals:
    connections_to_val = [(val, x) for x in vals 
        if (val != x and (x, val) not in connections)]
    connections.extend(connections_to_val)