In [1]:
p = ['a',1,3,'c',('a1','a2')]
In [4]:
a = []
for i in range(len(p)):
a.append(p[i])
print('a[%s]:%s' % (i,a[i]))
In [5]:
a,b,*c = p
In [6]:
c
Out[6]:
In [20]:
record = [
('foo', 1, 2),
('bar', 'hello'),
('foo', 3, 4),
]
In [8]:
type(record)
Out[8]:
In [10]:
def do_foo(x,y):
print('foo',x,y)
In [14]:
def do_bar(s):
print('bar',s)
In [22]:
for tag,*arg in record:
'''
if tag == 'foo':
do_foo(*arg)
'''
if tag == 'bar':
do_bar(*arg)
In [23]:
line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false'
In [25]:
name,*fields,sh = line.split(':')
In [26]:
name
Out[26]:
In [27]:
fields
Out[27]:
In [28]:
sh
Out[28]:
In [29]:
jiji = ['ac',12.34,(1,2,3,4)]
In [30]:
_,a,(*ign,need) = jiji
In [31]:
need
Out[31]:
In [32]:
ign
Out[32]:
In [33]:
riri = [1,5,6,7,8,9,10]
head,*tail = riri
head
Out[33]:
In [34]:
tail
Out[34]:
In [37]:
def ririsum(riri):
head,*tail = riri
return head + ririsum(tail) if tail else head
In [38]:
ririsum(riri)
Out[38]:
In [39]:
sum(riri)
Out[39]:
In [47]:
from collections import deque
def search(lines,pattern,history=2):
previous_lines = deque(maxlen=history)
for li in lines:
if pattern in li:
yield li,previous_lines
previous_lines.append(li)
# Example use on a file
if __name__ == '__main__':
with open(r'somefile.txt') as f:
for line, prevlines in search(f, 'python', 2):
for pline in prevlines:
print(pline, end=',')
#print(line, end='')
print('-' * 20)
In [49]:
d = deque()
In [50]:
d.append((1,2,3))
In [51]:
d
Out[51]:
In [52]:
d.append(1)
d
Out[52]:
In [53]:
d.appendleft(1)
In [54]:
d
Out[54]:
In [55]:
d.pop()
Out[55]:
In [56]:
d
Out[56]:
In [57]:
d.popleft()
Out[57]:
In [58]:
d
Out[58]: