我有一个嵌套列表,如:
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
我有一个参考嵌套列表,如:
list1 = [(A,0.75),(D,0.49),(Y,0.36)]
我希望输出为:
mainlist = [(A,10),(B,20),(C,30),(D,40),(E,50).........,(Y,250),(Z,260)]
答案 0 :(得分:2)
你在寻找这样的东西:
list1 = [('A',0.75),('D',0.49),('Y',0.36)]
mainlist = [('A',10),('B',20),('C',30),('D',40),('E',50),('Y',250),('Z',260)]
keys = {k[0] for k in list1} # create a set with keys from list1
newlist = [k for k in mainlist if k[0] in keys] # get items from mainlist with good keys
print(newlist)
[('A', 10), ('D', 40), ('Y', 250)]