从collections库的Counter类看items()方法和enumerate()方法

下面的代码是针对文件的词频统计,使用了collections库及其Counter类
import collections
def count_word_frequency(text):
    words = text.lower().split()
    word_counts = collections.Counter(words)
    return word_counts
def count_fileword_frequency(file_path):
    with open(file_path,'r') as file:
        text = file.read()
    words = text.lower().split()
    word_counts = collections.Counter(words)
    return word_counts
一个参数是字符串,一个参数是文件路径,一共定义了两个方法

对变量赋值
text = "This is a sample text.This text is used to domonstrate word frequency counting."
file_path = './subtitle.txt'

输出返回值类型
word_frequency = count_word_frequency(text)
print(type(word_frequency))
word_frequency = count_fileword_frequency('./subtitle.txt')
print(type(word_frequency))

输出结果为


类型是一个类

如何输出内容,分别用 items() 和 enumerate() 实现
使用 items()
for key,value in word_frequency.items():
    print(key,value)
结果为
make 2
sure 1
you 36
know 9
what 11
you're 7
supposed 1
使用enumerate()
for key,value in enumerate(word_frequency):
    print(key,value,word_frequency[value])
结果为
0 make 2
1 sure 1
2 you 36
3 know 9
4 what 11
5 you're 7
6 supposed 1

如果要求输出前三项,使items()时,需要先转换成list
for key,value in list(word_frequency.items())[:3]:
    print(key,value)
    
输出为
make 2
sure 1
you 36
使用enumerate()时,代码如下
for key,value in enumerate(word_frequency):
    if key in [0,1,2]:
        print(value,word_frequency[value])
输出同样为  
make 2
sure 1
you 36      

从代码中,可以items() 和 enumerate() 都可用可迭代对象的遍历,items()返回元素本身,enumerate()返回迭代对象,有索引和元素组成
    
 

你可能感兴趣的:(编程,items,enumerate,python)