Assuming you have two list of words, book1WordList and book2WordList
your can define your counter method as
def mycounter(list):
mydict = {}
key_val = 1
for i in list:
if i in mydict:
mydict[i]=mydict[i]+1
else:
mydict[i]=key_val
return mydict
book1WordList = ['jungle','forest','jungle','hi']
book2WordList = ['hi','hello','hello','forest','hi','hello','forest']
book1dict = mycounter(book1WordList)
book2dict = mycounter(book2WordList)
book1keyslist = list(book1dict.keys())
book2keyslist = list(book2dict.keys())
book1set = set(book1keyslist)
commankeys = book1set.intersection(book2keyslist)
for i in commankeys:
print (f"{i} {book1dict[i]} {book2dict[i]}")
Output:
forest 1 2
hi 1 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…