".join() 方法:
#使用 join() 方法合并字符串时,它会将列表(或元组)中多个字符串采用固定的分隔符连接在一起。
#join() 方法也是非常重要的字符串方法,它是 split() 方法的逆方法,用来将列表(或元组)中包含的多个字符串连接成一个字符串。
a = [1,2,3,4]
print(' '.join(map(str,a))) #1 2 3 4
str = ['www','ccc','aaa']
print('/n'.join(str))
#www
#ccc
#aaa
a='-'
b='helloword'
print(a.join(b))
#h-e-l-l-o-w-o-r-d
- 小数问题:
print("我喜欢%s,%s天%.2f时"%('你',32,125.158))
print('我喜欢{},{:d}天{:.2f}时'.format('你',32,123.158))
print('{:0>5}'.format('33'))#一共填充5个数字,前4个为0
#我喜欢你,32天125.16时
#我喜欢你,32天123.16时
#00033
#%s 字符串 {:s} {:#x}.format(18)#将18保存为16进制的格式
#%d 整数 {:d}
#%.2f 浮点数字(用小数点符号,4舍5入) {:.2f}
#%.2e 浮点数字(科学计数法) {:.2e}
#求1+1/2+1/4+1/8+⋯1/2**19的和
from fractions import Fraction#插入分数模块
sum = 1
for i in range(1,20):
#输入分数时,Fraction开头字母必须大写,否则会报错。
sum+=Fraction(1,2**i)#表示1/2+......+1/2^19
print(sum)#1048575/524288
- 常用变换:
bin()#转化成二进制
int()#转化为十进制
oct()#转化为八进制
hex()#转化为十六进制
round(number, digits) # 对数字number四舍五入保留digits位小数,四舍,五不进,六入
S.lower() # 小写
S.upper() # 大写
S.swapcase() #大小写互换
S.capitalize() # 只有首字母大写 L love you
S.title() # 只要遇到空格都会大写 L Love You
- 常用函数
:
#string类
S.find('a', 0,3)#在0-2的索引中找,只返回首次出现的位置
S.rfind('a',0,3)#从右侧开始查找,返回找到的第一个的位置
S.count(substr, start, end) # 计算substr在S中出现的次数
S.replace(oldstr, newstr, count)#把S中的oldstar替换为newstr,count为替换次数
S.strip(chars)#移除指定的chars字符
#以上均需要重新赋值,不更改原来的,ss = s.strip('a')
#list类
l.insert(index,newitem)#在索引为index的地方添加新的元素
l.extend(seq)#在列表末尾添加多个元素(seq)
del l[index]#删除索引为index的元素
l.pop(index)#删除索引为index的元素,默认为最后一个元素
l.remove(item)#删除列表元素item
l.clear()#删除列表所有元素
del lt#列表删除
l2=l.copy()#浅复制,l2改变l不变
l2=l#列表复制,l2改变l改变
l.index(item):返回元素item在列表中的索引
filter(function, iterable)#第一个为函数,第二个为序列,过滤掉不符合条件的元素
ret = filter(lambda x: x % 2 == 0, range(10))
print(list(ret))
#[0, 2, 4, 6, 8]
list = [('d',3),('a',5),('d',1),('c',2),('d',2)]
sorted(list,key=lambda x:x[1])#按照元组的第二个元素对列表进行排序
#[('d', 1), ('c', 2), ('d', 2), ('d', 3), ('a', 5)]
sorted(list, key = lambda x:(x[0],x[1]))#先按照元组的第一列进行排序,然后在第一列的基础按照元组的第二列进行排序
#[('a', 5), ('c', 2), ('d', 1), ('d', 2), ('d', 3)]
map(lambda x: x*x , [1, 2, 3, 4, 5])#当seq只有一个时,把函数func作用于seq的每个元素上,得到一个新的seq
#[1, 4, 9, 10, 25]
t=map(lambda x,y:(x**y,x+y),[1,2,3],[1,2,4])#当seq多于一个时,map可以并行地对每个seq执行func,也就是说,func有多个参数,用于接收相应序列的每个元素
for i in t:
print(i)
#(1, 2)
#(4, 4)
#(81, 7)
- math:
import math
math.e#自然常数 e 的值,约等于 2.71828
math.pi#圆周率 π 的值,约等于 3.14159
math.sqrt(x)#返回 x 的平方根
math.pow(x, y)#返回 x 的 y 次方
math.exp(x)#返回 e 的 x 次方
math.ceil(x)#返回不小于 x 的最小整数
math.floor(x)#返回不大于 x 的最大整数
math.trunc(x)#返回 x 的整数部分
math.modf(x)#返回 x 的小数部分和整数部分,以元组形式返回
math.fabs(x)#返回 x 的绝对值
math.factorial(x)#返回 x 的阶乘
math.gcd(a, b)#返回 a 和 b 的最大公约数
a*b//gcd(a,b)#返回a,b的最小公倍数
- datetime:
import datetime
a = datetime.time(20,20,20,10000)#返回时间格式
#20:20:20:10000
a = datetime.datetime(2023,4,18,15,16,20)
print(a)
#2023-04-18 15:16:20
delta = datetime.timedelta(days=5,minutes=23,seconds=37)
b = a + delta
print(b)
#2023-04-23 15:39:57
- queue:
import queue
q = queue.Queue()
q.put(1)#向队列中添加元素
q.put(2)
print(q.get()) # 输出1 get()从队列中获取元素
print(q.get()) # 输出2
q.qsize() # 返回队列长度
q.empty() # 判断队列是否为空
q = queue.deque() # 创建双端队列
q.append() # 入队,从队尾插入
q.appendleft() # 入队,从队头插入
q.pop() # 出队,从队尾删除,并返回该值
q.popleft() # 出队,从队头删除,并返回该值
- itertools:
import itertools
permutations = itertools.permutations([1, 2, 3], 2)
#此函数来生成一个序列的全排列。这个函数接受两个参数,第一个参数是需要排列的序列,第二个参数是排列的长度,默认为序列的长度
for p in permutations:
print(p)
#(1, 2)
#(1, 3)
#(2, 1)
#(2, 3)
#(3, 1)
#(3, 2)
itertools.islice(iterable, start, stop[, step])
#iterable:可迭代对象,如列表、元组、字符串等。
#start:切片的起始索引。
#stop:切片的结束索引,切片生成的元素不包含该索引对应的元素。
#step(可选):切片的步长,默认为 1,表示连续的元素。
c = itertools.islice([1,2,3,5,6,7], 0, 6, 2)
for i in c:
print(i)
#1
#3
#6
- 哈希表与字典:
s = [1, 2, 3, 4, 5]
x = enumerate(s)
for index,value in x:
print(index,value)
# 0,1
# 1,2
# 2,3
# 3,4
# 4,5
s = [1, 2, 3, 4, 5]
# 索引从1开始
for index,value in enumerate(s, 1):
print("%s,%s" %(index,value))
# 1,1
# 2,2
# 3,3
# 4,4
# 5,5
d = {key1 : value1, key2 : value2, key3 : value3 }
print(d[key1])# values1
del d[key2] # 删除键key2
d.clear() # 清空字典
del d # 删除字典
len(dict)#计算字典元素个数,即键的总数
dict.get(key[, value])
print ("key1 : ", d.get(key1))#value1
d={1:[1,10,199],3:[2,9,16]}
d.items()#返回字典中的键值对列表
#dict_items([(1, [1, 10, 199]), (3, [2, 9, 16])])
for o in d.items():
ids, ts_ = (i for i in o) # ids是1,3...号, ts_是[1,10,199],[2,9,16]
ts_ = sorted(ts_)
print(ids,ts_)#1 [1,10,199] 3 [2,9,16]