Python3基礎教程-第2章筆記-上


1、序列概覽

 

 

序列:列表,元組  →  每個元素都有編號

序列可包含其他序列,例如

>>> edward = ['edward gumby',42]
       
>>> john = ['john smith',50]
       
>>> database = [edward,john]
       
>>> database
       
[['edward gumby', 42], ['john smith', 50]]

 

 2、通用序列操作:索引、分片、加、乘、成員資格

(1)索引:元素編號,通過索引訪問單個元素。

*索引0指向第一個元素

*負數索引:最后一個元素的位置編號是-1非0

 

>>> greeting = 'hello'
       
>>> greeting[0]
       
'h'
>>> 'hello'[0]
       
'h'

 

函數調用返回一個序列:可直接對返回結果進行索引操作

例如,只對輸入年份的第四個數字感興趣

>>> fourth = input('year:')[3]
year:1994
>>> fourth
'4'
>>> 

 

 

索引示例:輸入年月日,打印相對應日期的月份名稱

#根據給定的年月日以數字形式打印日期
months = [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'December'
]

#以1—31的數字作為結尾的列表
endings = ['st','nd','rd',] + 17 * ['th']\
        + ['st','nd','rd',] + 17 * ['th']\
        + ['st']

year = input ('Year:')
month = input ('Month(1-12):')
day = input ('Day(1-31):')

month_number = int(month)
day_number = int(day)

#記得要將月份和天數減1,以獲得正確的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]

print (month_name + ' '+ordinal + ','+year)

打印結果

Year:1994
Month(1-12):7
Day(1-31):4
July 4th,1994

 

分片:訪問一定范圍內的元素,通過冒號相隔的兩個索引實現

*第一個索引是提取部分的第一個元素編號,第二個索引是分片之后剩下部分的第一個元素的編號(第1個索引的元素在分片內,第2個不在)

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:1]
[1]
>>>

1)優雅的捷徑

如果分片部分包括序列結尾的元素,那么只需置空最后一個索引

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:]
[4, 5, 6, 7, 8, 9, 10]
>>> 

序列開始的元素同樣適用

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[:3]
[1, 2, 3]
>>> 

若要復制整個序列,則兩個索引都置空

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 

2)更大的步長:捷徑依然可用,步長不能為0,但是可為負數

例如,提取每4個元素 的第1個

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[::4]
[1, 5, 9]
>>> 

步長為負數表示從右到左提取元素

例如,

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[8:3:-1]
[9, 8, 7, 6, 5]
>>> 

(2)序列相加:使用加號進行序列的連接操作

*列表和字符串是無法連接在一起的,兩種相同類型的序列才能進行連接操作

例如,

>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'hello,' + 'world!'
'hello,world!'
>>> [1,2,3] + 'world!'
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    [1,2,3] + 'world!'
TypeError: can only concatenate list (not "str") to list
>>> 

(3)乘法:乘法是重復序列

例如,

>>> 'python' * 5
'pythonpythonpythonpythonpython'
>>> [7]*10
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
>>> 

 

(4)成員資格:in運算符(布爾運算符),條件為真返回True,條件為假返回False

例如,

>>> permissions = 'rw'
>>> 'w' in permissions
True
>>> 'x' in permissions
False

>>> users = ['mlh','foo','bar']
>>> input ('Enter your user name:') in users
Enter your user name:mlh
True

>>> subject = '$$$ Get rich now!$$$'
>>> '$$$' in subject
True
>>>

 

序列成員資格示例

#檢查用戶名和PIN碼
database = [
    ['albert','1234'],
    ['dilbert','4242'],
    ['smith','7524'],
    ['jones','9843']
]
username = input ('Username:')
pin = input ('PIN code:')

if [username,pin] in database: print ('Access granted')

打印結果

=============== RESTART: D:/Python37/python_test/membership.py ===============
Username:albert
PIN code:1234
Access granted
>>> 
=============== RESTART: D:/Python37/python_test/membership.py ===============
Username:chenhuimin
PIN code:0704
>>> 

(5)長度、最小值和最大值:len,min,max

>>> numbers = [100,34,678]
>>> len(numbers)
3
>>> max(numbers)
678
>>> min(numbers)
34
>>> max(2,3)
3
>>> min(9,3,2,5)
2
>>> 

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com