>>> gen = (i for i in 'abcd')
>>> gen
<generator object <genexpr> at 0xb7375374>
>>> next(gen)
'a'
>>> next(gen)
'b'
>>> next(gen)
'c'
>>> next(gen)
'd'
>>> next(gen)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> next(gen)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> list(i for i in 'abc')
['a', 'b', 'c']
>>> list(i, j for i in 'abc')
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument
>>>
>>> tuple(i for i in 'abc')
('a', 'b', 'c')
>>>
На вход приходит как кириллица так и латиница.
Пробовал # -*- coding: utf-8 -*- и # -*- coding: cp1251 -*-
>>> def f(lst, func):
... for i in range(len(lst)):
... if isinstance(lst[i], list):
... f(lst[i], func)
... else:
... lst[i] = func(lst[i])
... return lst
...
>>> lst = [1, [2, 3], [4, [5, 6]]]
>>>
>>> lst = f(lst, lambda x: 2 * x + 1)
>>> lst
[3, [5, 7], [9, [11, 13]]]
>>>
>>> class Human:
...
... def __init__(self, age, name):
... self.age = age
... self.name = name
...
>>> lst = [Human(20, 'aaa'),
... Human(25, 'fff'),
... Human(30, 'ggg'),
... Human(35, 'jjj'),
... Human(40, 'zzz')]
>>>
>>> out = [i for i in lst if 'f' <= i.name[0] <= 'l']
>>> out
[<__main__.Human object at 0xb740d50c>, <__main__.Human object at 0xb740d58c>, <__main__.Human object at 0xb740d5ac>]
>>>
>>> [i.name for i in out]
['fff', 'ggg', 'jjj']
>>>
>>> out = filter(lambda i: 'f' <= i.name[0] <= 'l', lst)
>>> out
<filter object at 0xb740d68c>
>>>
>>> [i.name for i in out]
['fff', 'ggg', 'jjj']
>>>
if __name__ == '__main__':
>>> def f(n):
... for i in range(n):
... for j in range(n):
... if i == j or n - 1 - i == j:
... print(' 1', end='')
... else:
... print(' 0', end='')
... if j == n - 1:
... print()
...
>>> f(5)
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
>>> f(11)
1 0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 1
>>>
На мой субъективный взгляд это не выглядит слишком сложным.
>>> import xml.etree.ElementTree as etree
>>> import csv
>>> import sys
>>>
>>> text = """\
... <Root>
... <SubRoot>
... <Level1>A</Level1>
... <Level2>B</Level2>
... <Level3>C</Level3>
... <Level4>D</Level4>
... </SubRoot>
...
... <SubRoot>
... <Level1>1</Level1>
... <Level2>2</Level2>
... <Level3>3</Level3>
... </SubRoot>
... </Root>
... """
>>>
>>> root = etree.fromstring(text)
>>>
>>> out = [[i.text for i in node] for node in root]
>>> out
[['A', 'B', 'C', 'D'], ['1', '2', '3']]
>>>
>>> out = [(i + [None] * (4 - len(i))) for i in out]
>>> out
[['A', 'B', 'C', 'D'], ['1', '2', '3', None]]
>>>
>>> writer = csv.writer(sys.stdout)
>>> writer.writerows(out)
A,B,C,D
1,2,3,
>>>