我是初学者,请多多包涵!我有一段代码:
def friendslike(network,user):
friends=[]
friendgames=[]
friends+=get_connections(network,user)
for user in friends:
friendgames.append(get_games_liked(network,user))
return friendgames
返回:
[['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man'], ['Call of Arms', 'Dwarves and Swords'], ['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']]
我想要做的是返回出现在两个或多个列表中的游戏名称。我不确定如何解决这个问题 - 是拆分列表,还是某种交叉查询。
正如我所说,我是 Python 的新手,一般来说是编码方面的新手。感谢任何指点 - 即使它只是告诉我要研究什么。
请您参考如下方法:
我会使用 collections.Counter .它是一个类似 dict 的结构,可以计算你给它的东西。这就像 dict 的自动计数功能。
import collections
friendgames = [['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man'],
['Call of Arms', 'Dwarves and Swords'],
['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']]
c = collections.Counter(b for a in friendgames for b in a)
print [a for a,b in c.items() if b >= 2]
这会打印出至少出现两次的唯一条目,即:
['Super Mushroom Man']
