Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63938 > unrolled thread
| Started by | Igor Korot <ikorot01@gmail.com> |
|---|---|
| First post | 2014-01-14 13:10 -0800 |
| Last post | 2014-01-15 03:24 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
dictionary with tuples Igor Korot <ikorot01@gmail.com> - 2014-01-14 13:10 -0800
Re: dictionary with tuples YBM <ybmess@nooos.fr.invalid> - 2014-01-14 22:21 +0100
Re: dictionary with tuples Tobiah <toby@tobiah.org> - 2014-01-14 14:00 -0800
Re: dictionary with tuples emile <emile@fenx.com> - 2014-01-14 14:14 -0800
Re: dictionary with tuples YBM <ybmess@nooos.fr.invalid> - 2014-01-15 03:24 +0100
| From | Igor Korot <ikorot01@gmail.com> |
|---|---|
| Date | 2014-01-14 13:10 -0800 |
| Subject | dictionary with tuples |
| Message-ID | <mailman.5476.1389733811.18130.python-list@python.org> |
Hi, ALL,
C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dict = {}
>>> dict[(1,2)] = ('a','b')
>>> dict[(3,4)] = ('c','d')
>>> for (key1,key2),(value1,value2) in dict:
... print key1, " ", key2
... print value1, " ", value2
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>
What am I doing wrong?
Thank you.
[toc] | [next] | [standalone]
| From | YBM <ybmess@nooos.fr.invalid> |
|---|---|
| Date | 2014-01-14 22:21 +0100 |
| Message-ID | <52d5aa4c$0$2196$426a74cc@news.free.fr> |
| In reply to | #63938 |
Le 14/01/2014 22:10, Igor Korot a écrit :
> Hi, ALL,
> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> dict = {}
>>>> dict[(1,2)] = ('a','b')
>>>> dict[(3,4)] = ('c','d')
>>>> for (key1,key2),(value1,value2) in dict:
> ... print key1, " ", key2
> ... print value1, " ", value2
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
>>>>
>
> What am I doing wrong?
for ... in dict:
is a way to iterate through dictionnary items,
what you want to do can be done so:
for (key1,key2),(value1,value2) in dict.items():
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2014-01-14 14:00 -0800 |
| Message-ID | <GriBu.177658$Qi4.154313@fx11.iad> |
| In reply to | #63941 |
On 01/14/2014 01:21 PM, YBM wrote:
> Le 14/01/2014 22:10, Igor Korot a écrit :
>> Hi, ALL,
>> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
>> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
>> (Intel)] on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> dict = {}
>>>>> dict[(1,2)] = ('a','b')
>>>>> dict[(3,4)] = ('c','d')
>>>>> for (key1,key2),(value1,value2) in dict:
>> ... print key1, " ", key2
>> ... print value1, " ", value2
>> ...
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: 'int' object is not iterable
>>>>>
>>
>> What am I doing wrong?
>
> for ... in dict:
>
> is a way to iterate through dictionnary items,
>
> what you want to do can be done so:
>
> for (key1,key2),(value1,value2) in dict.items():
>
>
>
But it's (key1, value1), (key2, value2)
[toc] | [prev] | [next] | [standalone]
| From | emile <emile@fenx.com> |
|---|---|
| Date | 2014-01-14 14:14 -0800 |
| Message-ID | <mailman.5487.1389737708.18130.python-list@python.org> |
| In reply to | #63949 |
On 01/14/2014 02:00 PM, Tobiah wrote:
> On 01/14/2014 01:21 PM, YBM wrote:
>> Le 14/01/2014 22:10, Igor Korot a écrit :
>>> Hi, ALL,
>>> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
>>> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
>>> (Intel)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> dict = {}
>>>>>> dict[(1,2)] = ('a','b')
>>>>>> dict[(3,4)] = ('c','d')
>>>>>> for (key1,key2),(value1,value2) in dict:
>>> ... print key1, " ", key2
>>> ... print value1, " ", value2
>>> ...
>>> Traceback (most recent call last):
>>> File "<stdin>", line 1, in <module>
>>> TypeError: 'int' object is not iterable
>>>>>>
>>>
>>> What am I doing wrong?
>>
>> for ... in dict:
>>
>> is a way to iterate through dictionnary items,
>>
>> what you want to do can be done so:
>>
>> for (key1,key2),(value1,value2) in dict.items():
>>
>>
>>
>
> But it's (key1, value1), (key2, value2)
No it isn't. :)
Emile
[toc] | [prev] | [next] | [standalone]
| From | YBM <ybmess@nooos.fr.invalid> |
|---|---|
| Date | 2014-01-15 03:24 +0100 |
| Message-ID | <52d5f156$0$2227$426a34cc@news.free.fr> |
| In reply to | #63949 |
Le 14/01/2014 23:00, Tobiah a écrit :
> On 01/14/2014 01:21 PM, YBM wrote:
>> Le 14/01/2014 22:10, Igor Korot a écrit :
>>> Hi, ALL,
>>> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
>>> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
>>> (Intel)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> dict = {}
>>>>>> dict[(1,2)] = ('a','b')
>>>>>> dict[(3,4)] = ('c','d')
>>>>>> for (key1,key2),(value1,value2) in dict:
>>> ... print key1, " ", key2
>>> ... print value1, " ", value2
>>> ...
>>> Traceback (most recent call last):
>>> File "<stdin>", line 1, in <module>
>>> TypeError: 'int' object is not iterable
>>>>>>
>>>
>>> What am I doing wrong?
>>
>> for ... in dict:
>>
>> is a way to iterate through dictionnary items,
>>
>> what you want to do can be done so:
>>
>> for (key1,key2),(value1,value2) in dict.items():
>>
>>
>>
>
> But it's (key1, value1), (key2, value2)
No. Try it.
key1 key2 are the members of every tuple key.
value1, value2 are the members of every tuple value.
>>> d={ (1,2):('a','b'), (3,4):('c','d') }
>>> for (key1,key2),(value1,value2) in d.items():
... print key1,key2,value1,value2
...
1 2 a b
3 4 c d
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web