>>> greeting = 'hello'
>>> greeting[0]
'h'
>>> greeting[-1]
'o'
>>> 'hello'[1]
'e'
>>> tag = 'i love python'
>>> tag[2:6]
'love'
>>> tag[2:-7]
'love'
>>> tag[-6:]
'python'
>>> tag[:6]
'i love'
>>> numbers = [1, 2, 3, 4, 5, 6, 7]
>>> new_numbers = numbers[:]
>>> new_numbers
[1, 2, 3, 4, 5, 6, 7]
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers = [0:10:2]
[1, 3, 5, 7, 9]
>>> number[::4] # 提取每四個元素中的第乙個元素
[1, 5, 9]
>>> numbers[8:3:-2] # 當步長設定為負數時反向提取元素,此時開始點的索引必須大於結束點的索引
[9, 7, 5]
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> 'hello.' + 'world!'
'hello.world!'
>>> 'pthon' * 5
'pythonpythonpythonpythonpython'
>>> [42]*5
[42, 42, 42, 42, 42]
>>> sequence = [none] * 5
>>> sequence
[none, none, none, none, none]
>>> permissions = 'rw'
>>> 'w' in permissions
true
>>> 'x' in permissions
false
>>> users = ['mlh', 'foo', 'bar']
>>> 'mlh' in users
true
>>> numbers = [100, 34, 678]
>>> len(numbers)
3>>> max(numbers)
678>>> min(numbers)
34>>> min(9, 2, 3, 5)
3
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> x = [1, 1, 1]
>>> x[1] = 2
>>> x
[1, 2, 1]
>>> names = ['alice', 'beth', 'cecil', 'dee-dee', 'earl']
>>> del names[2]
>>> names
['alice', 'beth', 'dee-dee', 'earl']
# 等長賦值
>>> name = list('perl')
>>> name
['p', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['p', 'e', 'a', 'r']
# 不等長賦值
>>> name = list('perl')
>>> name[1:] = list('ython')
>>> name
['p', 'y', 't', 'h', 'o', 'n']
# 插入新元素
>>> numbers = [1, 5]
>>> numbers[1:1] = [2, 3, 4] # 只是「替換」了乙個空的分片,因此實際的操作時插入了乙個序列
>>> numbers
[1, 2, 3, 4, 5]
# 刪除元素
>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[1:4] =
>>> numbers
[1, 5]
>>> lst = [1, 2, 3]
>>> lst
[1, 2, 3, 4]
>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
2
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> knights = ['we', 'are', 'the', 'knights', 'who', 'say', 'ni']
>>> knights.index('who')
4
>>> numbers = [1, 2, 3, 5, 6]
>>> numbers.insert(3, 'four') # 插入位置的索引號為3
>>> numbers
[1, 2, 3, 'four', 5, 6]
>>> x = [1, 2, 3]
>>> x.pop()
3>>> x
[1, 2]
>>> x.pop(0)
1>>> x
[2]
>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> x = [1, 2, 3]
>>> x.reverse()
>>> x
[3, 2, 1]
>>> x = [4, 6, 2, 1, 7, 9]
>>> x.sort()
>>> x
[1, 2, 4, 6, 7, 9]
# 若在不改變原始列表的情況下得到排序後的列表,則應使用sorted(可應用於任何序列,但返回乙個列表)
>>> x = [4, 6, 2, 1, 7, 9]
>>> y = sorted(x)
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
>>> 1, 2, 3
(1, 2, 3)
# 另一種建立元組的方法
>>> (1, 2, 3)
(1, 2, 3)
>>> 42 # 不是元組
42>>> 42, # 是元組
(42,)
>>> (42) # 不是元組
42>>> (42,) # 是元組
(42,)
>>> tuple([1, 2, 3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1, 2, 3))
(1, 2, 3)
>>> x = 1, 2, 3
>>> x[1]
2>>> x[0:2] # 元組的分片還是元組
(1, 2)
python入門之列表和元組
列表和元組是一組資料的集合,同乙個集合中的元素型別可以不一樣 1.1 列表的基本操作 索引操作 分片操作 序列相加 序列相乘等 例如 索引操作 x 1,adb 3,45 print x 1 輸出adb print x 1 輸出45 例如 分片操作 x abcdefg print x 1 2 prin...
Python列表和元組
序列可修改,而元組不能。eda edward 42 序列可包含其它列表 edward edward 43 john john 44 database edward,john database edward 43 john 44 序列的分片 nubs range 10 nubs 0,1,2,3,4,5...
python 列表和元組
資料結構 資料結構是以某種方式組合起來的資料元素。在python中最基本的資料結構為序列,序列中的每個元素都有編號,就像學號,可以通過學號找到你本人。不同的序列中的編號一般都是從0開始。序列包括元組和列表,另外還有字典。列表和元組的區別 列表是可以修改的,而元組不可以。在處理資料要特別注意這一點,但...