4 回答
TA贡献1895条经验 获得超3个赞
没有图书馆:
pools = ['Apple', 'Orange', ...], ['Apple', 'Orange', ...], [...], [...]]
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool if y not in x]
print(result)
TA贡献1803条经验 获得超3个赞
pools = [['Apple', 'Orange','Pineapple'], ['Apple', 'Orange','pizza'],['Apple',
'Orange','pizza','beer']]
results = []
for pool in pools:
results.append([item for item in pool if item not in results][0])
Output: ['Apple', 'Orange', 'pizza']
TA贡献1783条经验 获得超4个赞
我会使用 itertools.product() 来生成组合,然后使用 set() 选择有效组合(没有重复项)。
是这样的:
from itertools import product
person_1 = ['Apple', 'Orange', 'Pineapple', ...]
person_2 = ['Apple', 'Orange', 'Nintendo', ...]
person_3 = ['Yam', 'Doritos', 'Apple', ...]
person_4 = ['Beer', 'Cheese', 'Sushi', ...]
for p in product(person_1, person_2, person_3, person_4):
if len(p) == len(set(p)):
print(p)
TA贡献1817条经验 获得超14个赞
这是使用熊猫的另一种方法:
import pandas as pd
d= {"Person 1" : ['Apple', 'Orange', 'Pineapple', ...],"Person 2" : ['Apple', 'Orange', 'Nintendo', ...],"Person 3" : ['Yam', 'Doritos', 'Apple', ...],"Person 4" : ['Beer', 'Cheese', 'Sushi', ...]}
df = pd.DataFrame(data=d)
df=df.drop_duplicates()
df=df.dropna(axis=0)
print(df)
添加回答
举报