I working with a list of lists. Each of those lists are the same — they contain title, url and some additional statistics (always in the same order).
I would like to create a function find_title
, which takes the wanted title and returns the whole list (with title, url and statistics). That’s my attempt
JavaScript
x
3
1
def find_title(title, ls):
2
return(list(filter(lambda x: x[0] == title, ls)))
3
However it doesn’t work, it returns nothing. That’s probably because x[0]
denotes only the first element in the big list. How can it be fixed?
Edit. That’s a part of ls:
JavaScript
1
10
10
1
[['Der Vagabund und das Kind (1921)',
2
'http://www.imdb.com/title/tt0012349/',
3
0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
4
['Goldrausch (1925)',
5
'http://www.imdb.com/title/tt0015864/',
6
0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
7
['Metropolis (1927)',
8
'http://www.imdb.com/title/tt0017136/',
9
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]]
10
Advertisement
Answer
You can directly extract the information from dataframe like this:
JavaScript
1
13
13
1
import pandas as pd
2
df = pd.DataFrame([['Der Vagabund und das Kind (1921)',
3
'http://www.imdb.com/title/tt0012349/',
4
0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
5
['Goldrausch (1925)',
6
'http://www.imdb.com/title/tt0015864/',
7
0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
8
['Metropolis (1927)',
9
'http://www.imdb.com/title/tt0017136/',
10
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]])
11
12
print(df[df[0] =='Goldrausch (1925)'])
13