如何从python中的现有列表创建新的元素列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28589583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:29:10  来源:igfitidea点击:

how to make new list of elements from existing list in python?

pythonlistloops

提问by Sergiu Costas

I have a list below. I need to use it to create a new list with only COUNTRY Names. Please help me to Loop X in order to have a list of Country Name

我在下面有一个清单。我需要用它来创建一个只有国家/地区名称的新列表。请帮我循环 X 以获得国家/地区名称列表

x= [["UK", "LONDON", "EUROPE"],["US", "WASHINGTON", "AMERICA"],["EG", "CAIRO", "AFRICA"],["JP","TOKYO","ASIA"]]

the outcome should looks like

结果应该是这样的

UK
US
EG
JP

采纳答案by Ignacio Vergara Kausel

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 expressionby changing the square brackets []for curly ones {}

此外,如果您的数据源是一个生成器(在这种情况下不是),或者如果您对每个元素进行一些昂贵的处理(也不是这种情况),您可以generator expression通过将方括号更改[]为 curl来使用 a那些{}

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.

这将按需计算元素,如果数据源太长或生成器也将减少与列表理解相比的内存占用。

回答by Vivek Sable

As cities LONDON, WASHINGTON, CAIRO, TOKYOare present in 1st position(starting from 0) on list items. So get all 1st item from the list items by list compression.

由于城市LONDONWASHINGTONCAIROTOKYO出现在列表项的第一个位置(从 0 开始)。因此,通过列表压缩从列表项中获取所有第一项。

e.g.

例如

>>> 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']

回答by jonrsharpe

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 comprehensionmakes it clear what the three values in each item from dataare, and which will be in the output, whereas the index 0doesn't tell the reader much at all.

这个列表理解清楚地表明每个项目中的三个值是什么data,哪些将在输出中,而索引0根本没有告诉读者太多。

回答by yoloseem

If you can find yourself assuming the data xalways contains the list in form of [COUNTRY, CITYNAME, CONTINENT], you can retrieve every first items from each list in xlike 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]

回答by Waqas Khalid Obeidy

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)