python学习3常用推导式
#!/usr/bin/python3 # -*- coding: UTF-8 -*- # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 while b < 10: print(b, end=',') a, b = b, a+b print(end='//') # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 n = 10 a, b = 0, 1 for i in range(n): print(b, end=',') a, b = b, a + b print(end='//') # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 while b < 1000: print(b, end=',') a, b = b, a+b ''' 列表(list)推导式 字典(dict)推导式 集合(set)推导式 元组(tuple)推导式 ''' multiples = [i for i in range(30) if i % 3 == 0] print(multiples) [0, 3, 6, 9, 12, 15, 18, 21, 24, 27] """ 列表(list)推导式 """ multiples = [i for i in range(30) if i % 3 == 0] print(multiples) ''' 字典(dict)推导式 ''' dic = {x: x**2 for x in (2, 4, 6)} print(dic) """ 集合(set)推导式 """ a = {x for x in 'abracadabra' if x not in 'abc'} print(a) """ 元组(tuple)推导式 """ a = (x for x in range(1,10)) print(tuple(a))