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

Python计数器-对列表中重复的单词数求和,python,Python,我试图计算每个单词在列表中出现的次数。因此,我尝试使用计数器模块,但不是对单词进行求和,而是对单个字母进行求和 代码段: from collections import Counter wkday = ans.strftime("%A") incoming.append([wkday,time]) mycount = Counter(wkday) print mycount 更多代码: with open('temp_dates.csv') as csvfil

我试图计算每个单词在列表中出现的次数。因此,我尝试使用计数器模块,但不是对单词进行求和,而是对单个字母进行求和

代码段:

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
输入数据(打印工作日)如下所示:

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

任何帮助都将不胜感激

根据
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})

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


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

# 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

为了汇总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) 
这允许我打印日期列表,如下所示:

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

您的代码没有正确缩进,似乎缺少某种循环。这不是完整的代码。您已经走得太远了一级,可能是在
for
循环中。请查看如何创建一个我添加了一个更大的代码块,谢谢。您能提供源文件以便提供完整的答案吗?@Justin根据您的
print(wkday)
wkday
不是日期字符串列表。。因此,不清楚这样做会导致每行单个计数:Counter({'Sunday':1})Counter({'Sunday':1})Counter({'Sunday':1})Counter({'Sunday':1})Counter({'Sunday':1})Counter({'Sunday':1})Counter({'Sunday':1})@Justin从您的代码中不清楚您传递给
计数器的
工作日的类型。只要确保你通过了day string列表,一切都会顺利进行。哈,就是这样!如果我在循环外初始化mycount,然后在循环外打印,它将按预期工作。谢谢