* 列表排序def que6(): # 6.输入三个整数x, y, z，形成一个列表，请把这n个数由小到大输出。 #

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 #

k): # 标识符 tab = False # 寻找插入位置 #

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 #

in range(1, len(m)): # 手上已经有index_choose张牌，比较第index_choose+1张牌则append #

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()

* 调换字典键值# 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] #

result.keys(): result[k] = match(d, k)[0] return result # 方法4 --- 列表转字典 <

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. 删除重复元素list =[1,2,5,4,1,5,6,8,0,2,5] 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))

* 输出质数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))

* 打印九九乘法表# 新写九九乘法表 for i in range(1,10): for j in range(1, i+1):
print('{}×{}={}'.format(i, j, i*j),end="\t") print('\n')

* 打印实心和空心星星# 4.打印三角形 def que4(): # 实心三角形 def triangle_solid(num): for i in
range(num): tab = False for j in range(i+1): print('*',end='') if j == i: tab =
True if tab: print('\n' ,end = '') # 空心三角形 def hollow_solid(num): for i in
range(num): tab = False for j in range(i + 1): # 判断是否最后一行 if i != num-1: #

print('*',end='') else : print(' ',end='') # 最后一行，全部打印星号 else: print('*',
end='') if tab: print('\n', end='') def hollow_solid_new(num): for i in
range(num): for j in range(i + 1): # 判断是否最后一行 if i != num-1: # 判断打印空格还是* if (i
== j or j == 0): print('*',end='') else : print(' ',end='') # 最后一行，全部打印星号 else:
print('*', end='') print('\n') num = int(input('你想打印几边形？请输入\n'))
triangle_solid1(num) print('\n') hollow_solid(num) return '\n' # que4()

* 判断是一年中第几天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()

* 猜数字# 重新猜数字 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 #

= 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

* 进制转换# 任意进制转十进制 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))) #

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: #

# 余数计入列表 num_hex.append(remainder) # 商做下一次循环 num = quotient #

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))