我正在尝试抓取Python中列表中列表的第二项。
示例:
people = [['John Doe', u'', u'25.78', u''], ['John Doe', u'', u'13.39', u''], ['John Doe', u'', u'11.93', u'0.00'], ['John Doe', u'', u'14.97', u'0.00'], ['John Doe', u'', u'14.34', u''], ['John Doe', u'', u'21.08', u''], ['John Doe', u'', u'13.24', u''], ['John Doe', u'', u'13.11', u'0.00'], ['John Doe', u'', u'', u''], ['John Doe', u'', u'19.45', u'0.00'], ['John Doe', u'', u'17.56', u''], ['John Doe', u'', u'20.57', u''], ['John Doe', u'', u'28.50', u''], ['John Doe', u'', u'24.38', u'0.00'], ['John Doe', u'', u'31.13', u''], ['John Doe', u'', u'17.20', u''], ['John Doe', u'', u'18.52', u'0.00'], ['John Doe', u'', u'6.42', u'0.00'], ['John Doe', u'', u'17.31', u'']]
我要做的是抓住列表中每个列表的第三个元素。
我希望我的回归:
['25.78','13.39','11.93','14.97','14.34']
等等。
此外,奖励积分。列表中每个项目前面的u
是否会影响任何内容?
答案 0 :(得分:2)
In [57]: people = [['John Doe', u'', u'25.78', u''], ['John Doe', u'', u'13.39', u''], ['John Doe', u'', u'11.93', u'0.00'], ['John Doe', u'', u'14.97', u'0.00'], ['John Doe', u'', u'14.34', u''], ['John Doe', u'', u'21.08', u''], ['John Doe', u'', u'13.24', u''], ['John Doe', u'', u'13.11', u'0.00'], ['John Doe', u'', u'', u''], ['John Doe', u'', u'19.45', u'0.00'], ['John Doe', u'', u'17.56', u''], ['John Doe', u'', u'20.57', u''], ['John Doe', u'', u'28.50', u''], ['John Doe', u'', u'24.38', u'0.00'], ['John Doe', u'', u'31.13', u''], ['John Doe', u'', u'17.20', u''], ['John Doe', u'', u'18.52', u'0.00'], ['John Doe', u'', u'6.42', u'0.00'], ['John Doe', u'', u'17.31', u'']]
In [58]: [p[2] for p in people]
Out[58]:
[u'25.78',
u'13.39',
u'11.93',
u'14.97',
u'14.34',
u'21.08',
u'13.24',
u'13.11',
u'',
u'19.45',
u'17.56',
u'20.57',
u'28.50',
u'24.38',
u'31.13',
u'17.20',
u'18.52',
u'6.42',
u'17.31']
字符串前面的u
基本上表示它们是unicode字符串。如果你想将它们转换为常规的ascii字符串(假设它们只有ascii字符):
In [59]: [str(p[2]) for p in people]
Out[59]:
['25.78',
'13.39',
'11.93',
'14.97',
'14.34',
'21.08',
'13.24',
'13.11',
'',
'19.45',
'17.56',
'20.57',
'28.50',
'24.38',
'31.13',
'17.20',
'18.52',
'6.42',
'17.31']
答案 1 :(得分:1)
您可以使用简单的列表推导来获取第三个元素。
numbers = [i[2] for i in people]
u
表示它是一个unicode字符串。假设它包含ascii
个字符(在这种情况下它包含),如果你不想要unicode,你可以使用str
。
numbers = [str(i[2]) for i in people]
答案 2 :(得分:1)
result = [each[2] for each in people]
print(result)
答案 3 :(得分:0)
在这里它是numpy
people = numpy.array(people)
print people[:,2]
答案 4 :(得分:0)
另外,只是为了好玩:
result = []
for x in range(len(people)):
result.append(people[x][2])