Python:元素列表

发布于 2025-01-23 04:01:12 字数 200 浏览 2 评论 0原文

请,如果我有一个元组列表,例如:

list = [('James',27,2000),('Aaron',24,1290),('Max',23,2300),('Ben', 27,1900)]

  1. 我如何按顺序获得所有名称?

  2. 如何获得以姓名上升顺序显示元组的列表?

谢谢

Please, if I have a list of tuples, say:

list = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]

  1. How do I get all the names in ascending order?

  2. How do I get a list that shows the tuples in ascending order of names?

Thanks

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

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

发布评论

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

评论(6

巷雨优美回忆 2025-01-30 04:01:12

如果您对元组进行排序,则默认情况下,将元组中的第一项作为钥匙。因此:

list_ = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]

for name, *_ in sorted(list_):
    print(name)

输出:

Aaron
Ben
James
Max

或,如果您想要元素,

for t in sorted(list_):
    print(t)

则会产生:...

('Aaron', 24, 1290)
('Ben', 27, 1900)
('James', 27, 2000)
('Max', 23, 2300)

If you sort a list of tuples, by default the first item in the tuple is taken as the key. Therefore:

list_ = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]

for name, *_ in sorted(list_):
    print(name)

Output:

Aaron
Ben
James
Max

Or, if you want the tuples then:

for t in sorted(list_):
    print(t)

...which produces:

('Aaron', 24, 1290)
('Ben', 27, 1900)
('James', 27, 2000)
('Max', 23, 2300)
成熟稳重的好男人 2025-01-30 04:01:12

假设您是字母顺序的?
您可以使用排序

a = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
sorted(a, key=lambda x: x[0])

输出:

[('Aaron', 24, 1290),
 ('Ben', 27, 1900),
 ('James', 27, 2000),
 ('Max', 23, 2300)]

Assuming by ascending you mean alphabetical?
You can do it with sorted:

a = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
sorted(a, key=lambda x: x[0])

Output:

[('Aaron', 24, 1290),
 ('Ben', 27, 1900),
 ('James', 27, 2000),
 ('Max', 23, 2300)]
極樂鬼 2025-01-30 04:01:12
lst = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]

print(sorted(lst))

印刷

[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]
lst = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]

print(sorted(lst))

Prints

[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]
唔猫 2025-01-30 04:01:12
list = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
sorted_names = sorted([i[0] for i in list])
print(sorted_names)

输出:

['Aaron', 'Ben', 'James', 'Max']
sorted_by_name = sorted(list, key=lambda tup: tup[0])
print(sorted_by_name)

输出:

[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]
list = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
sorted_names = sorted([i[0] for i in list])
print(sorted_names)

Output:

['Aaron', 'Ben', 'James', 'Max']
sorted_by_name = sorted(list, key=lambda tup: tup[0])
print(sorted_by_name)

Output:

[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]
厌倦 2025-01-30 04:01:12
>>> l = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
>>> sorted([name for name, _, _ in l])
['Aaron', 'Ben', 'James', 'Max']
>>> sorted(l)
[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]

请注意,list是Python中的对象,因此,当您使用名称list创建一个变量时,您覆盖该参考在您的程序中。

>>> l = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
>>> sorted([name for name, _, _ in l])
['Aaron', 'Ben', 'James', 'Max']
>>> sorted(l)
[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]

Be advised that list is an object in Python, so when you create a variable with the name list, you overwrite that reference and you cannot use list(something) after in your program.

不气馁 2025-01-30 04:01:12

您可以在中使用 sort> sort排序函数。这是一个示例:

my_list = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
my_list_sorted_by_names = sorted(my_list, key=lambda x: x[0])

结果将是:

[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]

您可以使用列表理解来从排序列表中提取名称:

ordered_names = [i[0] for i in my_list_sorted_by_names]

You can use key parameter in sort or sorted function. Here's an example:

my_list = [('James', 27, 2000), ('Aaron', 24, 1290), ('Max', 23, 2300), ('Ben', 27, 1900)]
my_list_sorted_by_names = sorted(my_list, key=lambda x: x[0])

And the result will be:

[('Aaron', 24, 1290), ('Ben', 27, 1900), ('James', 27, 2000), ('Max', 23, 2300)]

You can use list comprehension to extract names from sorted list:

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