line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false' uname, *fields, homedir, sh = line.split(':') print(uname) print(homedir) print(sh)
甚至还可以用于递归:
1 2 3 4 5 6 7
items = [1, 10, 7, 4, 5, 9]
defsum(items): head, *tail = items return head + sum(tail) if tail else head
print(sum(items))
迭代解压语法:
解压不确定个数或任意个数元素的可迭代对象
理论上来说可以和切片互换
经常==用它来快速截取需要的元素==
1.3 保留最后 N 个元素
保留有限历史记录使用 collections.deque
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from collections import deque
defsearch(lines, pattern, history=5): previous_lines = deque(maxlen=history) for line in lines: if pattern in line: yield line, previous_lines previous_lines.append(line)
# Example use on a file if __name__ == '__main__': withopen('somefile.txt') as f: for line, prevlines in search(f, 'python', 5): for pline in prevlines: print(pline, end='') print(line, end='') print('-'*20)
print("Should be bar:", q.pop()) print("Should be spam:", q.pop()) print("Should be foo:", q.pop()) print("Should be grok:", q.pop()) # Should be bar: Item('bar') # Should be spam: Item('spam') # Should be foo: Item('foo') # Should be grok: Item('grok')
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 each in groupby(rows,key=lambda x:x['date']): print(each) # ('07/01/2012', <itertools._grouper object at 0x0000021DAEBFDB38>) # ('07/04/2012', <itertools._grouper object at 0x0000021DAEBFDBA8>) # ('07/02/2012', <itertools._grouper object at 0x0000021DAEBFDB70>) # ('07/03/2012', <itertools._grouper object at 0x0000021DAEBFDB38>) # ('07/02/2012', <itertools._grouper object at 0x0000021DAEBFDBA8>) # ('07/01/2012', <itertools._grouper object at 0x0000021DAEBFDB70>) # ('07/04/2012', <itertools._grouper object at 0x0000021DAEBFDB38>)
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'}, ]
d = defaultdict(list)
for row in rows: d[row['date']].append(row['address'])
print(d) # defaultdict(<class 'list'>, {'07/01/2012': ['5412 N CLARK', '4801 N BROADWAY'], '07/04/2012': ['5148 N CLARK', '1039 W GRANVILLE'], '07/02/2012': ['5800 E 58TH', '5645 N RAVENSWOOD', '1060 W ADDISON'], '07/03/2012': ['2122 N CLARK']})
1.16 过滤序列元素
列表解析
1 2 3 4
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
new_list = [num for num in mylist if num > 0] print(new_list) # [1, 4, 10, 2, 3]
addresses = [ '5412 N CLARK', '5148 N CLARK', '5800 E 58TH', '2122 N CLARK', '5645 N RAVENSWOOD', '1060 W ADDISON', '4801 N BROADWAY', '1039 W GRANVILLE', ] counts = [ 0, 3, 10, 4, 1, 7, 6, 1]
more5 = [n > 5for n in counts] print(more5) # [False, False, True, False, False, True, True, False]
print(list(compress(addresses, more5))) # ['5800 E 58TH', '1060 W ADDISON', '4801 N BROADWAY']
files = os.listdir('dirname') ifany(name.endswith('.py') for name in files): print('There be python!') else: print('Sorry, no python.')
1 2 3
# Output a tuple as CSV s = ('ACME', 50, 123.45) print(','.join(str(x) for x in s))
1 2 3 4 5 6 7 8 9
# Data reduction across fields of a data structure portfolio = [ {'name':'GOOG', 'shares': 50}, {'name':'YHOO', 'shares': 75}, {'name':'AOL', 'shares': 20}, {'name':'SCOX', 'shares': 65} ]