使用元组操作python的字典和列表

使用元组操作python的字典和列表,python,list,dictionary,Python,List,Dictionary,我有一个列表,其中包含一些元组数据: [('a',12),('b',6),('c',9),('d',15),('e',4)] 在使用字典对数据进行一些操作之后 {1:['b','d',2:['a','c','e']}已创建。 我如何操作这个字典和初始列表来检索包含这些字母整数的列表? 像[[6,15],[12,9,4]] items = [('a',12), ('b',6), ('c',9), ('d',15), ('e',4)] bins = {1: ['b','d'], 2: ['a','c

我有一个列表,其中包含一些元组数据:
[('a',12),('b',6),('c',9),('d',15),('e',4)]

在使用字典对数据进行一些操作之后
{1:['b','d',2:['a','c','e']}
已创建。 我如何操作这个字典和初始列表来检索包含这些字母整数的列表? 像
[[6,15],[12,9,4]]

items = [('a',12), ('b',6), ('c',9), ('d',15), ('e',4)]
bins = {1: ['b','d'], 2: ['a','c','e']}
tempList = []
tList = []
for b in bins.keys():
    for i in range(len(bins[b])):
        if bins[b][i] == items[i][0]:
            tList.append(items[i][1])
    tempList.append(tList)
输出为:

[[12],[12]]

多谢各位

您可以从
元组的
列表中创建一个
dict
,并执行如下操作:

>>> a = [('a',12), ('b',6), ('c',9), ('d',15), ('e',4)]
>>> b = {1: ['b','d'], 2: ['a','c','e']}
>>> d = {k:v for k,v in a}
>>> [[d.get(y) for y in x] for x in b.values()]
[[6, 15], [12, 9, 4]]
为了更好地理解:

>>> d = {k:v for k,v in a} # create a dictionary with `char: value` from list of tuples for convenience :)
>>> l = [] # create a main list
>>> for values in b.values(): # we only need the values
...   il = [] # a temporary inner list to keep values
...   for value in values:
...     val = d[value] # get the corresponding value of the character we kept in the dictionary
...     il.append(val) # append the value to the inner list
...   l.append(il) # append the inner list to main list
... 
>>> l
[[6, 15], [12, 9, 4]]

到目前为止你试过什么?
test_dict=dict([a',12),[b',6],[c',9],[d',15],[e',4])
<代码>{key:[test_dict[x]for x in lst]for key,lst in{1:['b','d',2:['a','c','e']}.items()
@BrianJoseph您的答案是好的,但我的目标是输出这个结果:[[6,15],[12,9,4]],而您的答案是{1:[6,15],2:[12,9,4]}这很好!但我不明白它是如何以这种方式100%工作的?非常感谢!它就像一个符咒。当然,非常感谢您的解释!没问题。很高兴我能帮忙