将元组列表转换为字典

发布于 2024-07-08 04:22:02 字数 265 浏览 6 评论 0原文

我有一个像这样的元组列表:

[
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

我想通过第一个项目迭代这个键控,因此,例如,我可以打印这样的内容:

a 1 2 3
b 1 2
c 1

如果不保留一个项目来跟踪第一个项目是否和我循环元组一样吗? 这感觉相当混乱(而且我必须先对列表进行排序)......

I have a list of tuples like this:

[
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

I want to iterate through this keying by the first item, so, for example, I could print something like this:

a 1 2 3
b 1 2
c 1

How would I go about doing this without keeping an item to track whether the first item is the same as I loop around the tuples? This feels rather messy (plus I have to sort the list to start with)...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不打扰别人 2024-07-15 04:22:02
l = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

d = {}
for x, y in l:
    d.setdefault(x, []).append(y)
print d

产生:

{'a': [1, 2, 3], 'c': [1], 'b': [1, 2]}
l = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

d = {}
for x, y in l:
    d.setdefault(x, []).append(y)
print d

produces:

{'a': [1, 2, 3], 'c': [1], 'b': [1, 2]}
彡翼 2024-07-15 04:22:02

稍微简单一点...

from collections import defaultdict

fq = defaultdict(list)
for n, v in myList:
    fq[n].append(v)
    
print(fq) # defaultdict(<type 'list'>, {'a': [1, 2, 3], 'c': [1], 'b': [1, 2]})

Slightly simpler...

from collections import defaultdict

fq = defaultdict(list)
for n, v in myList:
    fq[n].append(v)
    
print(fq) # defaultdict(<type 'list'>, {'a': [1, 2, 3], 'c': [1], 'b': [1, 2]})
执笏见 2024-07-15 04:22:02

使用 groupby 输出的解决方案

from itertools import groupby
l = [('a',1), ('a', 2),('a', 3),('b', 1),('b', 2),('c', 1),]
[(label, [v for l,v in value]) for (label, value) in groupby(l, lambda x:x[0])]

[('a', [1, 2, 3]), ('b', [1, 2]), ('c', [1])]

groupby(l, lambda x:x[0]) 为您提供一个迭代器,其中包含

['a', [('a', 1), ...], c, [('c', 1)], ...]

A solution using groupby

from itertools import groupby
l = [('a',1), ('a', 2),('a', 3),('b', 1),('b', 2),('c', 1),]
[(label, [v for l,v in value]) for (label, value) in groupby(l, lambda x:x[0])]

Output:

[('a', [1, 2, 3]), ('b', [1, 2]), ('c', [1])]

groupby(l, lambda x:x[0]) gives you an iterator that contains

['a', [('a', 1), ...], c, [('c', 1)], ...]
往事随风而去 2024-07-15 04:22:02

我只会做基本的

answer = {}
for key, value in list_of_tuples:
  if key in answer:
    answer[key].append(value)
  else:
    answer[key] = [value]

如果这么短,为什么要使用复杂的东西。 当然,如果您不介意使用 setdefault 也没关系。

I would just do the basic

answer = {}
for key, value in list_of_tuples:
  if key in answer:
    answer[key].append(value)
  else:
    answer[key] = [value]

If it's this short, why use anything complicated. Of course if you don't mind using setdefault that's okay too.

草莓酥 2024-07-15 04:22:02

打印按第一项分组的元组列表

此答案基于 @gommen 一个

#!/usr/bin/env python

from itertools import groupby
from operator  import itemgetter

L = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

key = itemgetter(0)
L.sort(key=key) #NOTE: use `L.sort()` if you'd like second items to be sorted too
for k, group in groupby(L, key=key):
    print k, ' '.join(str(item[1]) for item in group)

输出:

a 1 2 3
b 1 2
c 1

Print list of tuples grouping by the first item

This answer is based on the @gommen one.

#!/usr/bin/env python

from itertools import groupby
from operator  import itemgetter

L = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

key = itemgetter(0)
L.sort(key=key) #NOTE: use `L.sort()` if you'd like second items to be sorted too
for k, group in groupby(L, key=key):
    print k, ' '.join(str(item[1]) for item in group)

Output:

a 1 2 3
b 1 2
c 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文