*args的用法
def test_var_args(f_arg, *args):
print("f_arg:"+f_arg)
for a in args:
print("args:"+a)
if __name__ == '__main__':
test_var_args("a","b","v","2")
def greet_me(**kwargs):
for key, value in kwargs.items():
print("{0} == {1}".format(key, value))
if __name__ == '__main__':
greet_me(name = "yasob")
def test_args_kwargs(arg1, arg2, arg3):
print("arg1:", arg1)
print("arg2:", arg2)
print("arg3:", arg3)
agrs = {"1","2","3"}
test_args_kwargs(*agrs)
kwargs = {"arg1":1,"arg2":2,"arg3":3}
test_args_kwargs(**kwargs)
那么如果你想在函数?同时使?所有这三种参数, 顺序是这样的: some_func(fargs, *args, **kwargs)
生成器 Generators
可迭代对象(Iterable)
?个迭代器是任意?个对象,只要它定义了?个next(Python2) 或者__next__?法。就 这么简单。
迭代(Iteration)
?简单的话讲,它就是从某个地?(?如?个列表)取出?个元素的过程。当我们使?? 个循环来遍历某个东西时,这就叫?个迭代。
'''
生成器就是生成的数不会在内存中 只是生成了还没有用他 等用的时候才会放 节省资源 大多数生成器以函数形式出现
'''
def generator_function():
for i in range(1,10):
yield i
gen = generator_function()
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
for item in generator_function():
print(item)
'''
下面是一个计算斐波那契数列的生成器:
'''
def fibon(n):
a = b = 1
for i in range(n):
yield a
a, b = b, a+b
for x in fibon(10000):
print(x)
my_string = "Yasoob"
next(my_string)
my_string = "Yasoob"
my_iter = iter(my_string)
next(my_iter)
Map , Filter, Set
'''
Map 和 Filter这两个函数能为函数式编程提供便利
'''
def multiply(x):
return x * x
def add(x):
return x + x
funs = [multiply, add]
for i in range(5):
value = list(map(lambda x: x(i), funs))
print(value)
number_list = [1, 2, 3, 4, 5, -9, -7, -56, -4]
result = list(filter(lambda x: x > 0, number_list))
print(result)
some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
duplicates = []
for value in some_list:
if some_list.count(value) > 1:
if value not in duplicates:
duplicates.append(value)
print(duplicates)
'''
更优雅的解决方案 set无序
'''
some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
duplicates = set([x for x in some_list if some_list.count(x) > 1])
print(duplicates)
valid = set(['yellow', 'red', 'blue', 'green', 'black'])
input_set = set(['red', 'brown'])
print(input_set.intersection(valid))
valid = set(['yellow', 'red', 'blue', 'green', 'black'])
input_set = set(['red', 'brown'])
print(input_set.difference(valid))
三元运算符
is_fat = True
state = "fat" if is_fat else "not fat"
print(state)
枚举
some_list = ['apple', 'banana', 'grapes', 'pear']
for index, value in enumerate(some_list):
print(index, value)
my_list = ['apple', 'banana', 'grapes', 'pear']
counter_list = list(enumerate(my_list, 1))
print(counter_list)
列表推导式 字典推导式 集合推导式
multiples = [i for i in range(50) if i % 3 == 0]
print(multiples)
squared = []
for x in range(10):
squared.append(x ** 2)
squared1 = [x ** 2 for x in range(10)]
mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
mcase_frequency = {k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0) for k in
mcase.keys()}
print(mcase_frequency)
squared = {x ** 2 for x in [1, 1, 2]}
print(squared)
处理多个异常
try:
file = open('test.txt', 'rb')
except Exception:
raise
|