[英]How to make a new list of first elements from existing list of lists
I have a list below.我在下面有一个清单。 I need to use it to create a new list with only country names.
我需要用它来创建一个只有国家/地区名称的新列表。 How do I loop
x
in order to have a list of country names?如何循环
x
以获得国家/地区名称列表?
x = [["UK", "LONDON", "EUROPE"],
["US", "WASHINGTON", "AMERICA"],
["EG", "CAIRO", "AFRICA"],
["JP", "TOKYO", "ASIA"]]
The outcome should look like结果应该是这样的
UK
US
EG
JP
You have two ways你有两种方法
Using a for loop使用 for 循环
countries = []
for e in x:
countries.append(e[0])
or with list comprehensions, which would be in most cases the better option或使用列表推导式,这在大多数情况下是更好的选择
countries = [e[0] for e in x]
Furthermore, if your data source is a generator (which it isn't in this case), or if you're doing some expensive processing on each element (not this case either), you could use a generator expression
by changing the square brackets []
for parenthesis ()
此外,如果您的数据源是一个生成器(在这种情况下不是),或者如果您对每个元素进行一些昂贵的处理(也不是这种情况),您可以通过更改方括号来使用
generator expression
[]
为括号()
countries = (e[0] for e in x)
This will compute on demand the elements, and if the data source is too long or a generator will also reduce the memory footprint compared to a list comprehension.这将按需计算元素,如果数据源太长或生成器也将减少与列表理解相比的内存占用。
The most readable way is probably:最易读的方式可能是:
>>> data = [["UK", "LONDON", "EUROPE"],
["US", "WASHINGTON", "AMERICA"],
["EG", "CAIRO", "AFRICA"],
["JP","TOKYO","ASIA"]]
>>> countries = [country for country, city, continent in data]
>>> countries
['UK', 'US', 'EG', 'JP']
This list comprehension makes it clear what the three values in each item from data
are, and which will be in the output, whereas the index 0
doesn't tell the reader much at all.这个列表推导式清楚地说明了
data
中每个项目中的三个值是什么,以及哪些将在输出中,而索引0
根本没有告诉读者太多。
As cities LONDON
, WASHINGTON
, CAIRO
, TOKYO
are present in 1st position(starting from 0) on list items.由于城市
LONDON
, WASHINGTON
, CAIRO
, TOKYO
出现在列表项的第 1 位(从 0 开始)。 So get all 1st item from the list items by list compression.因此,通过列表压缩从列表项中获取所有第一项。
eg例如
>>> x= [["UK", "LONDON", "EUROPE"],["US", "WASHINGTON", "AMERICA"],["EG", "CAIRO", "AFRICA"],["JP","TOKYO","ASIA"]]
>>> [i[1] for i in x]
['LONDON', 'WASHINGTON', 'CAIRO', 'TOKYO']
>>>
same for countries:国家相同:
>>> [i[0] for i in x]
['UK', 'US', 'EG', 'JP']
If you can find yourself assuming the data x
always contains the list in form of [COUNTRY, CITYNAME, CONTINENT]
, you can retrieve every first items from each list in x
like below:如果您发现自己假设数据
x
始终包含[COUNTRY, CITYNAME, CONTINENT]
形式的列表,您可以从x
每个列表中检索每个第一项,如下所示:
countries = []
for country, cityname, continent in x:
countries.append(country)
You can shorten above using list comprehension.您可以使用列表理解缩短上面的内容。
>>> countries = [country for country, cityname, continent in x]
Of course you can access the value via an index.当然,您可以通过索引访问该值。
>>> countries = [lst[0] for lst in x]
You can also unpack the list and append to new list:您还可以解压缩列表并附加到新列表:
x= [["UK", "LONDON", "EUROPE"],["US", "WASHINGTON", "AMERICA"],["EG", "CAIRO", "AFRICA"],["JP","TOKYO","ASIA"]]
countries = []
for a,b,c in x:
countries.append(a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.