91D*_*Dev 3 python tuples list
我有一个这样的元组
[
(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)
]
Run Code Online (Sandbox Code Playgroud)
我想代替这个:
[
(379146591, (('it', 55, 1, 1, 'NON ENTRARE', 'NonEntrate', 55, 1)),
(4746004, (('it', 28, 2, 2, 'NON ENTRARE', 'NonEntrate', 26, 2), ('it', 28, 2, 2, 'TheBestTroll Group', 'TheBestTrollGroup', 2, 3)))
]
Run Code Online (Sandbox Code Playgroud)
因此,对于任何元素,不是第一个元素的任何元素都在它的子元组之内,如果后一个元素与第一个元素具有相同的元素,它将被设置为前一个子元组的另一个子元组。
所以我可以做:
for i in data:
# getting the first element of the list
for sub_i in i[1]:
# i access all the tuples inside
Run Code Online (Sandbox Code Playgroud)
有一些功能可以做到这一点吗?
这很简单defaultdict;您将默认值初始化为列表,然后将该项目附加到同一键的值:
lst = [
(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)
]
from collections import defaultdict ?
d = defaultdict(list)
for k, *v in lst:
d[k].append(v)
list(d.items())
#[(4746004,
# [('it', 28, 2, 2, 'NON ENTRARE', 'NonEntrate', 26, 2),
# ('it', 28, 2, 2, 'TheBestTroll Group', 'TheBestTrollGroup', 2, 3)]),
# (379146591, [('it', 55, 1, 1, 'NON ENTRARE', 'NonEntrate', 55, 1)])]
Run Code Online (Sandbox Code Playgroud)
如果顺序很重要,请使用OrderedDict可以记住插入顺序的:
from collections import OrderedDict
d = OrderedDict()
?
for k, *v in lst:
d.setdefault(k, []).append(v)
list(d.items())
#[(379146591, [['it', 55, 1, 1, 'NON ENTRARE', 'NonEntrate', 55, 1]]),
# (4746004,
# [['it', 28, 2, 2, 'NON ENTRARE', 'NonEntrate', 26, 2],
# ['it', 28, 2, 2, 'TheBestTroll Group', 'TheBestTrollGroup', 2, 3]])]
Run Code Online (Sandbox Code Playgroud)