首页 > 解决方案 > 通过分隔获取列表中每个单词的计数

问题描述

我有以下列表,并想获取每个单词的计数

t_series=['Chinese, Italian, Fast Food',
     'North Indian, Chinese, South Indian, Fast Food, Biryani, Street Food, Beverages',
     'South Indian, North Indian, Chinese, Biryani, Street Food, Sandwich, Beverages',
     'Bakery, Fast Food',
     'Fast Food, Italian, Chinese, Rolls, Sandwich',
     'Mithai, Street Food, Fast Food, Beverages',
     'South Indian, Chinese, Street Food, Fast Food, Desserts, Beverages',
     'North Indian, Chinese, South Indian, Fast Food, Desserts, Beverages',
     'Fast Food, Italian, Chinese, Rolls, Sandwich',
     'North Indian, South Indian',]

将字符串拆分为:-

list_sep = [st for row in t_series for st in row]

我得到的输出为: -

['C','h','i','n','e','s','e',',','','I','t','a','l ', 'i', 'a', 'n', ',',] 等等。

我希望输出为:- [Chinese, Italian, Fast Food, South Indian] 等等。

标签: python-3.xpandasdata-science

解决方案


使用者:split_,

list_sep = [st for row in t_series for st in row.split(', ')]
print (list_sep)
['Chinese', 'Italian', 'Fast Food', 'North Indian', 'Chinese', 'South Indian', 
 'Fast Food', 'Biryani', 'Street Food', 'Beverages', 'South Indian', 'North Indian',
 'Chinese', 'Biryani', 'Street Food', 'Sandwich', 'Beverages', 'Bakery', 'Fast Food', 
 'Fast Food', 'Italian', 'Chinese', 'Rolls', 'Sandwich', 'Mithai', 'Street Food', 
 'Fast Food', 'Beverages', 'South Indian', 'Chinese', 'Street Food', 'Fast Food',
 'Desserts', 'Beverages', 'North Indian', 'Chinese', 'South Indian', 'Fast Food', 
 'Desserts', 'Beverages', 'Fast Food', 'Italian', 'Chinese', 'Rolls', 'Sandwich', 
 'North Indian', 'South Indian']

推荐阅读