Python计数器 - 对列表中重复单词的数量求和

时间:2017-04-26 17:23:41

标签: python

我正在尝试计算每个单词出现在列表中的次数。因此,我试图使用计数器模块,而不是总结单词,而是总结单个字母。

代码段:

from collections import Counter

    wkday = ans.strftime("%A")
    incoming.append([wkday,time])
    mycount = Counter(wkday)
    print mycount

更多代码:

with open('temp_dates.csv') as csvfile2:
readCSV2 = csv.reader(csvfile2, delimiter=',')
incoming = []
for row in readCSV2:
     readin = row[0]
     time = row[1]
     year, month, day = (int(x) for x in readin.split('-'))
     ans = datetime.date(year, month, day)
     wkday = ans.strftime("%A")
     incoming.append([wkday,time])
     mycount = Counter(wkday)
     print mycount
     print wkday
with open('new_dates2.csv', 'w') as out_file:
  writer = csv.writer(out_file)
  writer.writerows(incoming)

如果我使用计数器,我会列出这是我得到的:

Counter({'e': 2, 'd': 2, 'a': 1, 'n': 1, 's': 1, 'W': 1, 'y': 1})
Counter({'a': 1, 'd': 1, 'M': 1, 'o': 1, 'n': 1, 'y': 1})
Counter({'a': 1, 'e': 1, 'd': 1, 's': 1, 'u': 1, 'T': 1, 'y': 1})
Counter({'a': 1, 'e': 1, 'd': 1, 's': 1, 'u': 1, 'T': 1, 'y': 1})
Counter({'a': 1, 'd': 1, 'M': 1, 'o': 1, 'n': 1, 'y': 1})
Counter({'a': 1, 'd': 1, 'M': 1, 'o': 1, 'n': 1, 'y': 1})
Counter({'e': 2, 'd': 2, 'a': 1, 'n': 1, 's': 1, 'W': 1, 'y': 1})

我需要的是:

Monday: 3
Tuesday: 16
Wednesday: 6
...etc

输入数据(print wkday)如下所示:

Tuesday
Monday
Monday
Sunday
Saturday
Saturday
Thursday
Wednesday
Sunday
Sunday
Wednesday
Tuesday
Thursday
Wednesday

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

根据print(wkday)的结果,您的wkday可能只是一个字符串,其中的天数由"\n"分隔。在传递给Count

之前将其拆分为单词
wkday = wkday.splitlines()

>>> print(wkday)

['Tuesday', 'Monday', 'Monday', 'Sunday', 'Saturday', 'Saturday', 'Thursday', 'Wednesday', 'Sunday', 'Sunday', 'Wednesday', 'Tuesday', 'Thursday', 'Wednesday']

>>> mycount = Counter(wkday)
>>> print(mycount)

Counter({'Sunday': 3, 'Wednesday': 3, 'Tuesday': 2, 'Monday': 2, 'Saturday': 2, 'Thursday': 2})

这证实了我对wkday类型的假设,并重现了你所拥有的

>>> print(Counter("\n".join(wkday)))

Counter({'d': 17, 'a': 16, 'y': 14, '\n': 13, 'u': 9, 'e': 8, 'n': 8, 's': 7, 'S': 5, 'T': 4, 'r': 4, 'W': 3, 'M': 2, 'o': 2, 't': 2, 'h': 2})

答案 1 :(得分:0)

您需要将所有输入数据提供给Counter(),例如

input_data = ["Tuesday", "Wednesday", "Tuesday"]
day_count = Counter(input_data)
print(day_count)

答案 2 :(得分:0)

您需要在mycount循环之外初始化for并为每个工作日增加值:

# Initialize mycount
mycount = Counter()
with open('temp_dates.csv') as csvfile2:
    readCSV2 = csv.reader(csvfile2, delimiter=',')
    incoming = []
    for row in readCSV2:
        readin = row[0]
        time = row[1]
        year, month, day = (int(x) for x in readin.split('-'))
        ans = datetime.date(year, month, day)
        wkday = ans.strftime("%A")
        incoming.append([wkday,time])
        # Increment count for weekday
        mycount[wkday] += 1

答案 3 :(得分:0)

只是为了汇总David Cullen提供的解决方案以及格式化所需的另一段代码,这里是最终版本:

mycount = Counter()
with open('temp_dates.csv') as csvfile2:
  readCSV2 = csv.reader(csvfile2, delimiter=',')
  incoming = []
    for row in readCSV2:
         readin = row[0]
         time = row[1]
         year, month, day = (int(x) for x in readin.split('-'))
         ans = datetime.date(year, month, day)
         wkday = ans.strftime("%A")
         incoming.append([wkday,time])
         mycount[wkday] += 1
with open('new_dates2.csv', 'w') as out_file:
    writer = csv.writer(out_file)
    writer.writerows(incoming)
csvfile2.close()

for key,value in sorted(mycount.iteritems()):
    daylist = key, value
    print(daylist) 

这允许我打印如下所示的日期列表:

('Friday', 13)
('Monday', 11)
('Saturday', 7)
('Sunday', 9)
('Thursday', 12)
('Tuesday', 13)
('Wednesday', 12)