sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
根据字典的value进行排序
a = {‘大王’:1,‘二王’:2,‘三王’:3}
sorted(a.items(), key= lambda:d: d[1], reverse=True)
d指代前面的可迭代对象
也叫匿名函数
f = lambda x, y: x3+y2
f(2,3)
n = input()
print("{:->20,}".format(eval(n)))
在使用千分位分隔符时格式化内容需要是整数
n = input()
nums = n.split(’,’)
s = 0
for i in nums:
s += eval(i)
print(s)
定理:两个整数的最大公约数等于其中较小的那个数和两个数相除余数的最大公约数。
证明去百度。
def GreatCommonDivisor(a,b):
if a > b:
a,b = b,a
r = 1
while r != 0:
r = a % b
a = b
b = r
return a
m = eval(input())
n = eval(input())
print(GreatCommonDivisor(m,n))
turtle.circle(rad, angle)#rad表示半径,angle表示角度,rad为正表示在海龟左侧,负在海龟右侧
import turtle
d = 0
k = 1
for j in range(10):
for i in range(4):
turtle.fd(k)
d += 91
turtle.seth(d)
k += 4
turtle.done()
import turtle
turtle.fillcolor("yellow")
turtle.begin_fill()
for i in range(12):
turtle.circle(-90,90)
turtle.right(120)
turtle.end_fill()
turtle.hideturtle()
turtle.done()
ls = [23,41,32,12,56,76,35,67,89,44]
print(ls)
def bub_sort(s_list):
for i in range(len(s_list)-1):
is_change = True
for j in range(len(s_list)-1-i):
if s_list[j] > s_list[j+1]:
s_list[j],s_list[j+1] = s_list[j+1],s_list[j]
is_change = False
if is_change:
break
return s_list
bub_sort(ls)
print(ls)
每次把前n个里面的最大的升到最后面
基础中文字符的Unicode编码范围是[0x4e00,0x9fa5],请统计给定文本中存在多少该范围内的基础中文字符以及每个字符的出现次数。以如下模式(CSV格式)保存在“侠客行-字符统计.txt”文件中。
侠(0x4fa0):888, 客(0x5ba2):666, 行(0x884c):111
(略)
fi = open("侠客行-网络版.txt", "r", encoding='utf-8')
fo = open("侠客行-字符统计.txt", "w", encoding='utf-8')
txt = fi.read()
d = {}
for c in txt:
if 0x4e00 <= ord(c) <= 0x9fa5: #ord只是把字符转为十进制的unicode编码
d[c] = d.get(c, 0) + 1
ls = []
for key in d:
ls.append("{}(0x{:x}):{}".format(key, ord(key),d[key])) #需要转为16进制 也可用hex,hex会有0x头, {:x}则无头
fo.write(",".join(ls))
fi.close()
fo.close()