[英]Using filter and lambda in a list of lists
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).我想创建一个函数find_title
,它接受想要的标题并返回整个列表(带有标题、网址和统计信息)。 That's my attempt这是我的尝试
def find_title(title, ls):
return(list(filter(lambda x: x[0] == title, ls)))
However it doesn't work, it returns nothing.但是它不起作用,它什么也不返回。 That's probably because x[0]
denotes only the first element in the big list.这可能是因为x[0]
仅表示大列表中的第一个元素。 How can it be fixed?如何修复?
Edit.编辑。 That's a part of ls:这是 ls 的一部分:
[['Der Vagabund und das Kind (1921)',
'http://www.imdb.com/title/tt0012349/',
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],
['Goldrausch (1925)',
'http://www.imdb.com/title/tt0015864/',
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],
['Metropolis (1927)',
'http://www.imdb.com/title/tt0017136/',
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]]
You can directly extract the information from dataframe like this:您可以直接从数据框中提取信息,如下所示:
import pandas as pd
df = pd.DataFrame([['Der Vagabund und das Kind (1921)',
'http://www.imdb.com/title/tt0012349/',
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],
['Goldrausch (1925)',
'http://www.imdb.com/title/tt0015864/',
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],
['Metropolis (1927)',
'http://www.imdb.com/title/tt0017136/',
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]])
print(df[df[0] =='Goldrausch (1925)'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.