[英]Unlisting a python list and removing element
I'm trying to remove "title" element out of this list that I scrape from the web: 我正在尝试从网络上删除的此列表中删除“ title”元素:
x =
[[(u'title', u'Goals for')], [(u'title', u'Goals against')], [(u'title', u'Penalty goal')], [(u'title', u'Goals for average')], [(u'title', u'Matches Played')], [(u'title', u'Shots on goal')], [(u'title', u'Shots Wide')], [(u'title', u'Free Kicks Received')], [(u'title', u'Offsides')], [(u'title', u'Corner kicks')], [(u'title', u'Wins')], [(u'title', u'Draws')], [(u'title', u'Losses')]]
I would like my final to be 我希望我的决赛成为
result = ['Goals for', 'Goals against','Penalty goal','Goals for average',....]
But I can do y = x[1][0][1]
=> 'Goals for' I couldn't not do x[i][0][1]
since it is index in my for loop statement I got error 但是我可以做到y = x[1][0][1]
=>'Goals for'我不能做x[i][0][1]
因为它是我的for循环语句中的索引,我得到了错误
TypeError: list indices must be integers, not tuple TypeError:列表索引必须是整数,而不是元组
How could I solve this? 我该如何解决?
I'd use a list comprehension: 我会使用列表理解:
>>> new = [sublist[0][1] for sublist in x]
>>> pprint.pprint(new)
[u'Goals for',
u'Goals against',
u'Penalty goal',
u'Goals for average',
u'Matches Played',
u'Shots on goal',
u'Shots Wide',
u'Free Kicks Received',
u'Offsides',
u'Corner kicks',
u'Wins',
u'Draws',
u'Losses']
Not sure what the pandas
connection is, though. 不过,不确定pandas
连接是什么。 If you're trying to extract a column from a MultiIndex
, there are simpler ways. 如果您尝试从MultiIndex
提取列,则有更简单的方法。
You can use list comprehension (generally more common because it is clear, concise and considered Pythonic): 您可以使用列表理解(通常更常见,因为它清晰,简洁并且被认为是Pythonic):
x = [[(u'title', u'Goals for')], [(u'title', u'Goals against')], [(u'title', u'Penalty goal')], [(u'title', u'Goals for average')], [(u'title', u'Matches Played')], [(u'title', u'Shots on goal')], [(u'title', u'Shots Wide')], [(u'title', u'Free Kicks Received')], [(u'title', u'Offsides')], [(u'title', u'Corner kicks')], [(u'title', u'Wins')], [(u'title', u'Draws')], [(u'title', u'Losses')]]
x = [i[0][1:] for i in x]
Or you can use a for
loop over the length of x
: 或者,您可以在x
的长度上使用for
循环:
for i in range(len(x)):
x[i] = x[i][0][1:]
As was pointed out after the original answer, my other original suggestion to use Python's del
statement (eg del x[0][0][0]
) also wouldn't have worked because tuple
does not support item deletion. 正如原始答案之后所指出的那样,我其他使用Python的del
语句的原始建议(例如del x[0][0][0]
)也不会起作用,因为tuple
不支持项目删除。
just have a try: 尝试一下:
x = [[('title', 'Goals for')], [('title', 'Goals against')], [('title', 'Penalty goal')], [('title', 'Goals for average')], [('title', 'Matches Played')], [('title', 'Shots on goal')], [('title', 'Shots Wide')], [('title', 'Free Kicks Received')], [('title', 'Offsides')], [('title', 'Corner kicks')], [('title', 'Wins')], [('title', 'Draws')], [('title', 'Losses')]]
print([element[0][1] for element in x ])
Other solution: 其他解决方案:
>>> map(lambda a: a[0][1], x)
... [u'Goals for', u'Goals against', u'Penalty goal', u'Goals for average', u'Matches Played', u'Shots on goal', u'Shots Wide', u'Free Kicks Received', u'Offsides', u'Corner kicks', u'Wins', u'Draws', u'Losses']
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.