亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: python的*args是什么意思? [打印本頁(yè)]

作者: niexining    時(shí)間: 2012-06-03 08:59
標(biāo)題: python的*args是什么意思?
  1. from itertools import izip_longest
  2. def grouper(n, iterable, fillvalue=None):
  3.     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
  4.     args = [iter(iterable)] * n
  5.     return izip_longest(fillvalue=fillvalue, *args)
  6. for i in grouper(3,'ABCEDFG','x'): print i

復(fù)制代碼
windows系統(tǒng)python2.7自帶文檔庫(kù)函數(shù)手冊(cè)中,有這樣一個(gè)例子。

請(qǐng)問(wèn)*args是怎么用的? izip_longest的參數(shù)一般有兩個(gè),一個(gè)是迭代對(duì)象(iterable),另一個(gè)是填充(filvalue)。為什么在這個(gè)例子中,參數(shù)卻是fillvalue和*args。

另外,某天有朋友問(wèn): 如何把列表3個(gè)3個(gè)地分組?     這兒似乎對(duì)這個(gè)問(wèn)題提供了一種別樣的解答。
作者: cdtits    時(shí)間: 2012-06-03 10:07
貌似指明為元組
作者: niexining    時(shí)間: 2012-06-03 15:58
本帖最后由 niexining 于 2012-06-03 16:00 編輯
  1. ('A', 'B', 'C')
  2. ('E', 'D', 'F')
  3. ('G', 'x', 'x')
復(fù)制代碼
這是運(yùn)行結(jié)果.


是不是放了3個(gè)迭代器進(jìn)去, 就3個(gè)字符3個(gè)字符地迭代?
作者: anonymous0502    時(shí)間: 2012-06-03 16:17
4.7.4. Unpacking Argument Lists
The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:

>>>
>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]
In the same fashion, dictionaries can deliver keyword arguments with the **-operator:

>>>
>>> def parrot(voltage, state='a stiff', action='voom'):
...     print "-- This parrot wouldn't", action,
...     print "if you put", voltage, "volts through it.",
...     print "E's", state, "!"
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
                           
作者: niexining    時(shí)間: 2012-06-05 16:37
回復(fù) 4# anonymous0502

這個(gè)兩個(gè)例子很好.   剩下的就是關(guān)于迭代器的還太理解.

   




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2