Python列表操作,一个条目中有多个单词



我有一个不同电影类型的列表:

['Action, Adventure, Thriller', 'Comedy, Drama, Romance', 'Adventure, Drama, Romance', 'Crime, Drama', 'Drama, Thriller, War', 'Animation, Adventure, Comedy']

第一个列表条目"动作、冒险、惊悚">由第一部电影的类型组成,第二个列表条目《喜剧、戏剧、浪漫》包括第二部电影的流派等。

我想要以下输出,以了解每个流派在列表中出现的频率:

['Action', 'Adventure', 'Thriller', 'Comedy', 'Drama', 'Romance', 'Adventure', 'Drama', 'Romance', 'Crime', 'Drama', 'Drama', 'Thriller', 'War', 'Animation', 'Adventure', 'Comedy']

如果每个流派都被单引号包围并用逗号分隔,我该如何实现这个列表

假设您的列表名为your_list

[word.strip() for words in your_list for word in words.split(',')]
import itertools

a = ['Action, Adventure, Thriller', 'Comedy, Drama, Romance', 'Adventure, Drama, Romance', 'Crime, Drama', 'Drama, Thriller, War', 'Animation, Adventure, Comedy']
b =[x.split(",") for x in a]
c = list(itertools.chain.from_iterable(b))
# or if you can use 
d = [item for sublist in b for item in sublist]

最新更新