python學習筆記2---python中表示「組」的概念與定義

列表(list)

>>> type([1,2,3,4,5,6])
<class 'list'>
>>> type(["hello","worls",1,9])
<class 'list'>
>>> type(["hello","worls",1,9,True,False])
<class 'list'>
>>> type([[1,2],[3,4],[True,False]])//嵌套列表,至關於其餘語言中的二維數組
<class 'list'>
列表的基本操做
'宮保雞丁'
>>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][3]
'魚香肉絲'
>>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][0:2]
['宮保雞丁', '糖醋里脊']
>>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][-1]
'魚香肉絲'
>>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][-1:]
['魚香肉絲']
>>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"]+['拍黃瓜','炒豆芽']
['宮保雞丁', '糖醋里脊', '粉絲蒸蝦', '魚香肉絲', '拍黃瓜', '炒豆芽']
>>> ['拍黃瓜','炒豆芽']*['拍黃瓜','炒豆芽']
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    ['拍黃瓜','炒豆芽']*['拍黃瓜','炒豆芽']
TypeError: can't multiply sequence by non-int of type 'list'
>>> ['拍黃瓜','炒豆芽']*3
['拍黃瓜', '炒豆芽', '拍黃瓜', '炒豆芽', '拍黃瓜', '炒豆芽']
>>> ['拍黃瓜','炒豆芽']-['拍黃瓜']
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    ['拍黃瓜','炒豆芽']-['拍黃瓜']
TypeError: unsupported operand type(s) for -: 'list' and 'list'

元組(tuple)

>>> (1,2,3,4,5)
(1, 2, 3, 4, 5)
>>> (1,'-1',True)
(1, '-1', True)
>>> (1,2,3,4)[0]
1
>>> (1,2,3,4)[0:2]
(1, 2)
>>> (1,2,3)+(4,5,6)
(1, 2, 3, 4, 5, 6)
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> type((1,2,3))
<class 'tuple'>
>>> type(1)
<class 'int'>
>>> type([1,2,3])
<class 'list'>
>>> type('hello')
<class 'str'>
>>> type(('hello'))
<class 'str'>
>>> type((1))//若是括號裏只有一個元素,python會默認爲是要作數學運算,等同於type(1)
<class 'int'>
>>> (1+1)*2
4
>>> (1,2,3)
(1, 2, 3)
>>> (1)
1
>>> (1,)
(1,)
>>> type((1,))
<class 'tuple'>
>>> type(())
<class 'tuple'>
>>> type([1])
<class 'list'>

str list tuple都是序列

>>> 'hello world'[2]
'l'
>>> [1,2,3][2]
3
>>> (1,2,3)[2]
3

切片

>>> [1,2,3,4,5][0:3]
[1, 2, 3]
>>> [1,2,3,4,5][-1:]
[5]
>>> 
>>> "hello world"[0:8:0]
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    "hello world"[0:8:0]
ValueError: slice step cannot be zero
>>> "hello world"[0:8:1]
'hello wo'
>>> "hello world"[0:8:2]
'hlow'
>>> "hello world"[0:8:3]
'hlw'
>>> "hello world"[0:8:4]
'ho'
>>> "hello world"[0:8:5]
'h '
>>> "hello world"[0:8:6]
'hw'
>>> "hello world"[0:8:7]

判斷一個序列中是否包含某個元素

>>> 3 in [1,2,3,4,5,6]
True
>>> 7 in [1,2,3,4,5,6]
False
>>> 3 not in [1,2,3,4,5,6]
False

序列的其餘操做

>>> len([1,2,3,4,5,6])
6
>>> len('hello world')
11
>>> max([1,2,3,4,5,6])
6
>>> min([1,2,3,4,5,6])
1
>>> max('hello world')
'w'
>>> min('hello world')
' '
>>> min('helloworld')
'd'

ASCII碼

>>> ord('w')
119
>>> ord('d')
100
>>> ord(' ')
32

set 集合

集合的特性

>>> type({1,2,3,4,5,6})
<class 'set'>
>>> {1,2,3,4,5,6}[0]  //集合是無序的,沒有像str,list,tuple的架構索引,不能切片
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    {1,2,3,4,5,6}[0]
TypeError: 'set' object does not support indexing
>>> {1,2,3,4,5,6}[0:2]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    {1,2,3,4,5,6}[0:2]
TypeError: 'set' object is not subscriptable
>>> {1,1,2,2,3,3,4,4}  //集合的第二個特性:不重複
{1, 2, 3, 4}

集合的操做

>>> len({1,2,3})
3
>>> len({1,1,1})
1
>>> len({1,2,2})
2
>>> 1 in {1,2,3}
True
>>> 1 not in {1,2,3}
False
>>> {1,2,3,4,5,6} - {3,4}  //求兩個集合的差集
{1, 2, 5, 6}
>>> {1,2,3,4,5,6} & {3,4}  //交集
{3, 4}
>>> {1,2,3,4,5,6} | {3,4,7}  //合集|並集
{1, 2, 3, 4, 5, 6, 7}
//定義一個空的集合set()
>>> type({})
<class 'dict'>
>>> type(set())
<class 'set'>
>>> len(set())
0

dict 字典

一個字典能夠有不少個key和value,字典是一個集合類型,而不是序列
經過key 獲得/訪問 value
>>> {"menu1":"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}[0]
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    {"menu1":"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}[0]
KeyError: 0
>>> {"menu1":"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}['menu1']
'宮保雞丁'
>>> {"menu1":"宮保雞丁","menu1":"白斬雞","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}['menu1']
'白斬雞'
>>> {"menu1":"宮保雞丁","menu1":"白斬雞","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}
{'menu1': '白斬雞', 'menu2': '糖醋里脊', 'menu3': '粉絲蒸蝦', 'menu4': '魚香肉絲'}
>>> {1:"宮保雞丁","1":"白斬雞","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}  //key也能夠是數字
{1: '宮保雞丁', '1': '白斬雞', 'menu2': '糖醋里脊', 'menu3': '粉絲蒸蝦', 'menu4': '魚香肉絲'}

value的類型能夠是str | int | float | list | set | dict

>>> type({1:"宮保雞丁","1":"白斬雞","menu2":{1:1},"menu3":"粉絲蒸蝦",})
<class 'dict'>

key必須是不可變的類型(str、tuple)

>>> {[1,2]:"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    {[1,2]:"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}
TypeError: unhashable type: 'list'
>>> {(1,2):"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}
{(1, 2): '宮保雞丁', 'menu2': '糖醋里脊', 'menu3': '粉絲蒸蝦', 'menu4': '魚香肉絲'}

clipboard.png

相關文章
相關標籤/搜索