[toc]

python 日常练习

和并两个有序列表,并且保持合并后的两个列表有序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
In [11]: l1 = [1,2,3,4]
In [12]: l2 = [2,3,7,9]
In [13]: l3 = []
In [14]: for x in l1:
...: while len(l2) > 0 :
...: if x > l2[0]:
...: l3.append(l2.pop(0))
...: else:
...: l3.append(x)
...: break
...: if len(l2) <= 0:
...: l3.append(x)
...: l3.append(l2)
...:
...:
In [15]:
In [15]: l3
Out[15]: [1, 2, 2, 3, 3, 4, [7, 9]]

按单词反转字符串, 如’I love Linux’ 反转为 ‘Linux love I’

1
2
3
4
5
6
7
In [30]: ' '.join(s.split()[::-1])
Out[30]: 'Linux love I'
In [31]: s = "I love Linux"
In [32]: ' '.join(s.split()[::-1])
Out[32]: 'Linux love I'
In [33]: s.split()[::-1]
Out[33]: ['Linux', 'love', 'I']

找出一个列表中只出现了一次的数字,并且保持原来的次序,例如[1,2,1,3,2,5],结果为[3,5]

1
2
3
4
5
6
Number(数字)
String(字符串)
List(列表)
Tuple(元组)
Set(集合)
Dictionary(字典)

有bug的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 例如,当1 出现三次时,会把1也记录下来
In [59]: lst_set
Out[59]: [1, 2, 3, 5]

In [60]: lst = [1,1,2,1,3,2,5]
In [61]: ret = []
In [62]: for x in lst:
...: if x in ret:
...: ret.remove(x)
...: else:
...: ret.append(x)
...:
In [63]: ret
Out[63]: [1, 3, 5]
1
2
3
4
5
6
7
8
9
# 去除bug后
In [51]: lst = [1,1,2,1,3,2,5]
In [52]: ret = []
In [53]: for x in lst:
...: if lst.count(x) == 1:
...: ret.append(x)
...:
In [54]: ret
Out[54]: [3, 5]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 当重复的数过大的时,不便重复的元素每个都遍历,如10w个1重复
In [77]: lst = [1,1,1,1,1,1,2,1,3,2,5]
In [78]: lst_set = list(set(lst))
In [79]: lst_set
Out[79]: [1, 2, 3, 5]
In [80]: ret = []
In [81]: ret
Out[81]: []
In [84]: for x in lst_set:
...: if lst.count(x) == 1:
...: ret.append(x)
...:
In [85]: ret
Out[85]: [3, 5]

取出一个列表中的最大值

1
2
3
In [86]: lst = [1,3,5,8,10,101,301]
In [87]: max(lst)
Out[87]: 301
1
2
3
4
5
6
7
8
In [93]: lst = [1,3,5,8,10,101,301]
In [94]: m = lst[0]
In [96]: for x in lst:
...: if x > m:
...: m = x
...:
In [97]: m
Out[97]: 301

写一个程序,把字符串转化为数字,不允许使用函数和模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
In [138]: s = '123.456'
In [139]: integer, decimals=s.split('.')
In [140]: list(enumerate(integer))
Out[140]: [(0, '1'), (1, '2'), (2, '3')]
In [141]: integer_length = len(integer)
In [142]: result = 0
...: for i, x in enumerate(integer):
...: if x == '0':
...: result += 0 * 10 ** (integer_length -i -1)
...: if x == '1':
...: result += 1 * 10 ** (integer_length -i -1)
...: if x == '2':
...: result += 2 * 10 ** (integer_length -i -1)
...: if x == '3':
...: result += 3 * 10 ** (integer_length -i -1)
...: if x == '4':
...: result += 4 * 10 ** (integer_length -i -1)
...: if x == '5':
...: result += 5 * 10 ** (integer_length -i -1)
...: if x == '6':
...: result += 6 * 10 ** (integer_length -i -1)
...: if x == '7':
...: result += 7 * 10 ** (integer_length -i -1)
...: if x == '8':
...: result += 8 * 10 ** (integer_length -i -1)
...: if x == '9':
...: result += 9 * 10 ** (integer_length -i -1)
...:
In [143]: for i, x in enumerate(decimals):
...: if x == '0':
...: result += 0 * 10 ** (-1 * (i+1))
...: if x == '1':
...: result += 1 * 10 ** (-1 * (i+1))
...: if x == '2':
...: result += 2 * 10 ** (-1 * (i+1))
...: if x == '3':
...: result += 3 * 10 ** (-1 * (i+1))
...: if x == '4':
...: result += 4 * 10 ** (-1 * (i+1))
...: if x == '5':
...: result += 5 * 10 ** (-1 * (i+1))
...: if x == '6':
...: result += 6 * 10 ** (-1 * (i+1))
...: if x == '7':
...: result += 7 * 10 ** (-1 * (i+1))
...: if x == '8':
...: result += 8 * 10 ** (-1 * (i+1))
...: if x == '9':
...: result += 9 * 10 ** (-1 * (i+1))
...:
In [144]: result
Out[144]: 123.456
In [145]: type(result)
Out[145]: float

评论