Python 如何获取二维列表中的每个第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30062429/
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 07:55:09  来源:igfitidea点击:

Python How to get every first element in 2 Dimensional List

pythonlistmultidimensional-arraypython-2.x

提问by CodingBeginner

I have a list like this :

我有一个这样的列表:

a = ((4.0, 4, 4.0), (3.0, 3, 3.6), (3.5, 6, 4.8))

I want an outcome like this (EVERYfirst element in the list) :

我想要这样的结果(列表中的每个第一个元素):

4.0, 3.0, 3.5

I tried a[::1][0], but it doesn't work

我尝试了 [::1][0],但它不起作用

I'm just start learning Python weeks ago. Python version = 2.7.9

我几周前才开始学习 Python。Python 版本 = 2.7.9

采纳答案by Cory Kramer

You can get the index [0]from each element in a list comprehension

您可以[0]从列表理解中的每个元素获取索引

>>> [i[0] for i in a]
[4.0, 3.0, 3.5]

Also just to be pedantic, you don't have a listof list, you have a tupleof tuple.

也正好是迂腐,你没有listlist,你有一个tupletuple

回答by Eric Renouf

You can get it like

你可以得到它

[ x[0] for x in a]

which will return a list of the first element of each list in a

这将返回每个列表的第一个元素的列表 a

回答by Joran Beasley

use zip

使用邮编

columns = zip(*rows) #transpose rows to columns
print columns[0] #print the first column
#you can also do more with the columns
print columns[1] # or print the second column
columns.append([7,7,7]) #add a new column to the end
backToRows = zip(*columns) # now we are back to rows with a new column
print backToRows

you can also use numpy

你也可以使用 numpy

a = numpy.array(a)
print a[:,0]

回答by Akash Srivastava

Try using

尝试使用

for i in a :
  print(i[0])

i represents individual row in a.So,i[0] represnts the 1st element of each row.

i 代表 a.So 中的单个行,i[0] 代表每行的第一个元素。

回答by notilas

Compared the 3 methods

3种方法比较

  1. 2D list: 5.323603868484497 seconds
  2. Numpy library : 0.3201274871826172 seconds
  3. Zip (Thanks to Joran Beasley) : 0.12395167350769043 seconds
  1. 2D 列表:5.323603868484497 秒
  2. Numpy 库:0.3201274871826172 秒
  3. Zip(感谢 Joran Beasley):0.12395167350769043 秒
D2_list=[list(range(100))]*100
t1=time.time()
for i in range(10**5):
    for j in range(10):
        b=[k[j] for k in D2_list]
D2_list_time=time.time()-t1

array=np.array(D2_list)
t1=time.time()        
for i in range(10**5):
    for j in range(10):
        b=array[:,j]        
Numpy_time=time.time()-t1

D2_trans = list(zip(*D2_list)) 
t1=time.time()        
for i in range(10**5):
    for j in range(10):
        b=D2_trans[j]
Zip_time=time.time()-t1

print ('2D List:',D2_list_time)
print ('Numpy:',Numpy_time)
print ('Zip:',Zip_time)

The Zip method works best. It was quite useful when I had to do some column wise processes for mapreduce jobs in the cluster servers where numpy was not installed.

Zip 方法效果最好。当我必须为未安装 numpy 的集群服务器中的 mapreduce 作业执行一些列明智的过程时,它非常有用。

回答by Angel Lira

You could use this:

你可以用这个:

a = ((4.0, 4, 4.0), (3.0, 3, 3.6), (3.5, 6, 4.8))
a = np.array(a)
a[:,0]
returns >>> array([4. , 3. , 3.5])