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
55
56
1 、冒泡排序
 
#!/usr/bin/env python
#coding:utf-8
arr = [ 88 , 20 , 10 , 3 , 2 , 5 , 7 , 9 , 100 , 55 , 77 ]
for  in  range ( len (arr) - 1 ):
     for  in  range ( len (arr) - 1 - i):
         if  arr[j]>arr[j + 1 ]:
             arr[j],arr[j + 1 ] = arr[j + 1 ],arr[j]
print  arr
 
 
2
 
li = [ 1 , 2 , 3 ]
li.pop()
print  li
#pop方法移除列表中的一个元素,默认是最后一个
 
函数简单的排序方法
def  quick(arr):
     if  len (arr)< = 1 :
         return  arr
     sign = arr.pop()
     left = []
     right = []
     for  num  in  arr:
         if  num<sign:
             left.append(num)
         else :
             right.append(num)
     return  quick(left) + [sign] + quick(right)
print  quick(arr)
 
 
3 、统计一段字符串中字符出现的次数
 
s = '''With regard to international cooperation, China and the United States will enhance cooperation in the Group of 20 and on other global economic governance platforms, advance the reform of the international economic and financial systems, and promote the strong, sustainable, balanced and inclusive development of the world economy.
"China and the United States are each other's largest trading partner and major source of investment. Cooperation is the only right choice for the two sides," Chinese Vice Premier Wang Yang said in his opening remarks at the CED on Wednesday.
China-U.S. cooperation will benefit not only the companies and people of the two countries, but also the whole world, said Wang.
The United States also acknowledged the importance of the bilateral economic relationship. "As the world's two largest economies and the major drivers of global growth, the United States and China have strong overlapping interests," said U.S. Treasury Secretary Steven Mnuchin in his opening remarks at the CED.
'''
 
res = {}
for  in  s:
     if  l = = ' '  or  l = = '/n' :
         continue
     res[l] = res.get(l, 0 ) + 1
for  k,v  in  sorted (res.items(),key = lambda  x:x[ 1 ],reverse = True )[: 3 ]:
     print  '%s count is %s' % (k,v)
     
     或,
def  GetOrderkey(x):
     return  x[ 1 ]
for  k,v  in  sorted (res.items(),key = GetOrderkey,reverse = True )[: 3 ]:
     print  '%s count is %s' % (k,v)