Pythonのいくつかの内蔵関数とその他
5908 ワード
>>>
getattr((), "pop"
)
Traceback (innermost last):
File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'pop'
import
statsout
def
output
(data, format="text"
):
output_function = getattr(statsout, "output_%s"
% format, statsout.output_text)
return
output_function(data)
フィルタリスト構文:
[mapping-expression
for
element
in
source-list
if
filter-expression
]
>>> li = ["a","dds","vgf","b","c","b"] >>> [elem for elem in li if len(elem)>1] ['dds', 'vgf'] >>>>>> [elem for elem in li if li.count(elem)==1] ['a', 'dds', 'vgf', 'c'] >>>
- >>> a = lambda x, y= 2 : x + y
- >>> a(3 )
- 5
- >>> a(3 , 5 )
- 8
- >>> a(0 )
- 2
- >>> a(0 , 9 )
- 9
- >>>
- >>> b = lambda *z: z
- >>> b(23 , 'zyx' )
- (23 , 'zyx' )
- >>> b(42 )
- (42 ,)