日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python中元组和列表转化_4.Python列表/元组/集合/字典

發(fā)布時(shí)間:2023/12/3 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中元组和列表转化_4.Python列表/元组/集合/字典 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

4.1 Python列表

? 列表用 [ ] 標(biāo)識,是Python 最通用的復(fù)合數(shù)據(jù)類型。
? 列表用 [ ] 表示,列表具有可嵌套性

4.1.1 Python列表截取

? 列表可以使用 [頭下標(biāo):尾下標(biāo)] 截取相應(yīng)的子列表, 從左到右索引默認(rèn) 0 開始,從右到左索引默認(rèn)-1開始

4.1.2 Python列表截取與更新

? 列表操作(索引)

4.1.3 Python更新列表

? 可以對列表的數(shù)據(jù)項(xiàng)進(jìn)行修改或更新,也可以使用append()方法來添加列表項(xiàng)。

4.1.4 Python刪除列表元素

? 可以使用 del 語句來刪除列表的的元素。

4.1.5 Python列表操作符

4.1.6 Python列表函數(shù)

4.1.7 Python列表方法

4.1.8 Python列表切片

? 列表操作(切片slicing ,用索引截取某一段列表)

? 加號+是列表連接運(yùn)算符,星號*是重復(fù)操作。例如:

4.2 Python元組

? 元組是一種復(fù)合數(shù)據(jù)類型,類似于List(列表),元組用()”標(biāo)識,內(nèi)部元素用逗號隔開。但是元組不能二次賦值,相當(dāng)于只讀列表。

? Tuples(元組、序列)

? Tuples操作

4.2.1 Python元組截取

4.2.2 Python元組不允許二次賦值

? Python元組不允許更新,而列表是允許更新的。以下對元組的操作是無效的。

4.2.3 元組內(nèi)置函數(shù)

4.3 Python集合(Set)

? 集合(set)是由一個(gè)或數(shù)個(gè)形態(tài)各異的大小整體組成的
? 可以使用大括號 { } 或者 set() 函數(shù)創(chuàng)建集合,注意:創(chuàng)建一個(gè)空集合必須用 set() 而不是{ },因?yàn)?{ } 是用來創(chuàng)建一個(gè)空字典。
? 創(chuàng)建集合格式:

? 或者

4.4 Python字典

? 字典是一種映射類型,字典用“{}”標(biāo)識,它是一個(gè)無序的鍵(key) : 值(value)對的集合。鍵(key)必須使用不可變類型。在同一個(gè)字典中,鍵(key)必須是唯一的。
? 字典是無序的對象集合。
? Dictionaries (Key-Value型Map)

4.5 Python序列

? 序列(sequence),在Python中是一種具有相同特性的高級數(shù)據(jù)結(jié)構(gòu)的統(tǒng)稱
? 序列的通用操作:
① 通過索引來獲取元素值
② 分片操作
③ 通過+合并元素
④ 通過*來復(fù)制元素
⑤ 支持成員運(yùn)算符
⑥ 最大值、最小值和長度函數(shù)支持

4.6 Python數(shù)據(jù)類型轉(zhuǎn)換

? 對數(shù)據(jù)內(nèi)置的類型進(jìn)行轉(zhuǎn)換,只需要將數(shù)據(jù)類型作為函數(shù)名即可。

4.7 實(shí)驗(yàn)

In:

ls1 = ['a,',['b',['c','d']]]

In:

id(ls1)

out:

2078516844424

In:

ls1 = ['c','d']

In:

id(ls1)

out:

2078505627016

In:

ls1

out:

['a,', ['b', ['c', 'd']]]

In:

ls1[1][1][1]

out:

'd'

In:

a1 = 1

In:

id(a1)

out:

140727695483280

In:

a1 = 2

In:

id(a1)

out:

140727695483312

In:

ls1[0] = 'f' ls1

out:

['f', ['b', ['c', 'd']]]

In:

id(ls1)

out:

2078516829704

In:

ls1.append(['h','g'])

In:

id(ls1)

out:

2078516844424

In:

ls1

out:

['f', ['b', ['c', 'd']], ['h', 'g']]

In:

del ls1[0] ls1

out:

[['b', ['c', 'd']], ['h', 'g']]

In:

