Needing python experts to help with a problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gms

    Needing python experts to help with a problem

    Hello,
    I have the following list:

    [{'count': u'2', 'manu': <Manufacturer : Manu1>}, {'count': u'4',
    'manu': <Manufacturer : Manu2>}, {'count': u'2', 'manu': <Manufacturer :
    Manu3>}, {'count': u'2', 'manu': <Manufacturer : Manu2>}]

    My current list currently contains four dictionaries. They are:

    {'count': u'2', 'manu': <Manufacturer : Manu1>}
    {'count': u'4', 'manu': <Manufacturer : Manu2>}
    {'count': u'2', 'manu': <Manufacturer : Manu3>}
    {'count': u'2', 'manu': <Manufacturer : Manu2>}

    I need to create a list for each specific Manufacturer. Since I have
    two dictionaries that have 'Manu2' as it's manufacturer. I will need
    some code that will create the following from the list above:

    [{'count': u'2', 'manu': <Manufacturer : Manu1>}]
    [{'count': u'4', 'manu': <Manufacturer : Manu2>},{'count ': u'2',
    'manu': <Manufacturer : Manu2>}]
    [{'count': u'2', 'manu': <Manufacturer : Manu3>}]

    The reason for this is because I want to send one email to each
    manufacturer. In this case I would send two emails to Manu2 because I
    have two dictionaries that have Manu2 as the manufacturer. That is
    why I need to create a separate list for each manufacturer.

    Any help on on how best to do this would be greatly appreciated!

    Thanks
  • CM

    #2
    Re: Needing python experts to help with a problem

    On Jun 7, 12:51 am, gms <esug...@gmail. comwrote:
    Hello,
    I have the following list:
    >
    [{'count': u'2', 'manu': <Manufacturer : Manu1>}, {'count': u'4',
    'manu': <Manufacturer : Manu2>}, {'count': u'2', 'manu': <Manufacturer :
    Manu3>}, {'count': u'2', 'manu': <Manufacturer : Manu2>}]
    >
    My current list currently contains four dictionaries. They are:
    >
    {'count': u'2', 'manu': <Manufacturer : Manu1>}
    {'count': u'4', 'manu': <Manufacturer : Manu2>}
    {'count': u'2', 'manu': <Manufacturer : Manu3>}
    {'count': u'2', 'manu': <Manufacturer : Manu2>}
    >
    I need to create a list for each specific Manufacturer. Since I have
    two dictionaries that have 'Manu2' as it's manufacturer. I will need
    some code that will create the following from the list above:
    >
    [{'count': u'2', 'manu': <Manufacturer : Manu1>}]
    [{'count': u'4', 'manu': <Manufacturer : Manu2>},{'count ': u'2',
    'manu': <Manufacturer : Manu2>}]
    [{'count': u'2', 'manu': <Manufacturer : Manu3>}]
    >
    The reason for this is because I want to send one email to each
    manufacturer. In this case I would send two emails to Manu2 because I
    have two dictionaries that have Manu2 as the manufacturer. That is
    why I need to create a separate list for each manufacturer.
    >
    Any help on on how best to do this would be greatly appreciated!
    >
    Thanks
    I'm not expert, but it just seems that this structure is overly
    complex. What is your goal, and what information do you want to
    associate with each manufacturer? Couldn't you simplify this
    this section:
    {'count': u'2', 'manu': <Manufacturer : Manu1>}
    {'count': u'4', 'manu': <Manufacturer : Manu2>}
    {'count': u'2', 'manu': <Manufacturer : Manu3>}
    {'count': u'2', 'manu': <Manufacturer : Manu2>}
    to something like:

    manu_dict = { Manu1:[2], Manu2:[4,2], Manu3:[2] }

    Comment

    • John Nagle

      #3
      Re: Needing python experts to help with a problem

      CM wrote:
      On Jun 7, 12:51 am, gms <esug...@gmail. comwrote:
      >Hello,
      >I have the following list:
      >>
      >[{'count': u'2', 'manu': <Manufacturer : Manu1>}, {'count': u'4',
      >'manu': <Manufacturer : Manu2>}, {'count': u'2', 'manu': <Manufacturer :
      >Manu3>}, {'count': u'2', 'manu': <Manufacturer : Manu2>}]
      ....

      This sounds like a homework assignment. If you're having trouble
      with this, sending mail from Python is really going to be a headache
      for you.

      You want something like

      inlist = [{'count': u'2', 'manu': <Manufacturer : Manu1>}, {'count': u'4',
      'manu': <Manufacturer : Manu2>}, {'count': u'2', 'manu': <Manufacturer :
      Manu3>}, {'count': u'2', 'manu': <Manufacturer : Manu2>}]
      outdict = {}
      for mandict in inlist :
      if not outdict.has_key (mandict[manu]) # if first for this manu
      outdict[mandict[manu]] = [] # make empty list
      outdict[mandict[manu]].append(mandict ) # append entry to list

      You now have the desired result in a dictionary, which you can convert to a list
      if you like.

      If it's a production job, and the number of manufacturers is large,
      you're probably better off using a database like MySQL, or some
      mail merge program.

      John Nagle

      Comment

      Working...