序列

In [6]:
edward = ['Edward', 42]
In [7]:
john = ['John', 50]
In [8]:
database = [edward, john]
In [9]:
database
Out[9]:
[['Edward', 42], ['John', 50]]
In [19]:
database[0]
Out[19]:
['Edward', 42]

索引

In [18]:
database[0][0][-1]
Out[18]:
'd'
In [22]:
fourth = input('Year:')[3]
Year:2018
In [23]:
fourth
Out[23]:
'8'
In [27]:
months = [
    'Jan.',
    'Feb.',
    'Mar.',
    'Apr.',
    'May',
    'Jun.',
    'Jul',
    'Aug.',
    'Sep.',
    'Oct.',
    'Nov.',
    'Dec.'
]
# 此处乘法为对应字符的个数
endings = ['st','nd','rd'] + 17 * ['th']\
        + ['st','nd','rd'] + 7 * ['th']\
        + ['st']
year = input('Year: ')
month = input('Month(1-12):')
day = input('Day(1-31):')
month_number = int(month)
day_number = int(day)
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print(month_name + ' ' + ordinal + ',' + year)
Year: 2018
Month(1-12):12
Day(1-31):1
Dec. 1st,2018

分片

In [28]:
tag = '<a href="http://csyanwb.cn">JingShui</a>'
In [29]:
tag[9:-14]
Out[29]:
'http://csyanwb.cn'
In [31]:
numbers = [1,2,3,4,5,6,7,8,9,10]
In [32]:
numbers[3:6]
Out[32]:
[4, 5, 6]
In [33]:
numbers[0:1]
Out[33]:
[1]
In [38]:
numbers[-3:0] #向右 --->
Out[38]:
[]
In [39]:
numbers[-3:]
Out[39]:
[8, 9, 10]
In [40]:
numbers[:3]
Out[40]:
[1, 2, 3]
In [42]:
numbers[:]
Out[42]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [43]:
numbers[0:10:1]
Out[43]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [44]:
numbers[0:10:2]
Out[44]:
[1, 3, 5, 7, 9]
In [57]:
numbers[::2]
Out[57]:
[1, 3, 5, 7, 9]
In [49]:
numbers[1:9:2]
Out[49]:
[2, 4, 6, 8]
In [52]:
numbers[9:1:-2]
Out[52]:
[10, 8, 6, 4]
In [55]:
numbers[1:9:-2]
Out[55]:
[]
In [58]:
numbers[::-2]
Out[58]:
[10, 8, 6, 4, 2]
In [59]:
numbers[:5:-2]
Out[59]:
[10, 8]

加法

In [60]:
[1,2,3]+[4,5,6]
Out[60]:
[1, 2, 3, 4, 5, 6]
In [61]:
'Hello,' + 'world!'
Out[61]:
'Hello,world!'
In [62]:
[1,2,3] + 'Hello'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-aee2ba5ec26d> in <module>()
----> 1 [1,2,3] + 'Hello'

TypeError: can only concatenate list (not "str") to list

乘法

In [63]:
[42]*10
Out[63]:
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
In [64]:
'python'*5
Out[64]:
'pythonpythonpythonpythonpython'
In [66]:
sequence = [None]*10
In [67]:
sequence
Out[67]:
[None, None, None, None, None, None, None, None, None, None]
In [82]:
sentence = input("Sentence:")
screen_width = 80
text_width = len(sentence)
box_width = text_width + 2
left_margin = (screen_width - box_width)//2
print("                                  ")
print(' '*left_margin + '+' + "-"*(box_width-2) + '+')
print(' '*left_margin + '|' + " "*text_width + '|')
print(' '*left_margin + '|' +  sentence + '|')
print(' '*left_margin + '|' + " "*text_width + '|')
print(' '*left_margin + '+' + "-"*(box_width-2) + '+')
print("                                   ")
Sentence:Today is Sunday!
                                  
                               +----------------+
                               |                |
                               |Today is Sunday!|
                               |                |
                               +----------------+
                                   

成员资格

In [83]:
permissions= 'rw'
In [85]:
'w' in permissions
Out[85]:
True
In [87]:
users = ['mlh','ywb','bar']
In [88]:
input("Enter your user name:") in users
Enter your user name:ywb
Out[88]:
True
In [89]:
subject = "$$$ Get rich now!!! $$$"
In [90]:
'$$$' in subject
Out[90]:
True
In [91]:
database = [
    ['albert', '1234'],
    ['dilbert','4567'],
    ['smith','7524'],
    ['jones','9843']
]
username = input("User name:")
pin = input("PIN Code:")
if [username, pin] in database:
    print("Access granted!")
User name:jones
PIN Code:9843
Access granted!

长度&最大、最小值

In [92]:
numbers=[100,34,678]
In [93]:
len(numbers)
Out[93]:
3
In [94]:
max(numbers)
Out[94]:
678
In [96]:
min(numbers)
Out[96]:
34
In [97]:
max(32,65,12,78,99)
Out[97]:
99

列表 list

In [98]:
list("Hello")
Out[98]:
['H', 'e', 'l', 'l', 'o']