ls2 = ['q','e'] ls1 + ls2

out:

[['b', ['c', 'd']], ['h', 'g'], 'q', 'e']

In:

'q' in ls2

out:

True

In:

for x in ls2:print(x, end=' ')

out:

q e

In:

list((1,2))

out:

[1, 2]

In:

ls1.extend(ls2) ls1

out:

[['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e', 'q', 'e']

In:

ls1.insert(0,'z')

In:

ls1

out:

['z', ['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e', 'q', 'e']

In:

ls1.pop() ls1

out:

['z', ['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e']

In:

ls1.remove('z')

In:

ls1

out:

[['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e']

In:

ls1.reverse()

In:

ls1

out:

['e', 'q', 'e', 'q', ['h', 'g'], ['b', ['c', 'd']]]

In:

ls2.sort()

In:

ls2.sort(reverse=True)

In:

# ls1.sort() #嵌套類型不能用

In:

ls2

out:

['q', 'e']

In:

ls2.append('e') ls2

out:

['q', 'e', 'e']

4.7.1 元組

In:

tu1 = ('a',1,['c','d'])

In:

tu1[0] = 'e'

out:

---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-38-75e55e2a1153> in <module> ----> 1 tu1[0] = 'e' TypeError: 'tuple' object does not support item assignment

4.7.2 集合

  • 不能重復(fù)
  • set()
  • 無序
    In:
set([1,1,2])

out:

{1, 2}

In:

a = set([1,2,3]) b = set([4,2,3])

In:

a - b

out:

{1}

In:

a & b

out:

{2, 3}

In:

a | b

out:

{1, 2, 3, 4}

In:

a ^ b

out:

{1, 4}

4.7.3 字典

In:

dic1 = {'a':1,'b':2,'c':3}

In:

dic1['c']

out:

3

In:

#新增 dic1['d'] = 4 dic1

out:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

In:

s1 = None # null, nan

In:

dic1['e']

out:

---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-61-898ad45657b2> in <module> ----> 1 dic1['e'] KeyError: 'e'

In:

dic1.get('e',None)

In:

dic1.keys()

out:

dict_keys(['a', 'b', 'c', 'd'])

In:

for key in dic1.keys():print(dic1[key])

out:

1 2 3 4

In:

dic1.values()

out:

dict_values([1, 2, 3, 4])

In:

dic1.items()

out:

dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

In:

for key,val in dic1.items():print(key + "|" +str(val))

out:

a|1 b|2 c|3 d|4

In:

lis1= ['a',1,['c','d']]

In:

a1,a2,a3 = lis1

In:

print(a1,a2,a3)

out:

a 1 ['c', 'd']

無序字典
In:

dic3 = {'a':1,'b':2} dic4 = {'b':2,'a':1} dic3 == dic4

out:

True

有序字典
In:

from collections import OrderedDict dic11 = OrderedDict() dic11['a'] = 1; dic11['b'] =2

In:

dic12 = OrderedDict() dic12['b'] = 2; dic12['a'] =1 dic11 == dic12

out:

False

In:

print(dic11)

out:

OrderedDict([('a', 1), ('b', 2), ('c', 0), (1, 3)])

In:

dic11.

實(shí)驗(yàn)
在一個(gè)字典中保存了股票的代碼和價(jià)格,找出股價(jià)大于100元的股票并創(chuàng)建一個(gè)新的字典。

說明:可以用字典的生成式語法來創(chuàng)建這個(gè)新字典。
In:

stocks = {'AAPL': 191.88,'GOOG': 1186.96,'IBM': 149.24,'ORCL': 48.44,'ACN': 166.89,'FB': 208.09,'SYMC': 21.29 }

In:

tar_dic = {} for key,val in stocks.items():if val > 100:tar_dic[key] = val

In:

tar_dic

out:

{'AAPL': 191.88, 'GOOG': 1186.96, 'IBM': 149.24, 'ACN': 166.89, 'FB': 208.09}

In:

tar_dic2 = {key:val for key,val in stocks.items() if val > 100}

In:

tar_dic2

out:

{'AAPL': 191.88, 'GOOG': 1186.96, 'IBM': 149.24, 'ACN': 166.89, 'FB': 208.09} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python中元组和列表转化_4.Python列表/元组/集合/字典的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。