つまり、開発者read the documentationが存在し、各DF.iteritems()
要素が2つのオブジェクトを持つ繰り返し可能な要素であることがわかったため、投稿した内容が正しく動作します。
あなたはまた、単一変数にそのようなオブジェクトを割り当てることができますだけfor
ループ作業中の標的(S)ため
for name_and_col in DF.iteritems():
i, col = name_and_col
:
for name_and_col in DF.iteritems():
print(name_and_col)
又はその後そのタプルをアンパック通常の課題と同じです。
Python自体はここで何も知る必要はありません。プログラマは、繰り返しの各項目を2つの名前に解凍するように指示します。 2つ以上の要素がある場合、または1つだけ存在する場合、または項目が反復可能でない場合、Pythonは例外をスローします。
代わりにデータフレームの、あなただけでプレイするために、独自のリストを作成できます。
>>> demo = [
... ('foo', 42),
... ('bar', 'Eric Idle'),
... ('spam', 3.145)
... ]
>>> for first, second in demo:
... print(first)
... print(second)
... print()
...
foo
42
bar
Eric Idle
spam
3.145
demo
リスト内の各要素は、2つの値を持つタプルそのものであるため、これは働いていました。我々はより多くの我々は、エラーを取得したい解凍しようとした場合
>>> for both in demo:
... print(both)
...
('foo', 42)
('bar', 'Eric Idle')
('spam', 3.145)
しかし:私たちは解凍しないことを選択することができ
>>> for first, second, third in demo: # won't work, not enough elements
... print(first, second, third)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
か、我々は何か他のものを解凍しようとした場合、我々はまた、取得したいがエラー:
>>> list_of_integers = [42, 81, 117]
>>> for first, second in list_of_integers:
... print(first, second)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
ありがとうMartijin –
助けてくれてうれしいです!あなたがそれが役に立ったと思ったら、[私の答えを受け入れる](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)を自由に感じてください。 :-) –