女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

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

    文章

    8490

    瀏覽量

    134058
  • python
    +關注

    關注

    56

    文章

    4823

    瀏覽量

    86146
  • 深度學習
    +關注

    關注

    73

    文章

    5554

    瀏覽量

    122466
收藏 人收藏

    評論

    相關推薦
    熱點推薦

    數據可視化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

    三維可視化的應用和優勢

    ,為此三維可視化運維系統登場了。  三維可視化的應用  宏觀場景可視化:在特定的環境對隨著時間推移而不斷變化的目標實體進行檢測,可以直觀、靈活、逼真的展示所處區域的情景和環境,可以快
    發表于 12-02 11:52

    函數可視化與Matlab作

    函數可視化與Matlab作2.1 實驗與觀察:函數可視化2.1.1 Matlab二維繪圖命令1.周期函數與線性p-周期
    發表于 10-17 00:30 ?2378次閱讀
    <b class='flag-5'>函數</b>的<b class='flag-5'>可視化</b>與Matlab作

    可視化技術有哪些

    完整的地理空間信息可視化概念主要包括科學計算可視化、數據可視化和信息可視化可視化技術作為解釋
    發表于 02-05 09:09 ?3873次閱讀

    Python拉勾網數據采集與可視化

    本文是先采集拉勾網上面的數據,采集的是Python崗位的數據,然后用Python進行可視化。主要涉及的是爬蟲&數據可視化的知識。
    的頭像 發表于 03-13 14:18 ?3445次閱讀
    <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 ?9256次閱讀

    Python數據可視化編程實戰

    Python數據可視化編程實戰資料免費下載。
    發表于 06-01 14:37 ?29次下載

    使用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 ?1807次閱讀
    使用<b class='flag-5'>Python</b>來收集、處理和<b class='flag-5'>可視化</b>人口數據

    Python 可視化如何配色

    我們在利用Python進行數據可視化時,有著大量的高質量庫可以用,比如: Matplotlib 、 seaborn 、 Plotly 、 Bokeh 、 ggplot 等等。但圖表好不好看,配色占
    的頭像 發表于 10-30 15:43 ?690次閱讀
    <b class='flag-5'>Python</b> <b class='flag-5'>可視化</b>如何配色