title=['A','B','C','D','E','F','G','H','I','G']
genres=['喜剧|动作|爱情','喜剧|动作','搞笑','喜剧|动作|精彩',
'言情|动作|连续剧','悬疑|爱情','喜剧|警匪|爱情','战争','古代|动作|爱情','科幻|幼稚']
newgenres=[]
for i in genres:
data=i.split('|')
newgenres.append(data)
datas=list(zip(title,newgenres))
genreset=set([m for n in genres for m in n.split('|')])
genresdict={}
for j in genreset:
data=[]
for k in datas:
if j in k[1]:
data.append(k[0])
genresdict[j]=data
for l in genresdict.items():
print(l)
print('-----------------')
#频率统计
genresdict2={}
for j in genreset:
for k in datas:
if j in k[1]:
genresdict2[j]=genresdict2.get(j,0)+1
for l in genresdict2.items():
print(l)
('言情', ['E'])
('战争', ['H'])
('连续剧', ['E'])
('精彩', ['D'])
('幼稚', ['G'])
('喜剧', ['A', 'B', 'D', 'G'])
('悬疑', ['F'])
('古代', ['I'])
('搞笑', ['C'])
('动作', ['A', 'B', 'D', 'E', 'I'])
('爱情', ['A', 'F', 'G', 'I'])
('科幻', ['G'])
('警匪', ['G'])
-----------------
('言情', 1)
('战争', 1)
('连续剧', 1)
('精彩', 1)
('幼稚', 1)
('喜剧', 4)
('悬疑', 1)
('古代', 1)
('搞笑', 1)
('动作', 5)
('爱情', 4)
('科幻', 1)
('警匪', 1)