[Tutor] Dynamic naming of lists
Japhy Bartlett
japhy at pearachute.com
Tue Mar 31 18:18:44 CEST 2015
Ian -
Note that if all your keys are named 'broadcast', the dictionary approach
is probably not going to work. You'll end up with something like:
{ 'broadcast': 'last_value_in_the_list' }
On Tue, Mar 31, 2015 at 10:56 AM, Ian D <duxbuz at hotmail.com> wrote:
> Ok Thanks a lot. And sadly not a typo, my bad logic overwriting values!
>
> ----------------------------------------
> > To: tutor at python.org
> > From: __peter__ at web.de
> > Date: Tue, 31 Mar 2015 17:50:01 +0200
> > Subject: Re: [Tutor] Dynamic naming of lists
> >
> > Ian D wrote:
> >
> >> Thanks I will look into these. The data going in is a list like
> >> this:['broadcast', '"d8on"', 'broadcast', '"d11on"']
> >>
> >> With the output beng something like this.
> >>
> >> lst_0 = ['broadcast', '"d8on"']
> >>
> >> lst_0 = ['broadcast', '"d11on"']
> >
> > Is that a typo, did you mean
> >
> > lst_1 = ['broadcast', '"d11on"']
> >
> > ? If so us a list. Complete example:
> >
> >>>> flat_pairs = ['broadcast', '"d8on"', 'broadcast', '"d11on"']
> >>>> it = iter(flat_pairs)
> >>>> pairs = list(zip(it, it))
> >>>> pairs
> > [('broadcast', '"d8on"'), ('broadcast', '"d11on"')]
> >
> > You can then access individual pairs by providing an index into the pairs
> > list:
> >
> >>>> pairs[0]
> > ('broadcast', '"d8on"')
> >>>> pairs[1]
> > ('broadcast', '"d11on"')
> >
> >> I have managed to use a dictionary as advised in a post on
> StackOverflow;
> >> not quite completed it as I am overwriting my lists each time. From your
> >> experience is it better to pursue the dictionary route or the zip tuple
> >> option. I am in python2.7
> >
> > A dictionary is typically used when the keys are not as regular as a
> > sequence of integers, e. g. to map a user name to an email address or a
> word
> > to a list of the positions where it occurs in a text.
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list