1.列表排序
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
def que6(): # 6.输入三个整数x, y, z,形成一个列表,请把这n个数由小到大输出。 # 程序分析:列表有sort方法,所以把他们组成列表即可。 li = np.random.randint( - 100 , 100 , size = 10 ) # 就地转化 li = li.tolist() # 用sort()结果 li_sort = sorted (li, reverse = False ) print ( '用sort方法,重新排列结果:{}' . format (li_sort)) # 不用sort方法,自己写排序方法做, # 冒泡排序 def bubbleSort(m): m = m.copy() for time in range ( 1 , len (m)): for index in range ( len (m) - time): if m[index] > m[index + 1 ]: m[index], m[index + 1 ] = m[index + 1 ] , m[index] return m # 选择排序 def selectSort(m): m = m.copy() for seat_L in range ( len (m) - 1 ): for seat_R in range (seat_L + 1 , len (m)): if m[seat_L] > m[seat_R]: m[seat_L], m[seat_R] = m[seat_R], m[seat_L] return m # 插入排序1(内部写成函数): def insertSort_1(m): result = [] # 单个元素k插入列表li def to_insert(li, k): # 标识符 tab = False # 寻找插入位置 # 循环次数应该至少大于列表长度+1,None也占一位(空列表),即认为扑克牌最末尾还有一张‘空牌' for i in range ( len (li) + 1 ): # 修改标识符,标志‘遍历完后的下一个循环',即在和‘空牌'比较 if i = = ( len (li)): tab = True # 如果在对li[-1]比较完成(包含)之前,且寻找到位置,即把扑克从左往右比较一遍 if not tab and k < li[i]: li.insert(i, k) break # 如果遍历完成,多循环一次,即和‘空牌'不需要比较,直接把牌替换掉‘空牌' if tab: li.append(k) return li # 遍历列表 # result = result[:1] for length in range ( len (m)): result = to_insert(result, m[length]) # print(result,m[length]) return result # 插入排序2(直接嵌套循环): def insertSort2(m): m = m.copy() result = m[: 1 ] for index_choose in range ( 1 , len (m)): # 手上已经有index_choose张牌,比较第index_choose+1张牌则append # 逐个比对手上的牌,如果都对比了一遍,则加到最后 for index_insert in range ( len (result) + 1 ): print (result, index_insert, '\n' ,m, index_choose, '\n\n' ) if index_insert ! = index_choose and m[index_choose] < result[index_insert] : result.insert(index_insert, m[index_choose]) break if index_insert = = index_choose: result.append(m[index_choose]) # print(result, m[index_choose]) return result # print(li) print ( '插入排序:' ,insertSort3(li)) print ( '选择排序:' ,selectSort(li)) print ( '冒泡排序:' ,bubbleSort(li)) que6() |
2.调换字典键值
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
|
# 1. 调换元素.\ def que1(): d = { 1 : "one" , 2 : "two" } # 方法1 --- 动态赋值 def method1(d): d = d.copy() result = {} for k,v in d.items(): result[v] = k return result # 方法2 --- 生成器 def method2(d): d = d.copy() result = {v:k for k,v in d.items()} return result # 方法3 --- 由值寻找键 def method3(d): d = d.copy() # 由键寻找值 def match(dic, b): return [k for k,v in dic.items() if v = = b] # 先生成key-None,再赋值 result = {} result = result.fromkeys(d.values()) for k in result.keys(): result[k] = match(d, k)[ 0 ] return result # 方法4 --- 列表转字典 < 直接转换/动态赋值 > def method4(d): d = d.copy() key = d.keys() val = d.values() data = list ( zip (key, val)) # 方法4-1 result1 = {} for i in range ( len (data)): result1[data[i][ 1 ]] = data[i][ 0 ] # 方法4-2 result2 = dict ( zip (val, key)) return result1, result2 print ( '新列表动态赋值方法:{}' . format (method1(d))) print ( '生成器方法:{}' . format (method2(d))) print ( '由键寻值方法:{}' . format (method3(d))) print ( '动态赋值列表转字典方法:{}' . format (method4(d)[ 0 ])) print ( '直接列表转字典方法:{}' . format (method4(d)[ 1 ])) # que1() |
3.删除列表中的重复元素
遇到问题没人解答?小编创建了一个Python学习交流群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
删除重复元素list =[1,2,5,4,1,5,6,8,0,2,5]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
a = np.random.randint( - 100 , 100 , size = 10 ) a = a.tolist() def method1(a): a = a.copy() a = set (a) return a def method2(a): b = a.copy() c = 0 for i in range ( len (a) - 1 ): if b[i + c] in b[:i + c] + b[i + c + 1 :]: b.pop(i + c) c - = 1 return b print ( '集合法:' ,method1(a)) print ( '遍历法:' ,method2(a)) |
4.输出质数
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
|
def prime(end): prime_list = [] if end < = 1 : print ( '必须大于1' ) else : # prime_list.append(2) for i in range ( 2 , end + 1 , 1 ): count = 0 if i = = 2 : if i % 2 ! = 0 : prime_list.append( 2 ) else : for m in range ( 2 , i): # 能够整除,则跳出循环 if (i % m) = = 0 : # print(i, m) break # 否则计数+1 else : count + = 1 # 判断是否整除完成(0/n) if count = = i - 2 : prime_list.append(i) print (count, i, m) return (prime_list) num = int ( input ( '想输出2到多少?' )) print (prime(num)) |
5.判断是一年中第几天
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
def que3(): # 3.输入某年某月某日,判断这一天是这一年的第几天?: # 闰年判断函数 def judge_leap(num): date = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] # (四年一闰 and not百年) or 四百年闰 if (num % 4 = = 0 and num % 100 ! = 0 ) or num % 400 = = 0 : date[ 1 ] = 29 return date # 格式转换 date = ( input ( '请输入一个日期,格式如:“2018.02.12”:' )) date_list = ( list ( map ( int , (date.split( '.' ))))) # 遍历计算天数 day = date_list[ 2 ] for i in range (date_list[ 1 ]): day + = judge_leap(date_list[ 0 ])[i] print ( '{}月{}日是{}年的第{}天\n' . format (date_list[ 1 ], date_list[ 2 ], date_list[ 0 ], day)) # que3() |
6.猜数字
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
|
# 重新猜数字 import random def judge_num(num, num_random): if num > num_random: print ( 'It\'s too big' ) return 1 elif num < num_random: print ( 'It\'s too small' ) return 1 else : print ( "Congratulation!! That\' right!" ) return 0 # 产生随机数 num_start = int ( input ( 'Digital lower limit of guess number:\n' )) num_end = int ( input ( 'Digital upper limit of guess number:\n' )) num_random = random.randint(num_start, num_end) # 参数初始化 result = 1 # 判断结果 i = 0 # 循环次数 frequency = 3 # 循环限制次数 # 提示总猜测次数、剩余次数 print ( 'WARNING: You have【{}】 chances you guess ' . format (frequency), end = '--&&>>--' ) print ( '【{}】 chances left now:\n' . format (frequency - i + 1 )) while result and i ! = frequency: # 猜数字 num = int ( input ( 'Please guess a int_number:\n' )) result = judge_num(num, num_random) i + = 1 |
7.进制转换
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
57
58
59
60
61
62
63
64
65
|
# 任意进制转十进制 def other_to_decimal( hex , num): # 整型转化为列表, num_str = str (num) # map()将List对象中的元素(list类型)转化为集合(set)类型 num_list = list ( map ( int , num_str)) # 列表反序 num_list = num_list[:: - 1 ] print ( list ( map ( int , num_str))) # 获取数字位数 digit = len (num_list) num_decimal = 0 # 累加 for i in range (digit): numi = num_list[i] # print(numi, hex**i) num_decimal + = numi * ( hex * * i) # 对每一位数的幂指数累加 return num_decimal # 十进制转任意进制 def decimal_to_other( hex , num): # 获取数字位数 digit = len ( str (num)) num_hex = [] quotient = 1 # 相除,余数计入列表num_hex while quotient: # 取余和取商 quotient = num / / hex remainder = num % hex # print(quotient, remainder) # 余数计入列表 num_hex.append(remainder) # 商做下一次循环 num = quotient # 列表反序,通过切片和sort()函数可以实现 num_hex = num_hex[:: - 1 ] # num_hex.sort(reverse=True) # 如果超过十进制,用ASCII码转化为字母 for i in range ( len (num_hex)): if num_hex[i] > 9 : num_hex[i] = chr ( int (num_hex[i]) + 87 ) # print(num_hex) # 列表转化为字符串 result = (' '.join(' % s' % m for m in num_hex)) return result Type = bool ( input ( "十进制转任意进制请输入1,任意进制转十进制请输入0\n" )) if Type : hex = int ( input ( "需要把十进制转换为多少进制?请输入正整数\n" )) num = int ( input ( "需要转换的数字是:" )) print ( "换算结果是:" , decimal_to_other( hex , num)) else : hex = int ( input ( "需要把多少进制转换为十进制?请输入正整数\n年" )) num = int ( input ( "需要转换的数字是:" )) print ( "换算结果是:" , other_to_decimal( hex , num)) |
到此这篇关于 7个关于Python的经典基础案例的文章就介绍到这了,更多相关Python 经典案例内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/python960410445/p/14964979.html