根据集合和另一个列表创建一个新列表

时间:2019-02-18 12:12:46

标签: python-3.x

a=[inc123,inc353,inc345] 

以下三组:

{apple,inc123} {beery,inc345} {cheery,inc353} 

B预期以a的顺序输出,并在各个集合中具有匹配的值

b=[apple,cheery,beery]

1 个答案:

答案 0 :(得分:0)

这有效。

a=['inc123','inc353','inc345'] 
c= [{'apple','inc123'}, {'beery','inc345'}, {'cheery','inc353'}]
b = []
for ae in a:
    for ce in c:
        # Have to convert set to list as set would enumerate in random fashion
        # e.g.read in this sequence 'inc353' and 'cheery' if ce inside enumerate
        # instead of l
        l = list(ce) 
        f = None
        for i,e in enumerate(l):
            if i == 0:
                f = e
            else:
                if e == ae:
                    b.append(f)
print(b)

有人可能会通过过滤和映射发布带有lambda的更优雅的方法。

看看here并玩一下。