简体   繁体   English

计算列表列表中的项目

[英]Counting items within a list of lists

I can't figure out how to count within a list of lists that was imported from a txt file.我无法弄清楚如何在从 txt 文件导入的列表列表中进行计数。 It is just a list of job details (about 100 jobs).它只是一份工作详细信息列表(大约 100 个工作)。

Each list format looks like this:每个列表格式如下所示:

['Business Analyst', 'Hotwire.com', 'San Francisco CA', 'NA', 'Full Time', 'Bachelors', '3', 'Business Analysis', 'Business Intelligence', 'IT development', 'UML', 'BPMN', 'Applied Analytics', 'Statistics', 'Communication Skills', 'Sales Experience', 'Organized', 'Detail-Oriented']

I want to make a new list of a specific detail to count and graph eventually.我想制作一个特定细节的新列表,以最终计算和绘制图表。

For example, I want to create a list of cities (index[2]).例如,我想创建一个城市列表(索引 [2])。

data = []
with open('Travel Industry Jobs.txt') as f:
    for line in f:
        data.append(line.strip().split(','))

f.close()

for i in data:
    count = 0
    city = data[i].count("San Francisco CA")
    count += city

You can pull out a column easily with list comprehensions您可以使用列表推导式轻松拉出一列

cities = [row[2] for row in data]

You could also use other tools such as collections.Counter with a generator that emits all of the cities.您还可以使用其他工具,例如collections.Counter和一个生成所有城市的生成器。 Here each city gets its own key in a dictionary.在这里,每个城市在字典中都有自己的键。 Any minor spelling deviations get different entries, so beware任何小的拼写偏差都会得到不同的条目,所以要小心

citycount = collections.Counter(row[2] for row in data)
a = [[1,2,3],[2,3,4]]
for t in a:
  print len(t)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM