如何在 Python 中对元组列表进行 enumerate() ?

发布于 2024-07-18 18:25:27 字数 603 浏览 9 评论 0原文

我有一些这样的代码:

letters = [('a', 'A'), ('b', 'B')]
i = 0
for (lowercase, uppercase) in letters:
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)
    i += 1

我被告知有一个 enumerate() 函数可以为我处理“i”变量:

for i, l in enumerate(['a', 'b', 'c']):
    print "%d: %s" % (i, l)

但是,我不知道如何将两者结合起来:如何当相关列表由元组组成时,我使用枚举吗? 我必须这样做吗?

letters = [('a', 'A'), ('b', 'B')]
for i, tuple in enumerate(letters):
    (lowercase, uppercase) = tuple
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)

或者有更优雅的方式吗?

I've got some code like this:

letters = [('a', 'A'), ('b', 'B')]
i = 0
for (lowercase, uppercase) in letters:
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)
    i += 1

I've been told that there's an enumerate() function that can take care of the "i" variable for me:

for i, l in enumerate(['a', 'b', 'c']):
    print "%d: %s" % (i, l)

However, I can't figure out how to combine the two: How do I use enumerate when the list in question is made of tuples? Do i have to do this?

letters = [('a', 'A'), ('b', 'B')]
for i, tuple in enumerate(letters):
    (lowercase, uppercase) = tuple
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)

Or is there a more elegant way?

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

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

发布评论

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

评论(4

不念旧人 2024-07-25 18:25:27

这是一个巧妙的方法:

letters = [('a', 'A'), ('b', 'B')]
for i, (lowercase, uppercase) in enumerate(letters):
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)

This is a neat way to do it:

letters = [('a', 'A'), ('b', 'B')]
for i, (lowercase, uppercase) in enumerate(letters):
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)
似狗非友 2024-07-25 18:25:27

这就是我要做的:

import itertools

letters = [('a', 'A'), ('b', 'B')]
for i, lower, upper in zip(itertools.count(),*zip(*letters)):
    print "Letter #%d is %s/%s" % (i, lower, upper)

编辑:解包变得多余。 这是一种更紧凑的方式,可能有效也可能无效,具体取决于您的用例:

import itertools

letters = [('a', 'A'), ('b', 'B')]
for i in zip(itertools.count(),*zip(*letters)):
    print "Letter #%d is %s/%s" % i

This is how I'd do it:

import itertools

letters = [('a', 'A'), ('b', 'B')]
for i, lower, upper in zip(itertools.count(),*zip(*letters)):
    print "Letter #%d is %s/%s" % (i, lower, upper)

EDIT: unpacking becomes redundant. This is a more compact way, which might work or not depending on your use case:

import itertools

letters = [('a', 'A'), ('b', 'B')]
for i in zip(itertools.count(),*zip(*letters)):
    print "Letter #%d is %s/%s" % i
離人涙 2024-07-25 18:25:27

你也可以这样做:

letters = [('a', 'A'), ('b', 'B')]
for i, letter in enumerate(letters):
    print "Letter #%d is %s/%s" % (i, letter[0], letter[1])

You can do this way too:

letters = [('a', 'A'), ('b', 'B')]
for i, letter in enumerate(letters):
    print "Letter #%d is %s/%s" % (i, letter[0], letter[1])
﹎☆浅夏丿初晴 2024-07-25 18:25:27

您还可以编写一个生成器:

def enumerate_nested(nested_collection, start=0):

    for index, row in enumerate(nested_collection, start):
        yield index, *row

然后,它允许您迭代元组(或列表)的集合并解压缩值:

names = [["Heinz", "Steiner"], ["Fred", "Glauser"], ["Nicole", "Hauser"]]

for index, first_name, last_name in enumerate_nested(names, 1):
    print(index, first_name, last_name)

但是,有一个更简单的内置解决方案可以实现此目的。 看看 Richie Hindle 的回答

names = [["Heinz", "Steiner"], ["Fred", "Glauser"], ["Nicole", "Hauser"]]

for index, (first_name, last_name) in enumerate(names, 1):
    print(index, first_name, last_name)

You could also write a generator:

def enumerate_nested(nested_collection, start=0):

    for index, row in enumerate(nested_collection, start):
        yield index, *row

Which then allows you to iterate over the collection of tuple (or list) and to unpack the values:

names = [["Heinz", "Steiner"], ["Fred", "Glauser"], ["Nicole", "Hauser"]]

for index, first_name, last_name in enumerate_nested(names, 1):
    print(index, first_name, last_name)

However, there is a simpler built-in solution to achieve this. Have a look at Richie Hindle's answer:

names = [["Heinz", "Steiner"], ["Fred", "Glauser"], ["Nicole", "Hauser"]]

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