python列表 元組與集合

2021-08-20 09:23:45 字數 1026 閱讀 3371

python列表

一、列表的建立與操作

1.建立列表

a =     ##元素型別可為int,float,complex,str,list,tuple

b = [a, 1, true, 3j + 2, "hi"]

c = [[1, 2, 3, 4], [a, b], 233, "hello"]

d = [a, b, c ]

2.列表的索引與切片

>> c = [[1, 2, 3, 4], [5,6], 233, "hello"]">>>> c

[[1, 2, 3, 4], [5, 6], 233, 'hello']

>>> type(c) ##顯示型別

>>> c[0][-1]

4>>> c[-1][3]

'l'>>> c[-2]

233

3.強制轉換

>>> s=list(range(5))  ##採用內建方法list()強制轉換

>>> s

[0, 1, 2, 3, 4]

>>> s[3]

3

4.列表的重複

>>> list=['a',3,true,'hello']

>>> list*3

['a', 3, true, 'hello', 'a', 3, true, 'hello', 'a', 3, true, 'hello']

5.列表的成員操作符

>>> list

['a', 3, true, 'hello']

>>>

'a'in list ##判斷元素是否在列表中

true

>>>

'test'

in list

false

>>>

true

in list

true

python列表 元組 集合

型別 list tuple set特點 有序,可重複,內容可變,可通過下標獲取元素 有序,可重複,內容不可變,可通過下標獲取元素 無序 不可重複 定義 張三 李四 王五 張三 李四 王五 只包含乙個元素的元組 張三 空 set 查詢list index tuple index 修改list inde...

Python元組 列表和集合

atuple 3,2,6 sorted atuple 2,3,6 atuple.sort traceback most recent call last file line 1,in atuple.sort attributeerror tuple object has no attribute s...

python列表 元組 字典 集合

列表list 用 表示,list是一種有序的集合,可以隨時新增和刪除其中的元素。元組tuple 用 表示,和列表類似,元組也是一種有序列表,雖然tuple和list非常之類似,但是list初始化之後使可以改變的,但是,元組一旦初始化之後就不可以改變。這點與python中的字串類似,所以我們說元組和字...