简体   繁体   English

在 Python 中理解 [foo for bar, foo in arr]

[英]Understanding [foo for bar, foo in arr] in Python

I have a list of tuples, such as [(4, 'c'), (5, 'o'), (6, 'd')] .我有一个元组列表,例如[(4, 'c'), (5, 'o'), (6, 'd')] I am trying to get better at extracting only the data I need.我正在努力更好地仅提取我需要的数据。 I can do it now with for loops or unpacking with zip but Im looking for other methods to expand my knowledge.我现在可以使用 for 循环或使用 zip 解包来完成,但我正在寻找其他方法来扩展我的知识。

I have seen the code below used in several places which extracts the second index of the tuple我在几个地方看到了下面的代码,它提取了元组的第二个索引

letters = [ foo for bar, foo in arr ] 

How does this work?这是如何运作的? and if I wanted to extract the first index, is that possible?如果我想提取第一个索引,这可能吗?

The [.. for .. in ..] syntax denotes a comprehension expression , specifically one for creating a list as indicated by to the enclosing [ ] . [.. for .. in ..]语法表示一个理解表达式,特别是用于创建一个列表,如封闭的[ ] Comprehensions generally work similar to for statements in that they allow to iterate over something, assign or unpack each element, and then operate on each element.推导式通常与for语句类似,因为它们允许迭代某些东西,分配解包每个元素,然后对每个元素进行操作。

[ foo for bar, foo in arr ] 
#                     ^ iterable providing elements
#         ^ assignment of each element to name(s)
# ^ operation for each element

Of note is that the assignment in the middle has the same power as using = for assignment:需要注意的是,中间的赋值与使用=进行赋值具有相同的功能:

  • a single name target is assigned the entire value单个名称目标被分配了整个值
    a = (1, 2) or for a in [(1, 2), (3, 4)] a = (1, 2) or for a in [(1, 2), (3, 4)]
  • multiple name targets are assigned the parts of the value多个名称目标被分配值的部分
    a, b = (1, 2) or for a, b in [(1, 2), (3, 4)] a, b = (1, 2)for a, b in [(1, 2), (3, 4)]

This makes comprehensions convenient to unpack elements and operate on each part individually – for example to combine them or to discard one.这使得理解可以方便地拆开元素并单独对每个部分进行操作 - 例如组合它们或丢弃一个。

Using proper names for the parts of each element makes it more obvious what is going on, and how to change the result:为每个元素的部分使用适当的名称可以更清楚地了解正在发生的事情以及如何更改结果:

>>> # code of the question, using proper names
>>> pairs = [(4, 'c'), (5, 'o'), (6, 'd')]
>>> [letter for number, letter in pairs]
['c', 'o', 'd']
>>> # desired result of the question
>>> [number for number, letter in pairs]
[4, 5, 6]

Comprehensions, especially for lists, are a feature found in many programming languages.推导式,尤其是列表的推导式,是许多编程语言中的一项功能。 They are derived from mathematical "set builder notation" that some people may be familiar with.它们源自某些人可能熟悉的数学“集合构建器符号”。 For example, the mathematical expression "set of numbers in arr"例如,数学表达式“arr中的一组数字”

{ number | { 数量 | (number, letter) ∈ arr} (数字,字母)∈ arr}

directly translates to a set comprehension:直接转化为集合理解:

#      { number | (number, letter) ∈ arr}
# 
nums = {number for number, letter in arr}

You could extract the first index in the same fashion:您可以以相同的方式提取第一个索引:

letters_first = [bar for bar, foo in arr]

This shorthand technique is called "list comprehension" in Python.这种速记技术在 Python 中称为“列表理解” The general syntax is一般语法是

[expression for variable in list] 
arr = [(4, 'c'), (5, 'o'), (6, 'd')]
letters = [ bar for bar, foo in arr ] #  it will extract the first index

This is called list comprehension.这称为列表理解。 It creates a list with foo from bar and foo from arr.它使用来自 bar 的 foo 和来自 arr 的 foo 创建一个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM