国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python Data Structures: Dictionary, Tuples

這篇具有很好參考價(jià)值的文章主要介紹了Python Data Structures: Dictionary, Tuples。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Chapter9 Dictionary

1. list and dictionary

(1)順序
List: 有序-linear collection of values that stay in order. (一盒薯片)
Dictionary: 無(wú)序-’bag’ of values, each with its own label. (key + value)

lst=list()
lst.append(21)
lst.append(183)
print(lst)

[21, 183]

ddd=dict()
ddd['age']=21
ddd['course']=183
print(ddd)

{‘a(chǎn)ge’: 21, ‘course’: 183}

collections.OrderedDict 可以有序
Python 3.7才保證了順序。

(2)Dictionary別名
Associative arrays
Associative arrays, also known as maps, dictionaries, or hash maps in various programming languages, refer to a data structure that associates keys with values. In Python, this concept is implemented through dictionaries, where each key-value pair allows efficient lookup and retrieval of values based on unique keys.

2. 修改值:

修改position

lst=list()
lst.append(21)
lst.append(183)
lst[0]=23
print(lst)

修改label

ddd=dict()
ddd['age']=21
ddd['course']=183
ddd['age']=23
print(ddd)

3. 計(jì)算名字出現(xiàn)次數(shù)

counts=dict()
names=['csev','cwen','csev','zqian','cwen']
for name in names:
    if name not in counts:
        counts[name]=1
    else:
        counts[name]+=1
print(counts)

4. get()

if a key is already in a dictionary and assuming a default value if the key is not there
找不到就返回第二個(gè)參數(shù),To provide a default value if the key is not found

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']

for name in names:
    counts[name] = counts.get(name, 0) + 1

print(counts)

這里 counts.get(name, 0) 返回 name 對(duì)應(yīng)的值,如果 name 不在字典中,返回默認(rèn)值 0。然后將這個(gè)值加 1,最后將結(jié)果存儲(chǔ)回字典中。這樣就能得到每個(gè)名字出現(xiàn)的次數(shù)。

5. Dictionary and Files

先把句子split成words,然后再count。

counts=dict()
line=input('Please enter a line of text:')
words=line.split()
print(f'Words: {words}')

for word in words:
    counts[word]=counts.get(word,0)+1
print(f'Counts:{counts}')

6. Retrieving lists of keys and values

The first is the key, and the second variable is the value.

jjj={'chunck':1,'fred':42,'jason':100}
print(list(jjj))
print(jjj.keys())
print(jjj.values())
print(jjj.items())

[‘chunck’, ‘fred’, ‘jason’]
dict_keys([‘chunck’, ‘fred’, ‘jason’])
dict_values([1, 42, 100])
dict_items([(‘chunck’, 1), (‘fred’, 42), (‘jason’, 100)])

7.items():產(chǎn)生tuples

items() 是 Python 字典(dictionary)對(duì)象的方法,用于返回一個(gè)包含字典所有鍵值對(duì)的視圖對(duì)象。這個(gè)視圖對(duì)象可以用于迭代字典中的所有鍵值對(duì)

8.計(jì)算文件中的名字次數(shù)最大值

name=input(‘Please enter a line of text:’)
handle=open(name)

#全部統(tǒng)計(jì)
counts=dict()
for line in handle:
    words=line.split()
    for word in words:
        counts[word]=counts.get(word,0)+1

#計(jì)算最大
bigcount = None
bigword = None
for word, count in counts.items():
    if bigcount is None or count > bigcount:
        bigword = word
        bigcount = count
print(bigword,bigcount)

Chapter10 Tuples

1. Tuples Are Like Lists

-Tuples are another kind of sequence that functions much like a list
-they have elements which are indexed starting at 0

2. Tuples are immutable. (Same as strings)

#list
x=[9,8,7]
x[2]=6
print(x)

R: [9, 8, 6]

#String
y='ABC'
y[2]='D'
print(y)

TypeError: ‘str’ object does not support item assignment

#Tuples
z=(5,4,3)
z[2]=0
print(z)

TypeError: ‘tuple’ object does not support item assignment

3. 方法

(1)not to do (所有和change相關(guān)的)
.sort()
.append()
.reverse()

(2)其中,sort和sorted()不同:
列表用方法sort() 直接修改,不能用于元組。
函數(shù)sorted() 返回一個(gè)新的已排序的列表,不會(huì)修改原始可迭代對(duì)象??捎糜诹斜?、元組、字符串等。

for k,v in sorted(d.item()):
sorted in key order

對(duì)value進(jìn)行排序:

c={'a': 10, 'b': 1, 'c': 22}
tmp = list ()

# 將字典 c 的key,value調(diào)換,并轉(zhuǎn)換為元組.
for k, v in c.items():
    tmp.append((v, k))
print(tmp)

#從大到小排序value
tmp = sorted(tmp,reverse=True)
print(tmp)

[(10, ‘a(chǎn)’), (1, ‘b’), (22, ‘c’)]
[(22, ‘c’), (10, ‘a(chǎn)’), (1, ‘b’)]

(v,k):元組的第一個(gè)元素是值(value),第二個(gè)元素是鍵(key)。
使用 sorted() 函數(shù)對(duì)列表 tmp 進(jìn)行排序,參數(shù) reverse=True 表示降序排序(從大到?。?。排序后的結(jié)果將存儲(chǔ)在列表 tmp 中。

簡(jiǎn)化:

c={'a': 10, 'b': 1, 'c': 22}
print(sorted([(v,k)for k,v in c.items()]))

(3)通用:index() 查找指定索引。
(4)可用:items():returns a list of (key, value) tuples.

4. more efficient

· memory use and performance than lists
· making “temporary variables”
· For a temporary variable that you will use and discard without modifying
(sort in place原地排序,指在排序過(guò)程中不創(chuàng)建新的對(duì)象,而是直接在現(xiàn)有的數(shù)據(jù)結(jié)構(gòu)中進(jìn)行排序。用于列表)
· We can also put a tuple on the left-hand side of an assignment statement

(a,b)=(99,98)
print(a,b)

6.Comparable:

按順序比較,第一個(gè)數(shù)字一樣,第二個(gè)數(shù)字3更大。
print((0,2,200)<(0,3,4))
True
按字母前后。字母越靠前越小。
print(‘Amy’>‘Abby’)
True文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-793388.html

到了這里,關(guān)于Python Data Structures: Dictionary, Tuples的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Python】成功解決ValueError: dictionary update sequence element #0 has length 1; 2 is required】

    【Python】成功解決ValueError: dictionary update sequence element #0 has length 1; 2 is required】

    【Python】成功解決ValueError: dictionary update sequence element #0 has length 1; 2 is required】 ?? 個(gè)人主頁(yè):高斯小哥 ?? 高質(zhì)量專欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分9

    2024年03月10日
    瀏覽(29)
  • 如何在 Python 中讀取 .data 文件?

    創(chuàng)建.data文件是為了存儲(chǔ)信息/數(shù)據(jù)。 此格式的數(shù)據(jù)通常以逗號(hào)分隔值格式或制表符分隔值格式放置。 除此之外,該文件可以是二進(jìn)制或文本文件格式。在這種情況下,我們將不得不找到另一種訪問(wèn)它的方式。 在本教程中,我們將使用.csv文件,但首先,我們必須確定文件的內(nèi)

    2024年02月07日
    瀏覽(17)
  • 使用Python編程語(yǔ)言處理數(shù)據(jù) (Processing data using Python programm

    作者:禪與計(jì)算機(jī)程序設(shè)計(jì)藝術(shù) Python作為一種高級(jí)、開源、跨平臺(tái)的編程語(yǔ)言,已經(jīng)成為當(dāng)今最流行的數(shù)據(jù)分析和機(jī)器學(xué)習(xí)工具。本文介紹了使用Python編程語(yǔ)言處理數(shù)據(jù)的一些基礎(chǔ)知識(shí),如列表、字典、集合、迭代器等,并對(duì)pandas、numpy、matplotlib、seaborn等數(shù)據(jù)分析庫(kù)進(jìn)行了

    2024年02月07日
    瀏覽(27)
  • python-用form-data形式上傳文件請(qǐng)求

    python-用form-data形式上傳文件請(qǐng)求

    雖然現(xiàn)在基本上都約定俗成的接口都用json形式請(qǐng)求 但是不可避免地 有些接口需要傳文件流,此時(shí)就需要用form-data形式上傳了 for.e: 存在以下接口,通過(guò)接口創(chuàng)建海報(bào)圖 但需要上傳縮略圖, 此時(shí)接口的Content-Type就不能是application/json,而是multipart/form-data; 參數(shù)格式也是以表單

    2023年04月08日
    瀏覽(17)
  • python + request實(shí)現(xiàn)multipart/form-data請(qǐng)求上傳文件

    1、multipart/form-data介紹 ????????multipart/form-data 是 HTTP 協(xié)議中用于上傳文件的一種類型。它允許客戶端向服務(wù)器發(fā)送文件以及一些額外的元數(shù)據(jù)(例如文件名、MIME 類型、圖片等)。這種類型的請(qǐng)求不同于普通的application/x-www-form-urlencoded 格式,其中數(shù)據(jù)是在請(qǐng)求體中進(jìn)行編

    2024年02月11日
    瀏覽(22)
  • Python Packages for Big Data Analysis and Visualization

    作者:禪與計(jì)算機(jī)程序設(shè)計(jì)藝術(shù) Python第三方庫(kù)主要分為兩類:數(shù)據(jù)處理、可視化。下面是用于大數(shù)據(jù)分析與可視化的常用的Python第三方庫(kù)列表(按推薦順序排序): NumPy: NumPy 是用 Python 編寫的一個(gè)科學(xué)計(jì)算庫(kù),其功能強(qiáng)大且全面,尤其適用于對(duì)大型多維數(shù)組和矩陣進(jìn)行快速

    2024年02月07日
    瀏覽(21)
  • requests 庫(kù):發(fā)送 form-data 格式的 http 請(qǐng)求 (python)

    requests官方網(wǎng)站地址 requests_toolbelt Python自動(dòng)化 requests 庫(kù):發(fā)送 form-data 格式的 http 請(qǐng)求 requests-toolbelt · PyPI

    2024年02月03日
    瀏覽(22)
  • 相關(guān)性分析——Pearson相關(guān)系數(shù)+熱力圖(附data和Python完整代碼)

    相關(guān)性分析——Pearson相關(guān)系數(shù)+熱力圖(附data和Python完整代碼)

    相關(guān)性分析:指對(duì)兩個(gè)或多個(gè)具有相關(guān)性的變量元素進(jìn)行分析 相關(guān)系數(shù)最早是由統(tǒng)計(jì)學(xué)家卡爾 皮爾遜設(shè)計(jì)的統(tǒng)計(jì)指標(biāo),是研究變量之間線性相關(guān)承兌的值,一般用字母 r 表示。 Pearson相關(guān)系數(shù)是衡量?jī)蓚€(gè)數(shù)據(jù)集合是否在一條線上面,用于衡量變量間的線性關(guān)系。 這里是引用

    2024年02月05日
    瀏覽(26)
  • Python報(bào)錯(cuò):TypeError: Cannot interpret ‘1‘ as a data type

    在使用np.zeros()創(chuàng)建新數(shù)組的時(shí)候,我傳入的參數(shù)是如下代碼: 運(yùn)行報(bào)錯(cuò): 報(bào)錯(cuò)原因是我們給zeros()函數(shù)傳入的參數(shù)發(fā)生問(wèn)題: 我傳入的參數(shù)是(layers_dims[l],1),這是不對(duì)的,因?yàn)閦eros只需要傳入一個(gè)參數(shù),就是shape。所以我們應(yīng)該更改為: 把(layers_dims[l],1)用括號(hào)括起來(lái)

    2024年02月13日
    瀏覽(18)
  • 6.Best Practices for Handling Big Data with Python in

    作者:禪與計(jì)算機(jī)程序設(shè)計(jì)藝術(shù) 大數(shù)據(jù)處理是企業(yè)中最常用的一種數(shù)據(jù)分析方法。Amazon Web Services (AWS) 提供了很多工具幫助用戶進(jìn)行大數(shù)據(jù)的存儲(chǔ)、處理、分析等工作。下面,我將分享一些在 AWS 上處理大數(shù)據(jù)的方法和技巧。希望能給讀者帶來(lái)幫助。 本文適合具有一定Python編

    2024年02月07日
    瀏覽(23)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包