Python 将一个列表与另一个嵌套列表(无序)进行比较并输出一个列表
以下是我的清单:Python 将一个列表与另一个嵌套列表(无序)进行比较并输出一个列表,python,list,python-2.7,Python,List,Python 2.7,以下是我的清单: x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']] y = ['2014', '2015', '2014'] 例如,以y[0]为例,将其
x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']
例如,以y[0]为例,将其与x[0][0]~x[2][2]进行比较,然后在x中打印元素在y中的列表(嵌套列表)
此函数应将y中的所有元素与x中的每个元素进行比较
我已经想了两天了,我想不出来。请帮忙 使用列表理解:
list(i for i in x if y[0] in i)
>>> [['What if?', '2014', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
请参见清除循环方法
output = []
for yy in y:
for xlist in x:
for xx in xlist:
if yy == xx:
output.append( xlist )
break
print output
简单的嵌套循环问题。只需遍历
x
中的所有嵌套列表,如果第二个值在y
中,则打印它
for nested in x:
if nested[1] in y:
print(nested)
或者附加到结果列表并在比较后打印,具体取决于您想要什么。据我所知,您希望在
x
中列出出版日期在y
中的书籍。这应该做到:
>>> [b for b in x if b[1] in y]
[['What if?', '2014', 'Randall Munroe'],
['Thing Explainer', '2015', 'Randall Munroe'],
['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y
可能应该是这里的集合。性能增益可以忽略不计,因为y
非常小,但作为一个集合,它传达了您打算如何使用它:
years = {'2014', '2015', '2014'}
最后,您可能希望使用集合
中的命名的tuple
来表示您的书籍。比如:
from collections import namedtuple
Book = namedtuple('Book', 'name year author')
books = [Book(b) for b in x]
然后,上述列表将变为:
[b for b in books if b.year in years]
它既美观又可读。您可以使用内置方法筛选所需内容:
它的作用是:
它通过使用lambda
函数,遍历x
列表中的每个列表,并检查每个子列表的第二个元素是否在y[1]
中找到
编辑:
如果您确定x
的每个子列表中的日期保持相同的索引,即s[1]
,则上述代码将起作用
但在这种情况下,您不能保证,那么我更喜欢下一个代码(我在x
中添加了其他元素,具有不同的日期索引:
>>> z = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge'],['2015','Thing Explainer', 'Randall Munroe'], ['Alan Turing: The Enigma', 'Andrew Hodge','2014']]
>>>
>>>
>>> filter(lambda s: set(s).intersection(y), z)
[['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge'], ['2015', 'Thing Explainer', 'Randall Munroe'], ['Alan Turing: The Enigma', 'Andrew Hodge', '2014']]
运行y,使用“in”检查x
x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']
for a in y:
print("""{}:""".format(a))
for b in x:
if a in b:
print(b)
产生:
2014:
['What if?', '2014', 'Randall Munroe']
['Alan Turing: The Enigma', '2014', 'Andrew Hodge']
2015:
['Thing Explainer', '2015', 'Randall Munroe']
2014:
['What if?', '2014', 'Randall Munroe']
['Alan Turing: The Enigma', '2014', 'Andrew Hodge']
检查此代码:您可以将其作为一个模块运行。它返回一个一致性列表,每个项目都是y中的索引列表和x中的索引列表
def mifunc(x, y):
coincidence = []
for alpha in range(len(x)):
for beta in range(len(x[alpha])):
for gamma in range(len(y)):
if y[gamma] == x[alpha][beta]:
coincidence.append([gamma, [alpha, beta]])
return coincidence
x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']
if __name__ == '__main__':
coincidence = mifunc (x, y)
告诉我我是否需要澄清一些事情。给定列表的预期输出是什么?[['What if','2014',Randall Munroe'],['Thing Explainer','2015','Randall Munroe'],['Alan Turing:the Enigma','2014','Andrew Hodge'][xx代表yy在集合中(y),xx代表xx在x中,如果yy在xx中]
这是一个不完整的解决方案-如果年份不是y
中的第一个元素,该怎么办?
def mifunc(x, y):
coincidence = []
for alpha in range(len(x)):
for beta in range(len(x[alpha])):
for gamma in range(len(y)):
if y[gamma] == x[alpha][beta]:
coincidence.append([gamma, [alpha, beta]])
return coincidence
x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']
if __name__ == '__main__':
coincidence = mifunc (x, y)