Python 如何将嵌套在元组中的列表的值组合成索引?

Python 如何将嵌套在元组中的列表的值组合成索引?,python,list,indexing,tuples,Python,List,Indexing,Tuples,我很难实现我需要实现的目标,所以我想知道这里是否有人可以帮助我:-) 我已经看到了示例11.4。(列出会员名单)在某些方面与我的目标非常接近 该项目是: 从引用的元组列表开始(键,[值列表]) 我想扫描“我的列表”,以便附加嵌套列表,只为一个键组合值列表,如下所示: my_list = [('a',[0, 3]), ('b',[1]), ('c',[2])] 我成功地手动组合了值,但我想自动化它,我找不到如何做到这一点^^ 现在,我得到的是: # my_input == 'a b c a

我很难实现我需要实现的目标,所以我想知道这里是否有人可以帮助我:-)

我已经看到了示例11.4。(列出会员名单)在某些方面与我的目标非常接近

该项目是:
  • 从引用的元组列表开始(键,[值列表])

  • 我想扫描“我的列表”,以便附加嵌套列表,只为一个键组合值列表,如下所示:

    my_list = [('a',[0, 3]), ('b',[1]), ('c',[2])]
    
  • 我成功地手动组合了值,但我想自动化它,我找不到如何做到这一点^^

  • 现在,我得到的是:

    # my_input == 'a b c a'
    
    #splitting input to list
    >>> raw_list = my_input.split()
    >>> raw_list
    ['a', 'b', 'c', 'a']
    
    #getting an enumeration for each entry
    #### (in order of appearance, this is important!) ####
    >>> enum_list = [(b,[a]) for a, b in enumerate(raw_list)]
    >>> enum_list
    [('a', [0]), ('b', [1]), ('c', [2]), ('a', [3])]
    
    #trying to append the enum value of the second 'a' to the first tuple of 'a'
    >>> for (x, y) in enum_list :
    ...     for (x, z) in enum_list :
    ...             enum_list[enum_list.index((x, z))][1].append(y)
    ... 
    >>> enum_list
    [('a', [0, [...], [1, [...], [...], [2, [...], [...], [...], [3, [...], [...], [...], [...]]], [3, [...], [...], [2, [...], [...], [...], [...]], [...]]], [2, [...], [1, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [3, [...], [1, [...], [...], [...], [...]], [...], [...]]], [3, [...], [1, [...], [...], [2, [...], [...], [...], [...]], [...]], [2, [...], [1, [...], [...], [...], [...]], [...], [...]], [...]]]), ('b', [1, [0, [...], [...], [2, [...], [...], [...], [3, [...], [...], [...], [...]]], [3, [...], [...], [2, [...], [...], [...], [...]], [...]]], [...], [2, [0, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [...], [3, [0, [...], [...], [...], [...]], [...], [...], [...]]], [3, [0, [...], [...], [2, [...], [...], [...], [...]], [...]], [...], [2, [0, [...], [...], [...], [...]], [...], [...], [...]], [...]]]), ('c', [2, [0, [...], [1, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [3, [...], [1, [...], [...], [...], [...]], [...], [...]]], [1, [0, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [...], [3, [0, [...], [...], [...], [...]], [...], [...], [...]]], [...], [3, [0, [...], [1, [...], [...], [...], [...]], [...], [...]], [1, [0, [...], [...], [...], [...]], [...], [...], [...]], [...], [...]]]), ('a', [3, [0, [...], [1, [...], [...], [2, [...], [...], [...], [...]], [...]], [2, [...], [1, [...], [...], [...], [...]], [...], [...]], [...]], [1, [0, [...], [...], [2, [...], [...], [...], [...]], [...]], [...], [2, [0, [...], [...], [...], [...]], [...], [...], [...]], [...]], [2, [0, [...], [1, [...], [...], [...], [...]], [...], [...]], [1, [0, [...], [...], [...], [...]], [...], [...], [...]], [...], [...]], [...]])]
    
  • 很抱歉线路太长了,但我认为这会更符合整个错误

如果我不够清楚,请毫不犹豫地告诉我,我会提供更多细节


感谢您的时间和解释:-)

使用
OrderedDict
首先导入集合
)这似乎非常简单:

现在,如果要恢复元组,只需在字典上迭代:

In [441]: [(k, v) for k, v in dict_.items()]
Out[441]: [('a', [0, 3]), ('b', [1]), ('c', [2])]

您可以使用字典来构造列表

my_list = [('a',[0]), ('b',[1]), ('c',[2]), ('a',[3])]
my_dict = {}

for item in my_list:
    if item[0] in my_dict.keys():
        ### if key exists append to list
        my_dict[item[0]].extend(item[1][0:])
    else:
        ### if key does not exist create new key-value pair
        my_dict[item[0]] = [item[1][0]]

my_list = my_dict.items()
print my_list
您可以使用容器创建列表字典,然后只存储每个元组对的值。请注意,我使用
('d',[4,5])
项扩展了原始列表,以说明此方法也适用于任意长度的列表

from collections import defaultdict

my_list = [('a', [0]), ('b', [1]), ('c', [2]), ('a', [3]), ('d', [4, 5])]

dd = defaultdict(list)
for pair in my_list:
    k, v = pair
    dd[k].extend(v)  
>>> dd.items()
[('a', [0, 3]), ('c', [2]), ('b', [1]), ('d', [4, 5])]

是的,使用字典是解决这个问题的最好方法。这里的顺序受到影响。事实上,在面对
setdefault
.Hmm时,defaultdict是过分的,不是很明确,但使用列表和元组则表明情况并非如此。。。无论如何,这是在OP上。看起来不错,但出于某种原因,它对我不起作用:python在第dd[k]行之后引发以下错误。extend(v[0])>>>>>:Traceback(最近一次调用):File“”,第3行,在TypeError中:“int”对象不是iterable@EtienneS它应该是
dd[k].extend(v)
而不是
dd[k].extend(v[0])
。请注意,
dd[k].append(v[0])
在每个元组对的列表中只有一个数字的情况下有效。如果列表中有多个元素,则会丢失数据。我建议改为
extend
my_list = [('a',[0]), ('b',[1]), ('c',[2]), ('a',[3])]
my_dict = {}

for item in my_list:
    if item[0] in my_dict.keys():
        ### if key exists append to list
        my_dict[item[0]].extend(item[1][0:])
    else:
        ### if key does not exist create new key-value pair
        my_dict[item[0]] = [item[1][0]]

my_list = my_dict.items()
print my_list
from collections import defaultdict

my_list = [('a', [0]), ('b', [1]), ('c', [2]), ('a', [3]), ('d', [4, 5])]

dd = defaultdict(list)
for pair in my_list:
    k, v = pair
    dd[k].extend(v)  
>>> dd.items()
[('a', [0, 3]), ('c', [2]), ('b', [1]), ('d', [4, 5])]