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

时间:2017-08-18 05:28:25

标签: python list indexing tuples

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

我已经看过例11.4。 (列出会员资格)http://openbookproject.net/thinkcs/python/english3e/lists.html,并且在某些方面与我的目标非常接近。

该项目是:

  • 从引用(,[值列表])的元组列表开始

    my_list = [('a',[0]), ('b',[1]), ('c',[2]), ('a',[3])]
    
  • 我想扫描一下my_list'为了附加嵌套列表,只为一个组合值列表,如下所示:

    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, [...], [...], [...], [...]], [...], [...], [...]], [...], [...]], [...]])]
    
  • 很抱歉超长线,但我认为它会更符合整个错误......

如果我不够清楚,请不要犹豫告诉我,我会提供更多详情。

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

3 个答案:

答案 0 :(得分:1)

使用OrderedDictimport collections优先)时,这似乎非常简单:

In [438]: dict_ = collections.OrderedDict()

In [439]: for l in my_list:
     ...:     dict_.setdefault(l[0], []).extend(l[1])
     ...:     

In [440]: dict_
Out[440]: OrderedDict([('a', [0, 3]), ('b', [1]), ('c', [2])])

现在,如果你想让你的元组重新遍历字典:

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

答案 1 :(得分:1)

您可以使用defaultdict容器创建列表字典,然后只存储每个元组对的值。请注意,我使用项('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])]

答案 2 :(得分:0)

您可以使用字典构建列表

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, readOnly = false)
public class UserRolesService{
..........
}