| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#11
| |||
| |||
| On Aug 27, 1:52*pm, brad <byte8b...@gmail.com> wrote: > Mike Kent wrote: > > Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) > > [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > >>>> k = {} > >>>> k['1'] = [] > >>>> k['1'].append('Tom') > >>>> k['1'].append('Bob') > >>>> k['1'].append('Joe') > > >>>> k['1'] > > ['Tom', 'Bob', 'Joe'] > > There is only one '1' key in your example. I need multiple keys that are > all '1'. I thought Python would have something built-in to handle this > sort of thing. > > I need a true multimap: > > k['1'] = 'Tom' > k['1'] = 'Tommy' > > without Tommy overwriting Tom and without making K's value a list of > stuff to append to. That's still just a regular map. What would you want to happen if you were to execute "print k['1']"? Best I can tell, you want some sort of association list like this: k = [] k.append(("1","Tom")) k.append(("1","Tommy")) which you can iterate through like this: for key,value in k: .... And if you need to retrieve items with a certain "key", probably it's easiest to maintain sorted invariant, and to do insertion and lookup with bisection algorithm (see bisect module). I don't know of any class in the standard library that does all that for you though. Out of curiosity, what does a true multimap solve that a dictionary of lists not solve? Carl Banks |
|
#12
| |||
| |||
| Carl Banks wrote: > Out of curiosity, what does a true multimap solve that a dictionary of > lists not solve? Nothing really. I went with a variation of the suggested work around... it's just that with Python I don't normally have to use work arounds and normally one obvious approach is correct: |
|
#13
| |||
| |||
| On Aug 28, 2:41*pm, brad <byte8b...@gmail.com> wrote: > Carl Banks wrote: > > Out of curiosity, what does a true multimap solve that a dictionary of > > lists not solve? > > Nothing really. I went with a variation of the suggested work around... > it's just that with Python I don't normally have to use work arounds and > * normally one obvious approach is correct: Might I suggest that the C++ multimap is the workaround, rather than the Python way of using dicts or lists or dicts of sets? It was too much programming overhead and line noise confusion to define nested templates to hold your nested data structures in C++, so the STL provided a container that eliminated the nesting. In Python, the obvious nested way to do it is easy, especially now with defaultdicts, so there was no reason to provide a multimap. Carl Banks |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.