我有一个这样的元组
[
(379146591, 'it', 55, 1, 1, 'NON ENTRARE', 'NonEntrate', 55, 1),
(4746004, 'it', 28, 2, 2, 'NON ENTRARE', 'NonEntrate', 26, 2),
(4746004, 'it', 28, 2, 2, 'TheBestTroll Group', 'TheBestTrollGroup', 2, 3)
]
我想换成这样:
^{pr2}$所以对于任何元素,任何不是第一个元素的元素都在它的一个子元组中,如果下面的元素与第一个元素具有相同的元素,它将被设置为前一个元素的另一个子元组。在
所以我可以:
for i in data:
# getting the first element of the list
for sub_i in i[1]:
# i access all the tuples inside
有什么功能可以做到这一点吗?在
你可以使用collection.defaultdict公司名称:
您可以使用Python3变量解包和
OrderedDict
来保持顺序:输出:
^{2}$使用
defaultdict
非常简单;您将默认值初始化为一个列表,然后将该项附加到同一个键的值:如果顺序很重要,请使用可以记住插入顺序的
^{2}$OrderedDict
:相关问题 更多 >
编程相关推荐