元素赋值

In [99]:
x = [1,1,1]
In [100]:
x[1] = 2
In [101]:
x
Out[101]:
[1, 2, 1]

删除元素 del

In [102]:
del x[2]
In [103]:
x
Out[103]:
[1, 2]

分片赋值

In [104]:
name = list('Perl')
In [105]:
name
Out[105]:
['P', 'e', 'r', 'l']
In [107]:
name[2:] = list("ar")
In [108]:
name
Out[108]:
['P', 'e', 'a', 'r']
In [109]:
name[1:] = list("ython")
In [110]:
name
Out[110]:
['P', 'y', 't', 'h', 'o', 'n']
In [112]:
numbers = [1,5]
In [115]:
numbers[1:1] = [2,3,4]
In [116]:
numbers
Out[116]:
[1, 2, 3, 4, 5]
In [117]:
numbers[1:4] = []
In [118]:
numbers
Out[118]:
[1, 5]

append

In [121]:
lst = [1,2,3]
In [122]:
lst.append(4)
In [123]:
lst
Out[123]:
[1, 2, 3, 4]

count

In [124]:
['to','be','or','not','to','be'].count('to')
Out[124]:
2
In [125]:
x = [[1,2],1,1,[2,1,[1,2]]]
In [126]:
x.count([1,2])
Out[126]:
1

extend

In [160]:
a = [1,2,3]
b = [4,5,6]
In [161]:
a.extend(b)
a
Out[161]:
[1, 2, 3, 4, 5, 6]
In [162]:
a = [1,2,3]
b = [4,5,6]
In [163]:
a[len(a):] = b
a
Out[163]:
[1, 2, 3, 4, 5, 6]

index

In [165]:
knights = ['we', 'are', 'the', 'knights', 'who', 'say', 'ni']
knights.index('who')
Out[165]:
4
In [166]:
knights.index("herring")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-166-87226c97b57d> in <module>()
----> 1 knights.index("herring")

ValueError: 'herring' is not in list

insert

In [171]:
numbers = [1,2,3,5,6,7]
numbers.insert(3,'four')
numbers
Out[171]:
[1, 2, 3, 'four', 5, 6, 7]
In [172]:
numbers = [1,2,3,5,6,7]
numbers[3:3] = ['four']
numbers
Out[172]:
[1, 2, 3, 'four', 5, 6, 7]

pop

In [182]:
x = [1,2,3]
x.pop()
Out[182]:
3
In [183]:
x
Out[183]:
[1, 2]
In [184]:
x.pop(0)
x
Out[184]:
[2]
In [185]:
x = [1,2,3]
x.append(x.pop())
x
Out[185]:
[1, 2, 3]

remove

In [189]:
x = ['to','be','or','not','to','be']
x.remove('be')
In [258]:
x
Out[258]:
[9, 7, 6, 4, 2, 1]

reverse

In [191]:
x = [1,2,3]
x.reverse()
In [192]:
x
Out[192]:
[3, 2, 1]
In [194]:
list(reversed(x))
Out[194]:
[1, 2, 3]

sort

In [207]:
x = [5,3,9,1,6,8]
In [196]:
x.sort()
In [197]:
x
Out[197]:
[1, 3, 5, 6, 8, 9]
In [206]:
y = x.sort()  # Wrong
In [205]:
print(y)
None
In [215]:
x = [5,3,9,1,6,8]
y = x #直接赋值也不能保持x不发生变化!
y.sort()
x
Out[215]:
[1, 3, 5, 6, 8, 9]
In [218]:
x = [5,3,9,1,6,8]
y = sorted(x) #sorted
x
Out[218]:
[5, 3, 9, 1, 6, 8]
In [219]:
y
Out[219]:
[1, 3, 5, 6, 8, 9]
In [235]:
sorted('Python') #现在已区分大小写!
Out[235]:
['P', 'h', 'n', 'o', 't', 'y']

高级排序

cmp在python3未指定

In [248]:
x = ['asdasdasd','sdssdsd','weqqw','weqe','dsfddfs']
x.sort(key=len)
x
Out[248]:
['weqe', 'weqqw', 'sdssdsd', 'dsfddfs', 'asdasdasd']
In [250]:
x = [4,6,2,1,7,9]
x.sort(reverse=True)
x
Out[250]:
[9, 7, 6, 4, 2, 1]

元组

In [252]:
1,2,3
Out[252]:
(1, 2, 3)
In [253]:
()
Out[253]:
()
In [254]:
42
Out[254]:
42
In [255]:
42,
Out[255]:
(42,)
In [256]:
3*(40+2)
Out[256]:
126
In [257]:
3*(40+2,)
Out[257]:
(42, 42, 42)

tuple

In [259]:
tuple([1,2,3])
Out[259]:
(1, 2, 3)
In [260]:
tuple('abc')
Out[260]:
('a', 'b', 'c')
In [261]:
tuple((1,2,3))
Out[261]:
(1, 2, 3)
In [263]:
x = 1,2,3
x[1]
Out[263]:
2
In [264]:
x[0:2]
Out[264]:
(1, 2)