我是新手,现在已经工作了一个星期,但找不到任何解决方案。我希望有人可以帮我解决这个问题。
如何在列表中找到项目 - 列表项并单独输出项目?
listitems = ['Beer, Chicken', 'Cake, Chocolate', 'Lemon with ci, Chicken', 'Beer, Beer, Cake, Chocolate']
无论如何,我可以在收据清单中计算相关食物吗?
到目前为止,我只能找到一件食品。我的代码如下:
我还计算了每个项目的另一个列表供比较。
eachitems = ['Beer', 'Cake', 'Chocolate', 'Lemon with ci', 'Chicken']
答案 0 :(得分:0)
我个人会使用基于每个项目的键的词典以及相关项目作为其值,也可以更容易地获得您想要的结果,而不是确切地知道如何从列表中完成它只做了。
从原始代码中,在末尾添加“print(combi)”,“print(checklist)”和“print(correlist)”,你会发现它并没有真正按照你想要的方式添加它。
答案 1 :(得分:0)
在Python 3.5中:
import itertools
listOfStrings = ['Beer, Chicken', 'Cake, Chocolate', 'Lemon with ci, Chicken', 'Beer, Beer, Cake, Chocolate']
print ('Original list:', listOfStrings)
print ()
listOfLists = [group.replace (', ', ',') .split (',') for group in listOfStrings]
print ('Turned into list of lists:', listOfLists)
print ()
allFoods = set (itertools.chain (*listOfLists))
print ('All individual foods:', allFoods)
print ()
listOfSets = [set (group) for group in listOfLists]
print ('A list of sets is handy, since sets contain no duplicates:', listOfSets)
print ()
dictOfFoods = dict ([[food, set ()] for food in allFoods])
print ('Prepare a dictionary, where we can put the associated foods:', dictOfFoods)
print ()
for food in dictOfFoods:
for foodSet in listOfSets:
if food in foodSet:
dictOfFoods [food] .update (foodSet)
dictOfFoods [food] .remove (food)
print ('The dictionary is now filled:', dictOfFoods)
print ()
for food in dictOfFoods:
print ('People who buy', food, 'also buy:')
for otherFood in dictOfFoods [food]:
print (otherFood)
print ()
将打印:
Original list: ['Beer, Chicken', 'Cake, Chocolate', 'Lemon with ci, Chicken', 'Beer, Beer, Cake, Chocolate']
Turned into list of lists: [['Beer', 'Chicken'], ['Cake', 'Chocolate'], ['Lemon with ci', 'Chicken'], ['Beer', 'Beer', 'Cake', 'Chocolate']]
All individual foods: {'Chocolate', 'Lemon with ci', 'Chicken', 'Cake', 'Beer'}
A list of sets is handy, since sets contain no duplicates: [{'Chicken', 'Beer'}, {'Chocolate', 'Cake'}, {'Chicken', 'Lemon with ci'}, {'Chocolate', 'Cake', 'Beer'}]
Prepare a dictionary, where we can put the associated foods: {'Chocolate': set(), 'Lemon with ci': set(), 'Cake': set(), 'Beer': set(), 'Chicken': set()}
The dictionary is now filled: {'Chocolate': {'Cake', 'Beer'}, 'Lemon with ci': {'Chicken'}, 'Cake': {'Chocolate', 'Beer'}, 'Beer': {'Chocolate', 'Chicken', 'Cake'}, 'Chicken': {'Lemon with ci', 'Beer'}}
People who buy Chocolate also buy:
Cake
Beer
People who buy Lemon with ci also buy:
Chicken
People who buy Cake also buy:
Chocolate
Beer
People who buy Beer also buy:
Chocolate
Chicken
Cake
People who buy Chicken also buy:
Lemon with ci
Beer
如果你不想使用itertools和*,你也可以在循环中循环遍历listOfLists的所有元素,并将它们添加到最初为空的allFoods中。
答案 2 :(得分:0)
我花了一些时间才准确理解你想要的东西,但这是一个有效的解决方案。
listitems = ['Beer, Chicken', 'Cake, Chocolate', 'Lemon with ci, Chicken', 'Beer, Beer, Cake, Chocolate']
eachitems = ['Beer', 'Cake', 'Chocolate', 'Lemon with ci', 'Chicken']
for item in eachitems:
assoc = [associated for associated in listitems if item in associated]
result = set()
for itemlist in assoc:
itemlist = itemlist.replace(', ', ',').split(',')
itemlist = set(itemlist)
itemlist.remove(item)
result = result | itemlist
print('People who buy {} also buy: '.format(item), ', '.join(sorted(result)))
输出
People who buy Beer also buy: Cake, Chicken, Chocolate
People who buy Cake also buy: Beer, Chocolate
People who buy Chocolate also buy: Beer, Cake
People who buy Lemon with ci also buy: Chicken
People who buy Chicken also buy: Beer, Lemon with ci
这个解决方案的关键部分是使用Sets删除重复项和|
(联合)运算符。
作为旁注,请不要像这样使用|
result = result | itemlist
您可以使用
修改设置result.update(itemlist)