【test】Enumerate 用法实例

enumerate可以做什么?

1. 如何理解enumerate?

enumerate()是python的内置函数,用相当于给可迭代的对象(iterable object,比如string, list, dict, tuple, set等)增加了一个计数器。可以理解为带计数器的列表,字典,元组等。

2. 对可迭代对象进行索引

list_a = ['this', 'is', 'a', 'test']
list_b= list(enumerate(list_a, start =10))  # 两个参数,第一个参数为可迭代对象;第二个参数为计数器的起始值
list_c= list(enumerate(list_a, start =20))
print (list_b)
print(list_c)

print(type(enumerate(list_a)))  # 测试enumerate()返回数据类型

运行输出结果:

[(10, 'this'), (11, 'is'), (12, 'a'), (13, 'test')]
[(20, 'this'), (21, 'is'), (22, 'a'), (23, 'test')]
<class 'enumerate'>

Python3在线测试工具

用for循环遍历enumerate()中的索引和值,代码如下:

list_a = ['this', 'is', 'a', 'test']

for index, value in enumerate(list_a, start=100):
    print(f'计数器索引号码为:{index}。\t\t 数据值为{value}。')

 运行输出结果如下:

计数器索引号码为:100。         数据值为this。
计数器索引号码为:101。         数据值为is。
计数器索引号码为:102。         数据值为a。
计数器索引号码为:103。         数据值为test。

3. 对读取到的文件行进行索引

更进一步,我们可以读取text文件中的行,并用enumerate()方法对行进行索引。

使用方法 :enumerate(filepath, start)

参数1:filepath = open('example.txt','r'),默认为项目路径,以只读方式打开example.txt文件。example.txt文件内容如下:

this is line one.
this is line two.
this is line three.
this is line four.

参数2:start默认为0,可以自行指定

用for循环遍历enumerate,具体代码如下:

for index, line in enumerate(open('example.txt', 'r'), start=100):
    print (f'The index of this line is {index} :', line)

 运行结果如下:

The index of this line is 100 : this is line one.

The index of this line is 101 : this is line two.

The index of this line is 102 : this is line three.

The index of this line is 103 : this is line four.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的KWIC面向对象实例,用Python语言实现: ```python class Text: def __init__(self, content): self.content = content self.processed = None def process(self): # 将文本转换为小写,并将每行文本保存为一个元组 lines = self.content.lower().split('\n') lines = [tuple(line.split()) for line in lines] self.processed = lines class Keyword: def __init__(self, word, line_num, word_num): self.word = word self.line_num = line_num self.word_num = word_num class KWIC: def __init__(self, text): self.text = text self.keywords = [] def generate_keywords(self): # 遍历每行文本,将关键字及其位置信息保存到keywords列表中 for line_num, line in enumerate(self.text.processed): for word_num, word in enumerate(line): keyword = Keyword(word, line_num, word_num) self.keywords.append(keyword) def sort_keywords(self): # 对keywords列表按照关键字进行排序 self.keywords.sort(key=lambda x: x.word) def print_results(self): # 输出结果 for keyword in self.keywords: line = self.text.processed[keyword.line_num] before = line[:keyword.word_num] after = line[keyword.word_num+1:] print(keyword.word, ' '.join(before), '[', keyword.word, ']', ' '.join(after)) ``` 这个示例中,`Text`类表示文本对象,包含原始文本和处理后的文本;`Keyword`类表示关键字对象,包含关键字、所在行数和单词位置;`KWIC`类表示KWIC系统,包含一个文本对象和一个关键字列表,可以生成关键字列表并进行排序,最后输出结果。 使用该示例的方法如下: ```python # 创建文本对象 content = 'This is a test.\nHello world.\nPython is awesome.' text = Text(content) # 处理文本 text.process() # 创建KWIC对象 kwic = KWIC(text) # 生成关键字列表并排序 kwic.generate_keywords() kwic.sort_keywords() # 输出结果 kwic.print_results() ``` 该示例输出的结果为: ``` a this is [a] test. awesome python is [awesome]. hello world. is this is a test. is awesome python [is]. python is [awesome]. test. this is a [test.] this is [a] test. world. hello [world.] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值