Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39149 > unrolled thread
| Started by | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| First post | 2013-02-18 16:52 -0800 |
| Last post | 2013-02-18 17:38 -0800 |
| Articles | 19 — 5 participants |
Back to article view | Back to comp.lang.python
Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 16:52 -0800
Re: Dictionaries with tuples or tuples of tuples Mitya Sirenef <msirenef@lightbird.net> - 2013-02-18 20:11 -0500
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 17:19 -0800
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 17:19 -0800
Re: Dictionaries with tuples or tuples of tuples Roy Smith <roy@panix.com> - 2013-02-18 20:17 -0500
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 17:20 -0800
Re: Dictionaries with tuples or tuples of tuples Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-19 01:37 +0000
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 17:38 -0800
Re: Dictionaries with tuples or tuples of tuples Dave Angel <davea@davea.name> - 2013-02-18 20:51 -0500
Re: Dictionaries with tuples or tuples of tuples Mitya Sirenef <msirenef@lightbird.net> - 2013-02-18 20:56 -0500
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 18:17 -0800
Re: Dictionaries with tuples or tuples of tuples Mitya Sirenef <msirenef@lightbird.net> - 2013-02-18 21:54 -0500
Re: Dictionaries with tuples or tuples of tuples Dave Angel <davea@davea.name> - 2013-02-18 22:14 -0500
Re: Dictionaries with tuples or tuples of tuples Mitya Sirenef <msirenef@lightbird.net> - 2013-02-18 22:56 -0500
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 18:17 -0800
Re: Dictionaries with tuples or tuples of tuples Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-19 02:34 +0000
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 18:39 -0800
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 18:39 -0800
Re: Dictionaries with tuples or tuples of tuples Jon Reyes <everystepsayes@gmail.com> - 2013-02-18 17:38 -0800
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 16:52 -0800 |
| Subject | Dictionaries with tuples or tuples of tuples |
| Message-ID | <c8abdc96-a47c-462a-9d6e-fcbaad1102fe@googlegroups.com> |
So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of tuples. Is there a better way to go about accessing the values of the dictionary? All the tuples contain four elements.
So say:
col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}
Then to access the values of the tuple I'd do this:
for key,value in col.iteritems():
if isinstance(value[0], tuple):
#iterate through the tuples of a tuple
else:
#iterate through the tuple
At first I was thinking that I could just put the same keys with just single tuples on a dictionary but only one tuple exists when I iterate through the dictionary. I'm sorry, I'm really new at Python and I just grab anything I can when I need it from Google and the Python docs.
[toc] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-02-18 20:11 -0500 |
| Message-ID | <mailman.1987.1361236273.2939.python-list@python.org> |
| In reply to | #39149 |
On 02/18/2013 07:52 PM, Jon Reyes wrote:
> So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of
tuples. Is there a better way to go about accessing the values of the
dictionary? All the tuples contain four elements.
>
> So say:
> col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}
>
> Then to access the values of the tuple I'd do this:
>
> for key,value in col.iteritems():
> if isinstance(value[0], tuple):
> #iterate through the tuples of a tuple
> else:
> #iterate through the tuple
>
> At first I was thinking that I could just put the same keys with just
single tuples on a dictionary but only one tuple exists when I iterate
through the dictionary. I'm sorry, I'm really new at Python and I just
grab anything I can when I need it from Google and the Python docs.
It would be easier to process if, when adding a single tuple
to the dict, you could wrap it inside a tuple: (mytup,)
If your data set is not very large and you don't mind the
slight performance hit, you can simplify processing step:
for k,v in col.iteritems():
if not isinstance(v[0], tuple):
v = (v,)
for tup in v: ...
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
Although the most acute judges of the witches and even the witches
themselves, were convinced of the guilt of witchery, the guilt nevertheless
was non-existent. It is thus with all guilt. Friedrich Nietzsche
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 17:19 -0800 |
| Message-ID | <4c5cfebb-165f-4cbe-910c-3127c1e62fae@googlegroups.com> |
| In reply to | #39150 |
Wow, why didn't I think of that. Thanks! I'll try it now. By the way I think I don't need to wrap the single tuples in runtime because I'm declaring that dictionary anyway beforehand and I could just do it right there. I won't be adding elements to the tuple.
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 17:19 -0800 |
| Message-ID | <mailman.1989.1361236751.2939.python-list@python.org> |
| In reply to | #39150 |
Wow, why didn't I think of that. Thanks! I'll try it now. By the way I think I don't need to wrap the single tuples in runtime because I'm declaring that dictionary anyway beforehand and I could just do it right there. I won't be adding elements to the tuple.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-02-18 20:17 -0500 |
| Message-ID | <roy-1D2064.20170318022013@news.panix.com> |
| In reply to | #39149 |
In article <c8abdc96-a47c-462a-9d6e-fcbaad1102fe@googlegroups.com>,
Jon Reyes <everystepsayes@gmail.com> wrote:
> So I have a dictionary and the key is a number.
> [...]
> col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}
The keys here are strings, not numbers. But that's a detail. Somewhat
more importantly, that's a syntax error (one of the colons should be a
comma).
> The values are either a
> single tuple or a tuple of tuples. Is there a better way to go about
> accessing the values of the dictionary? All the tuples contain four elements.
I would make all the values the same shape, i.e. all lists of tuples:
col = {"1": [(0,1,2,3)]: "2": [(0,1,2,3),(2,3,4,5)]}
Then you're always doing the same thing with values when you process
them.
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 17:20 -0800 |
| Message-ID | <96c1fcf2-de49-476f-b948-35dc62450569@googlegroups.com> |
| In reply to | #39154 |
Sorry if I didn't check the code before I posted it, I just mocked it up in Google's editor. That's what Mitya suggested too, yep, I guess I just need to make it uniform to get rid of the extra checking. Thanks man!
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-02-19 01:37 +0000 |
| Message-ID | <mailman.1990.1361237726.2939.python-list@python.org> |
| In reply to | #39149 |
On 19/02/2013 00:52, Jon Reyes wrote:
> So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of tuples. Is there a better way to go about accessing the values of the dictionary? All the tuples contain four elements.
>
> So say:
> col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}
>
> Then to access the values of the tuple I'd do this:
>
> for key,value in col.iteritems():
> if isinstance(value[0], tuple):
> #iterate through the tuples of a tuple
> else:
> #iterate through the tuple
>
> At first I was thinking that I could just put the same keys with just single tuples on a dictionary but only one tuple exists when I iterate through the dictionary. I'm sorry, I'm really new at Python and I just grab anything I can when I need it from Google and the Python docs.
>
How about this using Python 3.
col = {"1": ((0,1,2,3),), "2": ((0,1,2,3),(2,3,4,5))}
for key,tuples in col.items():
for t in tuples:
print(key,t)
A slight aside, your keys look more like strings to me than numbers :)
--
Cheers.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 17:38 -0800 |
| Message-ID | <3752f235-1542-44c8-b7c7-93dfd98eac3f@googlegroups.com> |
| In reply to | #39158 |
Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps are always Strings. PS: Just checked, wow I could use ints as keys. Awesome!
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-02-18 20:51 -0500 |
| Message-ID | <mailman.1994.1361238723.2939.python-list@python.org> |
| In reply to | #39159 |
On 02/18/2013 08:38 PM, Jon Reyes wrote: > Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps are always Strings. > > PS: Just checked, wow I could use ints as keys. Awesome! > The keys to a dictionary may be any immutable type. That includes str, int, and tuple, but it also can include any other class that meets a couple of simple criteria. In simplified language, the only requirement is that the key object cannot change its value or hash, so that if two key objects are equal, they stay equal, and if they differ, they stay different. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-02-18 20:56 -0500 |
| Message-ID | <mailman.1995.1361238995.2939.python-list@python.org> |
| In reply to | #39159 |
On 02/18/2013 08:38 PM, Jon Reyes wrote: > Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps are always Strings. > > PS: Just checked, wow I could use ints as keys. Awesome! In fact, any hashable object can be a key in a dict, so you can define your own custom objects and use them as keys -- this can be extremely useful sometimes! -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ Oaths are the fossils of piety. George Santayana
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 18:17 -0800 |
| Message-ID | <dc54beb6-3949-4ab3-9c3d-90d264faa1e2@googlegroups.com> |
| In reply to | #39159 |
Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though: " so that if two key objects are equal, they stay equal, and if they differ, they stay different. " What does this mean? I won't be comparing key objects with one another. Also, when I had two keys with the same value the value of the other key disappeared so I assume in runtime if there are multiple keys of the same value only the last one will appear.
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-02-18 21:54 -0500 |
| Message-ID | <mailman.2000.1361242467.2939.python-list@python.org> |
| In reply to | #39168 |
On 02/18/2013 09:17 PM, Jon Reyes wrote: > Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though: > > " so that if two > key objects are equal, they stay equal, and if they differ, they stay > different. " > > What does this mean? I won't be comparing key objects with one another. Also, when I had two keys with the same value the value of the other key disappeared so I assume in runtime if there are multiple keys of the same value only the last one will appear. You won't be, but dict will. Dict is by definition a mapping where a value is assigned to a unique key. If you have two keys and two values, and then change one key to be equal to the second key, that's not kosher, because which value it's supposed to return when you try to get it by that key? So in effect, key's hash value should not change. If key is immutable, you can be certain that it's hash value will not change. If it's mutable, you have to make sure not to change the key in a way that'd make its hash value different than it was. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ Graphic design is the paradise of individuality, eccentricity, heresy, abnormality, hobbies and humors. George Santayana
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-02-18 22:14 -0500 |
| Message-ID | <mailman.2002.1361243666.2939.python-list@python.org> |
| In reply to | #39168 |
On 02/18/2013 09:54 PM, Mitya Sirenef wrote: > On 02/18/2013 09:17 PM, Jon Reyes wrote: >> Thanks Dave and Mitya for enlightening me about dictionaries. I'm >> still confused about this though: > > > > " so that if two > > key objects are equal, they stay equal, and if they differ, they stay > > different. " > > > > What does this mean? I won't be comparing key objects with one > another. Also, when I had two keys with the same value the value of the > other key disappeared so I assume in runtime if there are multiple keys > of the same value only the last one will appear. > > You won't be, but dict will. > > Dict is by definition a mapping where a value is assigned to a unique > key. If you have two keys and two values, and then change one key to > be equal to the second key, that's not kosher, because which value it's > supposed to return when you try to get it by that key? > > So in effect, key's hash value should not change. If key is immutable, > you can be certain that it's hash value will not change. If it's > mutable, you have to make sure not to change the key in a way that'd > make its hash value different than it was. > > -m > It's a little stronger than that, since equal hashes cannot assure equal data. The equality of each object pair in a dict must not change over time, not just the hashes of the individual objects. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-02-18 22:56 -0500 |
| Message-ID | <mailman.2004.1361246208.2939.python-list@python.org> |
| In reply to | #39168 |
On 02/18/2013 10:14 PM, Dave Angel wrote: > On 02/18/2013 09:54 PM, Mitya Sirenef wrote: >> On 02/18/2013 09:17 PM, Jon Reyes wrote: >>> Thanks Dave and Mitya for enlightening me about dictionaries. I'm >>> still confused about this though: >> > >> > " so that if two >> > key objects are equal, they stay equal, and if they differ, they stay >> > different. " >> > >> > What does this mean? I won't be comparing key objects with one >> another. Also, when I had two keys with the same value the value of the >> other key disappeared so I assume in runtime if there are multiple keys >> of the same value only the last one will appear. >> >> You won't be, but dict will. >> >> Dict is by definition a mapping where a value is assigned to a unique >> key. If you have two keys and two values, and then change one key to >> be equal to the second key, that's not kosher, because which value it's >> supposed to return when you try to get it by that key? >> >> So in effect, key's hash value should not change. If key is immutable, >> you can be certain that it's hash value will not change. If it's >> mutable, you have to make sure not to change the key in a way that'd >> make its hash value different than it was. >> >> -m >> > > It's a little stronger than that, since equal hashes cannot assure > equal data. The equality of each object pair in a dict must not > change over time, not just the hashes of the individual objects. > Ah, yes - that's true; if hashes were unequal and then the key is changed to be equal to the first key, both mydict[key1] and mydict[key2] will give you value1, but iterating over dict items will print key1, value1; key2, value2. And that's not a good thing. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ True friends stab you in the front. Oscar Wilde
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 18:17 -0800 |
| Message-ID | <mailman.1997.1361240841.2939.python-list@python.org> |
| In reply to | #39159 |
Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though: " so that if two key objects are equal, they stay equal, and if they differ, they stay different. " What does this mean? I won't be comparing key objects with one another. Also, when I had two keys with the same value the value of the other key disappeared so I assume in runtime if there are multiple keys of the same value only the last one will appear.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-02-19 02:34 +0000 |
| Message-ID | <mailman.1998.1361241298.2939.python-list@python.org> |
| In reply to | #39159 |
On 19/02/2013 01:38, Jon Reyes wrote: > Hi Mark. Well, doesn't iteritems() work the same? > It's iteritems for Python 2, items for Python 3. -- Cheers. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 18:39 -0800 |
| Message-ID | <7d01e10e-9661-403a-84d5-68c84ba8103c@googlegroups.com> |
| In reply to | #39172 |
Oh, I see, thanks! I was thinking I'll study 2.7 and once I'm comfortable with Python as a language I'll move to 3. Heck, I don't even know how to create a simple main method.
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 18:39 -0800 |
| Message-ID | <mailman.1999.1361241561.2939.python-list@python.org> |
| In reply to | #39172 |
Oh, I see, thanks! I was thinking I'll study 2.7 and once I'm comfortable with Python as a language I'll move to 3. Heck, I don't even know how to create a simple main method.
[toc] | [prev] | [next] | [standalone]
| From | Jon Reyes <everystepsayes@gmail.com> |
|---|---|
| Date | 2013-02-18 17:38 -0800 |
| Message-ID | <mailman.1993.1361238553.2939.python-list@python.org> |
| In reply to | #39158 |
Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps are always Strings. PS: Just checked, wow I could use ints as keys. Awesome!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web