1 x = [1, 'a', 2, 'b']
2 type(x)

list

 

%添加元素
1
x.append(3.3) 2 print(x)
[1, 'a', 2, 'b', 3.3]

%遍历链表
1
for item in x: 2 print(item)
1
a
2
b
3.3

%使用索引进行链表遍历
1
i=0 2 while( i != len(x) ): 3 print(x[i]) 4 i = i + 1
1
a
2
b
3.3

%使用+链接链表
1
[1,2] + [3,4]
[1, 2, 3, 4]

%使用*重复链表
1
[1]*3
[1, 1, 1]

%使用in判断链表中是否存在元素
1
1 in [1, 2, 3]
True

字符串
%使用[]来对字符串进行分段
1
x = 'This is a string' 2 print(x[0]) #first character 3 print(x[0:1]) #first character, but we have explicitly set the end character 4 print(x[0:2]) #first two characters
T
T
Th

%-1返回最后字符串最后一个元素
1
x[-1]
'g'

1 x[-4:-2]
'ri'

1 x[:3]
'Thi'

1 x[3:]
's is a string'

1 firstname = 'Christopher'
2 lastname = 'Brooks'
3 
4 print(firstname + ' ' + lastname)
5 print(firstname*3)
6 print('Chris' in firstname)
Christopher Brooks
ChristopherChristopherChristopher
True

%通过特殊字符拆分字符串
1
firstname = 'Christopher Arthur Hansen Brooks'.split(' ')[0] # [0] selects the first element of the list 2 lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-1] # [-1] selects the last element of the list 3 print(firstname) 4 print(lastname)
Christopher
Brooks

%字符串合并
1
'Chris' + '2'
'Chris2'

%字符串转换
1
'Chris' + str(2)
'Chris2'









 

posted on 2018-03-05 19:20  郑哲  阅读(154)  评论(0编辑  收藏  举报