[x**2 for x in range(10)]
[x for x in vec if x >= 0]
[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[num for elem in vec for num in elem]
[weapon.strip() for weapon in freshfruit]
[(x, x**2) for x in range(6)]
[str(round(pi, i)) for i in range(1, 6)]
[[row[i] for row in matrix] for i in range(4)]
list(zip(*matrix))
def read_file_line(file_name):
"""读取文件并返回文件的每一行.
@param file_name:文件的绝对路径.
@return: yiled line.
"""
with open(file_name) as fread:
for line in fread:
yield line
User = collections.namedtuple('User', ['name', 'age'])
user = User('张三','18')
print(user.name)
print(user.age)
return user
import itertools as it
print(list(it.combinations('1245',3)))
print(list(it.permutations('124',2)))
print(list(it.product([1,2,3],repeat=2)))
from operator import itemgetter
rows = [
{'address': '5412 N CLARK', 'date': '07/01/2012'},
{'address': '5148 N CLARK', 'date': '07/04/2012'},
{'address': '5800 E 58TH', 'date': '07/02/2012'},
{'address': '2122 N CLARK', 'date': '07/03/2012'},
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
{'address': '1060 W ADDISON', 'date': '07/02/2012'},
{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
]
for date,items in it.groupby(rows,key=itemgetter('date')):
print(date)
for i in items:
print(" ",i)
|