首先看看pyiter库简单的类型推断: 列表a放入it()后程序可判断a的类型list(int)然后给出匹配的方法: 匹配后的方法可以找到相应的demo,无需查看api文档 我们来看一下最后用associate方法的结果:
{'1': 1, '2': 2, '3': 3}
接下来看一下map的用法:
from pyiter import iterate as it
from tqdm import tqdm
text = ["hello", "world"]
text_upper = []
for i in text:
text_upper.append(i.upper())
print(text_upper)
text_upper1 = it(text).map(str.upper).to_list()
text_upper2 = it(text).map(lambda x: x.upper()).to_list()
print(text_upper1)
print(text_upper2)
it(range(10)).map(lambda x: str(x)).progress(lambda x: tqdm(x, total=x.len)).parallel_map(lambda x: x, max_workers=5).to_list()
result:
['HELLO', 'WORLD']
['HELLO', 'WORLD']
['HELLO', 'WORLD']
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:00<00:00, 3342.07it/s]
接下来看一下filter的用法:
A =['aaa','cc','ac']
print(list(filter(lambda x:x.startswith('a'),A)))
print([A[2]])
from pyiter import iterate as it
B = it(A).filter(lambda x:x.startswith('a')).to_list()
C = it(A).filter_indexed(lambda x,y:y==2).to_list()
print(B)
print(C)
D = ['a',1,'b']
print(list(filter(lambda x:isinstance(x,int),D)))
D1 = it(D).filter(lambda x:isinstance(x,int)).to_list()
print(D1)
print(list(filter(lambda x:not isinstance(x,int),D)))
D2 = it(D).filter(lambda x:not isinstance(x,int)).to_list()
print(D2)
result:
['aaa', 'ac']
['ac']
['aaa', 'ac']
['ac']
[1]
[1]
['a', 'b']
['a', 'b']
字典中map和find用法
dict = [{ 'name': 'A', 'age': 12}, { 'name': 'B', 'age': 13}]
a =[]
for i in dict:
if 'age' in i:
a.append(i['age'])
print(a)
b = [i['age'] for i in dict if 'age' in i]
print(b)
from pyiter import iterate as it
c = it(dict).map(lambda x:x['age'])
print(c)
d = it(dict).map_indexed(lambda x,i:x['age']+i)
print(d)
e = ['a','b','c']
f = it(e).filter(lambda x:x=='b').to_list()
print(f)
f1 = it(e).find(lambda x:x=='e')
print(f1)
e1 = ['a','bcd','c']
g = it(e1).find_last(lambda x:x=='c')
print(g)
12, 13]
[12, 13]
[12, 13]
[12, 14]
['b']
None
c
注: pyiter的api文档:https://pyiter.yish.org/pyiter/sequence.html#iterate python类型推断的地址:https://github.com/python/typeshed pyiter的github地址:https://github.com/mokeyish/pyiter
类型推断的更多呈现:
|