[Tutor] help

Sanhita Mallick sanhitam at yahoo.com
Sat Mar 14 00:40:54 CET 2009


help


--- On Sat, 3/14/09, tutor-request at python.org <tutor-request at python.org> wrote:

> From: tutor-request at python.org <tutor-request at python.org>
> Subject: Tutor Digest, Vol 61, Issue 50
> To: tutor at python.org
> Date: Saturday, March 14, 2009, 5:18 AM
> Send Tutor mailing list submissions to
> 	tutor at python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body
> 'help' to
> 	tutor-request at python.org
> 
> You can reach the person managing the list at
> 	tutor-owner at python.org
> 
> When replying, please edit your Subject line so it is more
> specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>    1. update list of dictionaries based on key (ski)
>    2. Re: update list of dictionaries based on key (greg
> whittier)
>    3. Re: update list of dictionaries based on key (ski)
>    4. best gui for the job? (W W)
>    5. Re: best gui for the job? (Lie Ryan)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Fri, 13 Mar 2009 16:09:58 +0100
> From: ski <norman at khine.net>
> Subject: [Tutor] update list of dictionaries based on key
> To: python tutor <tutor at python.org>
> Message-ID: <49BA7746.6090103 at khine.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> Hello,
> Here is what I have so far:
> 
>  >>> mylist = [{'index': 0,
> 'title': 'Association of British Travel 
> Agents', 'selected': False, 'edit_row':
> '?edit_affiliation=0', 
> 'affiliation': 'ABTA',
> 'affiliation_no': u'G3903'},
> {'index': 1, 
> 'title': 'Appointed Agents of IATA',
> 'selected': False, 'edit_row': 
> '?edit_affiliation=1', 'affiliation':
> 'IATA', 'affiliation_no': 
> u'none'}, {'index': 0, 'title':
> 'Association of Airline Cons.', 
> 'selected': False, 'edit_row':
> '?edit_affiliation=0', 'affiliation': 
> 'AAC', 'affiliation_no': u'sss'},
> {'index': 1, 'title': 'Association of 
> British Travel Agents', 'selected': False,
> 'edit_row': 
> '?edit_affiliation=1', 'affiliation':
> 'ABTA', 'affiliation_no': u'zser'}]
> 
>  >>> key = 'affiliation_no'
> 
>  >>> my_dup_keys = []
>  >>> merged_list = []
> 
>  >>> mykeys = [item['affiliation'] for
> item in mylist]
> 
>  >>> for x in mykeys:
>      	if mykeys.count(x) >= 2:
>          	my_dup_keys.append(x)
> 
>  >>> for item in mylist:
>      	if item['affiliation'] in set(my_dup_keys):
>          	merged_list.append(item)
> 
> 
>  >>> values = [d[key] for d in merged_list if
> d.has_key(key)]
>  >>> for dict in merged_list:
>      	dict[key] = values
>      	mylist.append(dict)
> 
>  >>> print mylist
> [{'index': 0, 'title': 'Association of
> British Travel Agents', 
> 'selected': False, 'edit_row':
> '?edit_affiliation=0', 'affiliation': 
> 'ABTA', 'affiliation_no':
> [u'G3903', u'zser']}, {'index': 1,
> 'title': 
> 'Appointed Agents of IATA', 'selected':
> False, 'edit_row': 
> '?edit_affiliation=1', 'affiliation':
> 'IATA', 'affiliation_no': 
> u'none'}, {'index': 0, 'title':
> 'Association of Airline Cons.', 
> 'selected': False, 'edit_row':
> '?edit_affiliation=0', 'affiliation': 
> 'AAC', 'affiliation_no': u'sss'},
> {'index': 1, 'title': 'Association of 
> British Travel Agents', 'selected': False,
> 'edit_row': 
> '?edit_affiliation=1', 'affiliation':
> 'ABTA', 'affiliation_no': 
> [u'G3903', u'zser']}, {'index': 0,
> 'title': 'Association of British 
> Travel Agents', 'selected': False,
> 'edit_row': '?edit_affiliation=0', 
> 'affiliation': 'ABTA',
> 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 
> 1, 'title': 'Association of British Travel
> Agents', 'selected': False, 
> 'edit_row': '?edit_affiliation=1',
> 'affiliation': 'ABTA', 
> 'affiliation_no': [u'G3903',
> u'zser']}]
> 
> 
> This sort of works but I want to return a list that updates
> 'mylist' not 
> append to it, so I will get:
> 
> [{'index': 0, 'title': 'Association of
> British Travel Agents', 
> 'selected': False, 'edit_row':
> '?edit_affiliation=0', 'affiliation': 
> 'ABTA', 'affiliation_no':
> [u'G3903', u'zser']}, {'index': 1,
> 'title': 
> 'Appointed Agents of IATA', 'selected':
> False, 'edit_row': 
> '?edit_affiliation=1', 'affiliation':
> 'IATA', 'affiliation_no': 
> u'none'}, {'index': 0, 'title':
> 'Association of Airline Cons.', 
> 'selected': False, 'edit_row':
> '?edit_affiliation=0', 'affiliation': 
> 'AAC', 'affiliation_no': u'sss'}]
>      	
> 
> Thanks
> 
> Norman
> 
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Fri, 13 Mar 2009 11:43:19 -0400
> From: greg whittier <greg at thewhittiers.com>
> Subject: Re: [Tutor] update list of dictionaries based on
> key
> To: python tutor <tutor at python.org>
> Message-ID:
> 	<a250eacf0903130843o6aed2c3bk1893a20feed0cce3 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> On Fri, Mar 13, 2009 at 11:09 AM, ski
> <norman at khine.net> wrote:
> > Hello,
> > Here is what I have so far:
> >
> >>>> mylist = [{'index': 0,
> 'title': 'Association of British Travel
> Agents',
> >>>> 'selected': False,
> 'edit_row': '?edit_affiliation=0',
> 'affiliation': 'ABTA',
> >>>> 'affiliation_no':
> u'G3903'}, {'index': 1, 'title':
> 'Appointed Agents of
> >>>> IATA', 'selected': False,
> 'edit_row': '?edit_affiliation=1',
> 'affiliation':
> >>>> 'IATA', 'affiliation_no':
> u'none'}, {'index': 0, 'title':
> 'Association of
> >>>> Airline Cons.', 'selected':
> False, 'edit_row': '?edit_affiliation=0',
> >>>> 'affiliation': 'AAC',
> 'affiliation_no': u'sss'}, {'index':
> 1, 'title':
> >>>> 'Association of British Travel
> Agents', 'selected': False, 'edit_row':
> >>>> '?edit_affiliation=1',
> 'affiliation': 'ABTA',
> 'affiliation_no': u'zser'}]
> >
> >>>> key = 'affiliation_no'
> >
> >>>> my_dup_keys = []
> >>>> merged_list = []
> >
> >>>> mykeys = [item['affiliation'] for
> item in mylist]
> >
> >>>> for x in mykeys:
> > ? ? ? ?if mykeys.count(x) >= 2:
> > ? ? ? ? ? ? ? ?my_dup_keys.append(x)
> >
> >>>> for item in mylist:
> > ? ? ? ?if item['affiliation'] in
> set(my_dup_keys):
> > ? ? ? ? ? ? ? ?merged_list.append(item)
> >
> >
> >>>> values = [d[key] for d in merged_list if
> d.has_key(key)]
> >>>> for dict in merged_list:
> > ? ? ? ?dict[key] = values
> > ? ? ? ?mylist.append(dict)
> >
> >>>> print mylist
> > [{'index': 0, 'title':
> 'Association of British Travel Agents',
> 'selected':
> > False, 'edit_row':
> '?edit_affiliation=0', 'affiliation':
> 'ABTA',
> > 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 1, 'title':
> 'Appointed
> > Agents of IATA', 'selected': False,
> 'edit_row': '?edit_affiliation=1',
> > 'affiliation': 'IATA',
> 'affiliation_no': u'none'},
> {'index': 0, 'title':
> > 'Association of Airline Cons.',
> 'selected': False, 'edit_row':
> > '?edit_affiliation=0', 'affiliation':
> 'AAC', 'affiliation_no': u'sss'},
> > {'index': 1, 'title': 'Association
> of British Travel Agents', 'selected':
> > False, 'edit_row':
> '?edit_affiliation=1', 'affiliation':
> 'ABTA',
> > 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 0, 'title':
> 'Association
> > of British Travel Agents', 'selected':
> False, 'edit_row':
> > '?edit_affiliation=0', 'affiliation':
> 'ABTA', 'affiliation_no': [u'G3903',
> > u'zser']}, {'index': 1,
> 'title': 'Association of British Travel
> Agents',
> > 'selected': False, 'edit_row':
> '?edit_affiliation=1', 'affiliation':
> 'ABTA',
> > 'affiliation_no': [u'G3903',
> u'zser']}]
> >
> >
> > This sort of works but I want to return a list that
> updates 'mylist' not
> > append to it, so I will get:
> >
> > [{'index': 0, 'title':
> 'Association of British Travel Agents',
> 'selected':
> > False, 'edit_row':
> '?edit_affiliation=0', 'affiliation':
> 'ABTA',
> > 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 1, 'title':
> 'Appointed
> > Agents of IATA', 'selected': False,
> 'edit_row': '?edit_affiliation=1',
> > 'affiliation': 'IATA',
> 'affiliation_no': u'none'},
> {'index': 0, 'title':
> > 'Association of Airline Cons.',
> 'selected': False, 'edit_row':
> > '?edit_affiliation=0', 'affiliation':
> 'AAC', 'affiliation_no': u'sss'}]
> >
> 
> It's a little hard to figure what you're getting
> at.  This looks like
> a table represented by a list of dictionaries that
> you'd like to group
> by "affiliation."
> 
> affiliation_nos = {}
> for row in mylist:
>    
> affiliation_nos.set_default(row['affiliation'],[]).append(row['affliation_no'])
> 
> will give you a list of affiliation_no's for each
> affiliation and if
> you want a list of dicts, you could do
> 
> mynewlist = [dict(affiliation=affiliation, affiliation_nos
> =
> affiliation_nos[affiliation]) for affiliation in
> affiliation_nos.keys()]
> 
> I don't know what to do about all the other fields
> though.  In your
> example input list you have two dictionaries with
> affiliation =
> 'ABTA'.  In your output you kept the one with
> index=0 and threw away
> index=1.  (same for 'edit_row')  How do you
> determine which to keep?
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Fri, 13 Mar 2009 17:23:32 +0100
> From: ski <norman at khine.net>
> Subject: Re: [Tutor] update list of dictionaries based on
> key
> To: greg whittier <greg at thewhittiers.com>
> Cc: python tutor <tutor at python.org>
> Message-ID: <49BA8884.3040708 at khine.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> 
> 
> greg whittier wrote:
> > On Fri, Mar 13, 2009 at 11:09 AM, ski
> <norman at khine.net> wrote:
> >> Hello,
> >> Here is what I have so far:
> >>
> >>>>> mylist = [{'index': 0,
> 'title': 'Association of British Travel
> Agents',
> >>>>> 'selected': False,
> 'edit_row': '?edit_affiliation=0',
> 'affiliation': 'ABTA',
> >>>>> 'affiliation_no':
> u'G3903'}, {'index': 1, 'title':
> 'Appointed Agents of
> >>>>> IATA', 'selected': False,
> 'edit_row': '?edit_affiliation=1',
> 'affiliation':
> >>>>> 'IATA',
> 'affiliation_no': u'none'},
> {'index': 0, 'title': 'Association of
> >>>>> Airline Cons.',
> 'selected': False, 'edit_row':
> '?edit_affiliation=0',
> >>>>> 'affiliation': 'AAC',
> 'affiliation_no': u'sss'}, {'index':
> 1, 'title':
> >>>>> 'Association of British Travel
> Agents', 'selected': False, 'edit_row':
> >>>>> '?edit_affiliation=1',
> 'affiliation': 'ABTA',
> 'affiliation_no': u'zser'}]
> >>>>> key = 'affiliation_no'
> >>>>> my_dup_keys = []
> >>>>> merged_list = []
> >>>>> mykeys = [item['affiliation']
> for item in mylist]
> >>>>> for x in mykeys:
> >>        if mykeys.count(x) >= 2:
> >>                my_dup_keys.append(x)
> >>
> >>>>> for item in mylist:
> >>        if item['affiliation'] in
> set(my_dup_keys):
> >>                merged_list.append(item)
> >>
> >>
> >>>>> values = [d[key] for d in merged_list
> if d.has_key(key)]
> >>>>> for dict in merged_list:
> >>        dict[key] = values
> >>        mylist.append(dict)
> >>
> >>>>> print mylist
> >> [{'index': 0, 'title':
> 'Association of British Travel Agents',
> 'selected':
> >> False, 'edit_row':
> '?edit_affiliation=0', 'affiliation':
> 'ABTA',
> >> 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 1, 'title':
> 'Appointed
> >> Agents of IATA', 'selected': False,
> 'edit_row': '?edit_affiliation=1',
> >> 'affiliation': 'IATA',
> 'affiliation_no': u'none'},
> {'index': 0, 'title':
> >> 'Association of Airline Cons.',
> 'selected': False, 'edit_row':
> >> '?edit_affiliation=0',
> 'affiliation': 'AAC',
> 'affiliation_no': u'sss'},
> >> {'index': 1, 'title':
> 'Association of British Travel Agents',
> 'selected':
> >> False, 'edit_row':
> '?edit_affiliation=1', 'affiliation':
> 'ABTA',
> >> 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 0, 'title':
> 'Association
> >> of British Travel Agents', 'selected':
> False, 'edit_row':
> >> '?edit_affiliation=0',
> 'affiliation': 'ABTA',
> 'affiliation_no': [u'G3903',
> >> u'zser']}, {'index': 1,
> 'title': 'Association of British Travel
> Agents',
> >> 'selected': False, 'edit_row':
> '?edit_affiliation=1', 'affiliation':
> 'ABTA',
> >> 'affiliation_no': [u'G3903',
> u'zser']}]
> >>
> >>
> >> This sort of works but I want to return a list
> that updates 'mylist' not
> >> append to it, so I will get:
> >>
> >> [{'index': 0, 'title':
> 'Association of British Travel Agents',
> 'selected':
> >> False, 'edit_row':
> '?edit_affiliation=0', 'affiliation':
> 'ABTA',
> >> 'affiliation_no': [u'G3903',
> u'zser']}, {'index': 1, 'title':
> 'Appointed
> >> Agents of IATA', 'selected': False,
> 'edit_row': '?edit_affiliation=1',
> >> 'affiliation': 'IATA',
> 'affiliation_no': u'none'},
> {'index': 0, 'title':
> >> 'Association of Airline Cons.',
> 'selected': False, 'edit_row':
> >> '?edit_affiliation=0',
> 'affiliation': 'AAC',
> 'affiliation_no': u'sss'}]
> >>
> > 
> > It's a little hard to figure what you're
> getting at.  This looks like
> > a table represented by a list of dictionaries that
> you'd like to group
> > by "affiliation."
> 
> i have objects in my database, called addresses, each
> address has a 
> metadata called affiliation.
> 
> also each address is a member of a company.
> 
> in my company class, i wanted to get all the addresses
> affiliations and 
> only list the unique values.
> 
> 
> > 
> > affiliation_nos = {}
> > for row in mylist:
> >    
> affiliation_nos.set_default(row['affiliation'],[]).append(row['affliation_no'])
> > 
> > will give you a list of affiliation_no's for each
> affiliation and if
> > you want a list of dicts, you could do
> > 
> > mynewlist = [dict(affiliation=affiliation,
> affiliation_nos =
> > affiliation_nos[affiliation]) for affiliation in
> > affiliation_nos.keys()]
> > 
> > I don't know what to do about all the other fields
> though.  In your
> > example input list you have two dictionaries with
> affiliation =
> > 'ABTA'.  In your output you kept the one with
> index=0 and threw away
> > index=1.  (same for 'edit_row')  How do you
> determine which to keep?
> 
> I want to keep it based on the 'affiliation' key.
> 
> 
> In essence, from my original 'mylist' I want to
> keep all the list items 
> as they are, but just update the list items that have more
> than one 
> 'affiliation' by updating only the
> 'affiliation_no' to contain both entries.
> 
> Perhaps there is a simpler way to do this ;)
> 
> I will change my code so that mylist does not contain the
> 'index' and 
> 'edit_row' keys. Perhaps then it will be easier to
> update the dictionary?
> 
> 
> 
> 
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> 
> 
> ------------------------------
> 
> Message: 4
> Date: Fri, 13 Mar 2009 12:29:16 -0500
> From: W W <srilyk at gmail.com>
> Subject: [Tutor] best gui for the job?
> To: "tutor at python.org" <Tutor at python.org>
> Message-ID:
> 	<333efb450903131029r62a29c30n60d24c5003fa7142 at mail.gmail.com>
> Content-Type: text/plain; charset=windows-1252
> 
> Hi,
> 
> What is the best gui to implement a simple paint-esque
> program in?
> 
> I already built a really simple one in Tkinter - but
> Tkinter lacks a
> save functionality. I considered using the PIL to draw in
> the
> background, but it didn't seem to have a good enough
> resolution. I
> thought about pyGTK because I have some familiarity with
> that, but I
> can't find any examples of saving a drawingarea.
> 
> Also, to clarify what I mean by "best" - I want
> it to be fairly simple
> to implement (my non-saving sketch in a single color/size
> is about 100
> lines), yet extensible - I'd like the ability to
> further enhance my
> program down the road. Changing the opacity, and the
> possibility to
> work with layers is a plus.
> 
> If anyone has tips or knows of any tutorials (I haven't
> really been
> able to find anything suitable), please let me know.
> 
> Thanks for any suggestions,
> Wayne
> 
> -- 
> To be considered stupid and to be told so is more painful
> than being
> called gluttonous, mendacious, violent, lascivious, lazy,
> cowardly:
> every weakness, every vice, has found its defenders, its
> rhetoric, its
> ennoblement and exaltation, but stupidity hasn?t. - Primo
> Levi
> 
> 
> ------------------------------
> 
> Message: 5
> Date: Sat, 14 Mar 2009 05:18:00 +1100
> From: Lie Ryan <lie.1296 at gmail.com>
> Subject: Re: [Tutor] best gui for the job?
> To: tutor at python.org
> Message-ID: <gpe80n$d7e$1 at ger.gmane.org>
> Content-Type: text/plain; charset=UTF-8; format=flowed
> 
> W W wrote:
> > Hi,
> > 
> > What is the best gui to implement a simple paint-esque
> program in?
> > 
> > I already built a really simple one in Tkinter - but
> Tkinter lacks a
> > save functionality. I considered using the PIL to draw
> in the
> > background, but it didn't seem to have a good
> enough resolution. I
> > thought about pyGTK because I have some familiarity
> with that, but I
> > can't find any examples of saving a drawingarea.
> 
> What do you mean "good enough resolution"?
> 
> 
> 
> ------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> End of Tutor Digest, Vol 61, Issue 50
> *************************************


More information about the Tutor mailing list