sorted() 对所有可迭代的对象进行排序操作,返回重新排序的列表。
sorted(iterable, key=None, reverse=False)
a = [5, 2, 3, 1, 4]
x1 = sorted(a)
# [1, 2, 3, 4, 5] # 默认为升序
x2 = sorted(a, reverse=True)
# [5, 4, 3, 2, 1]
实例,对奖牌榜进行排序
import numpy as np
s = "德国 10 11 16\n意大利 10 10 20\n荷兰 10 12 14\n法国 10 12 11\n英国 22 21 22\n中国 38 32 18\n日本 27 14 17\n美国 39 41 33\n俄罗斯奥委会 20 28 23\n澳大利亚 17 7 22\n匈牙利 6 7 7\n加拿大 7 6 11\n古巴 7 3 5\n巴西 7 6 8\n新西兰 7 6 7"
stodata = s.split('\n', -1) # 分割数组
# 使用sorted
para = {}
for line in range(len(stodata)):
# 每一行数据
data = stodata[line].split(' ') # 分割数组
print(data)
# 把奖牌数重新组合为一个数组,组装数据结构para={'China': [], 'Russia': []}
para[data[0]] = [int(i) for i in data[1:]]
# 开始排序(x[1]代表奖牌数目, x[0]代表国家),以奖牌总数降序排列
new_para = sorted(para.items(), key=lambda x: (x[1], x[0]), reverse=True) # items() 历遍数组
# 分割线
print('\n', np.char.center('分割线', 50, fillchar='*'), '\n')
c = [] # 排序后的国家名数组
for i in new_para:
c.append((i[0]))
for j in range(15):
print(f'{j+1} {c[j]}')
print() 用于打印输出。
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
, 分隔。\n,我们可以换成其他字符串。print('aaa''bbb')
# aaabbb
print('aaa', 'bbb')
# aaa bbb
print('aaa', 'bbb', sep='.') # 设置间隔符
# aaa.bbb
print() 函数常用用法总结1python 字符串格式化符号(部分)
| 符 号 | 描述 |
|---|---|
| %s | 格式化字符串 |
| %d | 格式化整数 |
| %u | 格式化无符号整型 |
| %f | 格式化浮点数字,可指定小数点后的精度 |
格式化操作符辅助指令(部分)
| 符号 | 功能 |
|---|---|
| * | 定义宽度或者小数点精度 |
| - | 用做左对齐 |
| + | 在正数前面显示加号 + |
| 0 | 显示的数字前面填充 0 而不是默认的空格 |
| (var) | 映射变量(字典参数) |
| m.n. | m 是显示的最小总宽度, n 是小数点后的位数(如果可用的话) |
print('the length of (%s) is %d' % ('runoob', len('runoob')))
# the length of(runoob) is 6
pi = 3.141592653
print('%10.3f' % pi) # 字段宽10,精度3
# 3.142
print('pi = %.*f' % (3, pi)) # 用*从后面的元组中读取字段宽度或精度
print('pi = %.3f' % pi) # 同上
# pi = 3.142
print('%010.3f' % pi) # 用0填充空白
# 000003.142
print('%-10.3f' % pi) # 左对齐,默认右对齐
# 3.142
print('%+f' % pi) #显示正负号
# +3.141593
print() 会自动在行末加上回车,如果不需回车,需设置分隔符 end。
for i in range(0, 3):
print(i)
# 0
# 1
# 2
for j in range(0, 3):
print(j, end=' ')
# 0 1 2
{} 输出变量和字符串2first_name = "John"
last_name = "Doe"
print("Hello {} {}, hope you're well!".format(last_name, first_name))
# Hello Doe John, hope you're well!
f-strings 输出变量和字符串2与上面方法相比,f-strings 是一种更简洁的字符串格式化方法。
在引号前加入字符 f ,在要添加变量值的地方添加一组大括号,里面是变量名。
first_name = "John"
last_name = "Doe"
print(f"Hello, {first_name}!")
# Hello, John!
print(f"Hello, {first_name} {last_name}!")
# Hello, John Doe!
format() 3Python2.6 开始,新增了一种格式化字符串的函数 str.format() ,基本语法是通过 {} 和 : 来代替以前的 % 。
format() 函数可以接受不限个数参数,位置可以不按顺序。
print("{} {}".format("hello", "world")) # 不设置指定位置,按默认顺序
# hello world
print("{0} {1}".format("hello", "world")) # 设置指定位置
# hello world
print("{1} {0} {1}".format("hello", "world")) # 设置指定位置
# world hello world
也可以设置参数
print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
# 网站名:菜鸟教程, 地址 www.runoob.com
# 通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site)) # **代表收集关键字参数
# 通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
print("{:.2f}".format(3.1415926))
# 3.14
| 数字 | 格式 | 输出 | 描述 |
|---|---|---|---|
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
| 2.71828 | {:.0f} | 3 | 不带小数 |
| 5 | {:0>2d} | 05 | 数字补零 (填充左边, 宽度为2) |
| 5 | {:x<4d} | 5xxx | 数字补x (填充右边, 宽度为4) |
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00e+09 | 指数记法 |
| 13 | {:>10d} | 13 | 右对齐 (默认, 宽度为10) |
| 13 | {:<10d} | 13 | 左对齐 (宽度为10) |
| 13 | {:^10d} | 13 | 中间对齐 (宽度为10) |
xxx is not callable 错误书写不规范,调用了一个错误的变量或者函数,注意不要使用内置函数名作为变量名。
import numpy as np
# 分割线
print('\n', np.char.center('分割线', 50, fillchar='*'), '\n')