简体   繁体   English

如何从现有列表列表中创建第一个元素的新列表

[英]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.

相关问题 从现有列表中的元素形成新列表并分配工作表名称 - Form new lists from elements in an existing list and assign a worksheet name 如何制作一个 function 来创建一个新列表,该列表通过添加在其他两个列表中添加元素? (取决于第一个列表的长度) - How to make a function that creates a new list which adds elements in two other lists by addition? (Depends on the length of the first list) python从两个列表中创建一个新列表,第二个列表表示重复第一个列表中的元素的次数 - python making a new list from two lists, second list says how many times to repeat the elements in the first list 比较两个列表中的元素并创建一个新列表 - Compare elements in two lists and make a new List 如何从 python 中现有的两个列表中创建新列表? - How to make new list from existing two list in python? 根据列表元素之一的值,从现有列表中准备4个列表 - Prepare 4 lists from existing lists based on the value of one of the list elements 如何自动从另一个列表的元素中创建 python 中的列表 - How to automate to make lists in python from the elements of another List 如何使用现有列表中的第一个列表中的所有元素但索引较少的现有列表在python中创建列表 - how to create a list in python from an existing list with all the elements from first list but with lesser indexes 从现有列表创建新的累积列表列表 - Creating new cumulative list of lists from existing list 根据给定的第一个元素从列表列表中检索列表 - Retrieve lists from a list of lists based on the given first elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM