0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Python中enumerate函数的解释及可视化

电子设计 来源:电子设计 作者:电子设计 2020-12-10 19:40 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

enumerate() 的作用

在许多情况下,我们需要在迭代数据对性(即我们可以循环的任何对象)时获取元素的索引。实现预期结果的一种方法是:

animals = ['dog', 'cat', 'mouse']
for i in range(len(animals)):
    print(i, animals[i])

输出:

0 dog
1 cat
2 mouse

大多数C ++ / Java背景的开发人员都可能会选择上述实现,通过索引迭代数据对象的长度是他们熟悉的概念。但是,这种方法效率很低。
我们可以使用enumerate()来实现:

for i, j in enumerate(example):
    print(i, j)

enumerate()提供了强大的功能,例如,当您需要获取索引列表时,它会派上用场:

(0, seq[0]), (1, seq[1]), (2, seq[2]), ...

案例研究1:枚举字符串

字符串只是一个列表

为了更好地理解字符串枚举,我们可以将给定的字符串想象为单个字符(项)的集合。因此,枚举字符串将为我们提供:

1.字符的索引。
2.字符的值。

word = "Speed"
for index, char in enumerate(word):
    print(f"The index is '{index}' and the character value is '{char}'")

输出:

The index is '0' and the character value is 'S'
The index is '1' and the character value is 'p'
The index is '2' and the character value is 'e'
The index is '3' and the character value is 'e'
The index is '4' and the character value is 'd'

案例研究2:列举列表

那么,我们应该如何列举一个列表呢?为了做到这一点,我们可以利用for循环并遍历每个项目的索引和值:

sports = ['soccer', 'basketball', 't`  ennis']
for index, value in enumerate(sports):
    print(f"The item's index is {index} and its value is '{value}'")

输出:

The item's index is 0 and its value is 'soccer'
The item's index is 1 and its value is 'basketball'
The item's index is 2 and its value is 'tennis'

案例研究3:自定义起始索引

我们可以看到枚举从索引0开始,但是们经常需要更改起始位置,以实现更多的可定制性。值得庆幸的是,enumerate()还带有一个可选参数[start]

enumerate(iterable, start=0)

可以用来指示索引的起始位置,方法如下:

students = ['John', 'Jane', 'J-Bot 137']
for index, item in enumerate(students, start=1):
    print(f"The index is {index} and the list element is '{item}'")

输出

The index is 1 and the list element is 'John'
The index is 2 and the list element is 'Jane'
The index is 3 and the list element is 'J-Bot 137'

现在,修改上述代码:1.起始索引可以为负;2.省略start=则默认从0索引位置开始。

teachers = ['Mary', 'Mark', 'Merlin']
for index, item in enumerate(teachers, -5):
    print(f"The index is {index} and the list element is '{item}'")

输出将是:

The index is -5 and the list element is 'Mary'
The index is -4 and the list element is 'Mark'
The index is -3 and the list element is 'Merlin'

案例研究4:枚举元组

使用枚举元组遵循与枚举列表相同的逻辑:

colors = ('red', 'green', 'blue')
for index, value in enumerate(colors):
    print(f"The item's index is {index} and its value is '{value}'")

输出:

The item's index is 0 and its value is 'red'
The item's index is 1 and its value is 'green'
The item's index is 2 and its value is 'blue'

案例研究5:枚举列表中的元组

让我们提高一个档次,将多个元组合并到一个列表中……我们要枚举此元组列表。一种做法的代码如下:

letters = [('a', 'A'), ('b', 'B'), ('c', 'C')]
for index, value in enumerate(letters):
    lowercase = value[0]
    uppercase = value[1]
    print(f"Index '{index}' refers to the letters '{lowercase}' and '{uppercase}'")

但是,元组拆包被证明是一种更有效的方法。比如:

letters = [('a', 'A'), ('b', 'B'), ('c', 'C')]
for i, (lowercase, uppercase) in enumerate(letters):
    print(f"Index '{i}' refers to the letters '{lowercase}' and '{uppercase}'")

输出:

Index '0' refers to the letters 'a' and 'A'
Index '1' refers to the letters 'b' and 'B'
Index '2' refers to the letters 'c' and 'C'

案例研究6:枚举字典

枚举字典似乎类似于枚举字符串或列表,但事实并非如此,主要区别在于它们的顺序结构,即特定数据结构中元素的排序方式。

字典有些随意,因为它们的项的顺序是不可预测的。如果我们创建字典并打印它,我们将得到一种结果:

translation = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
print(translation)
# Output on our computer: {'one': 'uno', 'two': 'dos', 'three': 'tres'}

但是,如果打印此词典,则顺序可能会有所不同!

由于索引无法访问字典项,因此我们必须利用for循环来迭代字典的键和值。该key — value对称为item,因此我们可以使用.items()方法:

animals = {'cat': 3, 'dog': 6, 'bird': 9}
for key, value in animals.items():
    print(key, value)

输出将是:

cat 3
dog 6
bird 9

本文由博客一文多发平台 OpenWrite 发布!

审核编辑 黄昊宇
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 机器学习
    +关注

    关注

    66

    文章

    8541

    浏览量

    136236
  • python
    +关注

    关注

    57

    文章

    4858

    浏览量

    89587
  • 深度学习
    +关注

    关注

    73

    文章

    5590

    浏览量

    123909
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    数据可视化Python-matplotlib概述

    数据可视化(二):Python-matplotlib
    发表于 07-22 14:58

    如何把AD中非可视化区域物件移到可视化区域?

    AD中非可视化区域物件怎么移到可视化区域???
    发表于 09-10 05:36

    python数据可视化的方法和代码

    Python数据可视化汇总
    发表于 10-14 14:59

    Python数据可视化专家的七个秘密

    分享 Python数据可视化专家的七个秘密
    发表于 05-15 06:43

    python数据可视化之画折线图

    python数据可视化之画折线图,散点图
    发表于 05-27 08:09

    Python数据可视化

    Python数据可视化:网易云音乐歌单
    发表于 07-19 08:30

    函数可视化与Matlab作

    函数可视化与Matlab作2.1 实验与观察:函数可视化2.1.1 Matlab二维绘图命令1.周期函数与线性p-周期
    发表于 10-17 00:30 2515次阅读
    <b class='flag-5'>函数</b>的<b class='flag-5'>可视化</b>与Matlab作

    可视化技术有哪些

    完整的地理空间信息可视化概念主要包括科学计算可视化、数据可视化和信息可视化可视化技术作为解释
    发表于 02-05 09:09 4080次阅读

    Python拉勾网数据采集与可视化

    本文是先采集拉勾网上面的数据,采集的是Python岗位的数据,然后用Python进行可视化。主要涉及的是爬虫&数据可视化的知识。
    的头像 发表于 03-13 14:18 3675次阅读
    <b class='flag-5'>Python</b>拉勾网数据采集与<b class='flag-5'>可视化</b>

    使用Python可视化数据,机器人开发编程

    机器学习开发,与Mail.Ru Search数据分析负责人Egor Polusmak和Mail.Ru Group数据科学家Yury Kashnitsky一起探索如何使用Python可视化数据。在机器学习领域中,可视化并不仅仅用来
    的头像 发表于 03-15 16:56 9816次阅读

    Python数据可视化编程实战

    Python数据可视化编程实战资料免费下载。
    发表于 06-01 14:37 29次下载

    怎样使用Python去进行可视化绘制?

    今天给大家带来绘制“手绘风格”可视化作品的小技巧,主要涉及Python编码绘制,内容如下。
    的头像 发表于 06-23 11:49 2681次阅读
    怎样使用<b class='flag-5'>Python</b>去进行<b class='flag-5'>可视化</b>绘制?

    使用arduino和python可视化你的比特币收益和损失

    电子发烧友网站提供《使用arduino和python可视化你的比特币收益和损失.zip》资料免费下载
    发表于 12-21 16:50 0次下载
    使用arduino和<b class='flag-5'>python</b><b class='flag-5'>可视化</b>你的比特币收益和损失

    使用Python来收集、处理和可视化人口数据

    如何使用Python这一流行的编程语言来收集、处理和可视化印度和中国的人口数据呢?本文将向你介绍一些基本的步骤和技巧,帮助你掌握Python进行可视化分析的方法。我们将使用以下几个库来
    的头像 发表于 06-21 17:08 2260次阅读
    使用<b class='flag-5'>Python</b>来收集、处理和<b class='flag-5'>可视化</b>人口数据

    Python 可视化如何配色

    我们在利用Python进行数据可视化时,有着大量的高质量库可以用,比如: Matplotlib 、 seaborn 、 Plotly 、 Bokeh 、 ggplot 等等。但图表好不好看,配色占
    的头像 发表于 10-30 15:43 1003次阅读
    <b class='flag-5'>Python</b> <b class='flag-5'>可视化</b>如何配色