I'm trying to remove "title" element out of this list that I scrape from the web:
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
TypeError: list indices must be integers, not tuple
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. If you're trying to extract a column from a MultiIndex
, there are simpler ways.
You can use list comprehension (generally more common because it is clear, concise and considered 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
:
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.
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']
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.