Python multimap

This is a discussion on Python multimap within the Python forums in Programming Languages category; Recently had a need to us a multimap container in C++. I now need to write equivalent Python code. How does Python handle this? k['1'] = 'Tom' k['1'] = 'Bob' k['1'] = 'Joe' .... Same key, but different values. No overwrites either.... They all must be inserted into the container Thanks, Brad...

Go Back   Application Development Forum > Programming Languages > Python

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 09:35 AM
brad
Guest
 
Default Python multimap

Recently had a need to us a multimap container in C++. I now need to
write equivalent Python code. How does Python handle this?

k['1'] = 'Tom'
k['1'] = 'Bob'
k['1'] = 'Joe'
....

Same key, but different values. No overwrites either.... They all must
be inserted into the container

Thanks,
Brad
Reply With Quote
  #2  
Old 08-27-2008, 09:52 AM
Mike Kent
Guest
 
Default Re: Python multimap

On Aug 27, 9:35*am, brad <byte8b...@gmail.com> wrote:
> Recently had a need to us a multimap container in C++. I now need to
> write equivalent Python code. How does Python handle this?
>
> k['1'] = 'Tom'
> k['1'] = 'Bob'
> k['1'] = 'Joe'
> ...
>
> Same key, but different values. No overwrites either.... They all must
> be inserted into the container
>
> Thanks,
> Brad


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']
>>>

Reply With Quote
  #3  
Old 08-27-2008, 10:08 AM
Matt Nordhoff
Guest
 
Default Re: Python multimap

brad wrote:
> Recently had a need to us a multimap container in C++. I now need to
> write equivalent Python code. How does Python handle this?
>
> k['1'] = 'Tom'
> k['1'] = 'Bob'
> k['1'] = 'Joe'
> ...
>
> Same key, but different values. No overwrites either.... They all must
> be inserted into the container
>
> Thanks,
> Brad


I don't know if this is exactly equivalent, but what about using a
defaultdict like this?

>>> from collections import defaultdict
>>> k = defaultdict(list)
>>> k['1'].append('Tom')
>>> k['1'].append('Bob')
>>> k['1'].append('Joe')
>>> k['1']

['Tom', 'Bob', 'Joe']
--
Reply With Quote
  #4  
Old 08-27-2008, 10:12 AM
Michele Petrazzo
Guest
 
Default Re: Python multimap

brad wrote:
> Recently had a need to us a multimap container in C++. I now need to
> write equivalent Python code. How does Python handle this?
>
> k['1'] = 'Tom'
> k['1'] = 'Bob'
> k['1'] = 'Joe'
> ....
>
> Same key, but different values. No overwrites either.... They all must
> be inserted into the container
>


Subclassing the builtin dict?

class d(dict):
def __setitem__(self, item, value):
if not item in self: super(d, self).__setitem__(item, [])
self[item].append(value)

>>> D = d()
>>> D[1] = "Hello"
>>> D[1] = "World!"
>>> D[1]

['Hello', 'World!']


> Thanks,
> Brad


Michele
Reply With Quote
  #5  
Old 08-27-2008, 01:52 PM
brad
Guest
 
Default Re: Python multimap

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.
Reply With Quote
  #6  
Old 08-27-2008, 02:08 PM
Miles
Guest
 
Default Re: Python multimap

brad wrote:
> 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 ... without making K's value a list of stuff
> to append to.


That's what a multimap is. If you really need the syntactic sugar,
it's simple to implement:

class multidict(dict):
def __setitem__(self, key, value):
try:
self[key].append(value)
except KeyError:
dict.__setitem__(self, key, [value])

-Miles
Reply With Quote
  #7  
Old 08-27-2008, 02:12 PM
castironpi
Guest
 
Default Re: Python multimap

On Aug 27, 12: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.


I don't understand what a multimap does that a map of lists doesn't do.
Reply With Quote
  #8  
Old 08-27-2008, 02:37 PM
Fredrik Lundh
Guest
 
Default Re: Python multimap

Miles wrote:

> That's what a multimap is.


iirc, a C++ multimap provides a flat view of the data, so you need to
provide custom enumeration and iteration methods as well.

</F>

Reply With Quote
  #9  
Old 08-27-2008, 02:38 PM
brad
Guest
 
Default Re: Python multimap

castironpi wrote:

> I don't understand what a multimap does that a map of lists doesn't do.


It counts both keys individually as separate keys. The Python workaround
does not... see examples... notice the key(s) that are '4'

Python output (using the k = [] idea):

Key: 4 Value: [[13, 'Visa'], [16, 'Visa']]
Key: 51 Value: [16, 'MC']
Key: 65 Value: [16, 'Discover']
Key: 2131 Value: [15, 'JCB']
Key: 300 Value: [14, 'Diners CB']
Key: 301 Value: [14, 'Diners CB']
Key: 302 Value: [14, 'Diners CB']
Key: 303 Value: [14, 'Diners CB']
Key: 304 Value: [14, 'Diners CB']
Key: 305 Value: [14, 'Diners CB']
Key: 35 Value: [16, 'JCB']
Key: 34 Value: [15, 'Amex']
Key: 55 Value: [16, 'MC or Diners US and CA']
Key: 36 Value: [14, 'Diners Intl']
Key: 37 Value: [15, 'Amex']
Key: 1800 Value: [15, 'JCB']
Key: 54 Value: [16, 'MC']
Key: 6011 Value: [16, 'Discover']
Key: 52 Value: [16, 'MC']
Key: 53 Value: [16, 'MC']
Key: 385 Value: [14, 'Diners CB']
21 is the size of the dict

A C++ multimap

Key: 1800 Value: JCB 15
Key: 2131 Value: JCB 15
Key: 300 Value: Diners_Club 14
Key: 301 Value: Diners_Club 14
Key: 302 Value: Diners_Club 14
Key: 303 Value: Diners_Club 14
Key: 304 Value: Diners_Club 14
Key: 305 Value: Diners_Club 14
Key: 34 Value: American_Express 15
Key: 35 Value: JCB 16
Key: 36 Value: Diners_Club 14
Key: 37 Value: American_Express 15
Key: 385 Value: Diners_Club 14
Key: 4 Value: Visa 16
Key: 4 Value: Visa 13
Key: 51 Value: MasterCard 16
Key: 52 Value: MasterCard 16
Key: 53 Value: MasterCard 16
Key: 54 Value: MasterCard 16
Key: 55 Value: MasterCard 16
Key: 6011 Value: Discover 16
Key: 65 Value: Discover 16
22 is the size of the multimap
Reply With Quote
  #10  
Old 08-27-2008, 03:39 PM
castironpi
Guest
 
Default Re: Python multimap

On Aug 27, 1:38*pm, brad <byte8b...@gmail.com> wrote:
> castironpi wrote:
> > I don't understand what a multimap does that a map of lists doesn't do.

>
> It counts both keys individually as separate keys. The Python workaround
> does not... see examples... notice the key(s) that are '4'
>
> Python output (using the k = [] idea):
>
> Key: 4 Value: [[13, 'Visa'], [16, 'Visa']]
>
> A C++ multimap
>
> Key: 4 Value: Visa 16
> Key: 4 Value: Visa 13


You are looking at a two-line workaround. A single Key-4 element is
always k[4][0], if 4 is in k. To remove k[4] is a little trickier.
If len( k[4] )> 1: k[4].pop( ), else k.pop( 4 )[ 0 ]. (Smooth.)
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:02 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.