1
一個Python 3學習者在這裏解釋蟒蛇拉鍊和拆包的解決方案:請
的question有以下接受的答案:
rr,tt = zip(*[(i*10, i*12) for i in xrange(4)])
返回兩個元。如果有人能夠分解答案並解釋Python 3在做什麼(我知道range()
在Python 3中返回一個迭代器),我將不勝感激。我理解列表解析,但我對解包感到困惑(我以爲你只能使用星號表達式作爲賦值目標的一部分)。
我同樣被下面的代碼弄糊塗了。我理解結果和壓縮(或者認爲我是這樣),但是星號表達式再次讓我感動。
x2, y2 = zip(*zip(x, y))
從this:
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True
非常感謝你。你的回答非常明確。 – Disnami