[Tutor] Novice Python Question

Kent Johnson kent37 at tds.net
Tue Jul 1 18:26:38 CEST 2008


Forwarding to the list with my reply. (Please use reply all to reply
to the list)

On Tue, Jul 1, 2008 at 11:36 AM, S Potter <f8lcoder at hotmail.com> wrote:
> Kent,
>
> Thanks for the response the answer to question 2 helped. It seem python is
> much less verbose than I anticipate at times.
>
> As for my first question, my apologies I'm using a static list not a
> dictionary.
>
> ex:
>
> senders = [('albany','somestreet address','state','zipcode'),
> ('albany','somestreet address','state','zipcode'),
> ('albany','somestreet address','state','zipcode'),
> ('albany','somestreet address','state','zipcode'),
> ('buffalo','somestreet address','state','zipcode'),
> ('buffalo','somestreet address','state','zipcode'),
> ('cairo','somestreet address','state','zipcode'),]
>
> states = [('albany'),
> ('buffalo'),
> ('cairo'),]
>
> or states = sort(senders[0])
>
> Lets say I select from list states the value of states[0] = 'albany'
>
> I'd like to filter senders for states[0] ## where the selected value is =
> 'albany'
>  so here I think I'd use a variable to grab the the value
>        x = states.getselectedvalue() ## for my example x should be 'albany'

I don't know how you are selecting from the list? There is no GUI
here, just a simple list.

Anyway, to get all the values of senders which start with 'albany' you
can use a list comprehension:
x = 'albany' # doesn't have to be in a variable, just showing that it
*can* be a variable
filteredlist = [ item for item in senders if item[0] == x ]

Are you reading a tutorial? If not I recommend you pick one from this list:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Kent

> providing me with a filtered list from senders only containing the
> following:
>
> [('albany','somestreet address','state','zipcode'),
> ('albany','somestreet address','state','zipcode'),
> ('albany','somestreet address','state','zipcode'),
> ('albany','somestreet address','state','zipcode'),]
>
> Please excuse this is quasi-pseudo code but I think it clears things up a
> bit.
>
> Basically I need an example of the syntax for this as I'm not certain I have
> it correct.
> I'm thinking it should be something like this maybe - result(senders,x)
>
> But I'm clear on it.
>
> Thanks Again,
>
> S
>
>
>
> ________________________________
>> Date: Tue, 1 Jul 2008 08:40:11 -0400
>> From: kent37 at tds.net
>> To: f8lcoder at hotmail.com
>> Subject: Re: [Tutor] Novice Python Question
>> CC: tutor at python.org
>>
>> On Tue, Jul 1, 2008 at 7:51 AM, S Potter <f8lcoder at hotmail.com> wrote:
>>
>> > Question 1.)
>> > I have dictionary or list containing multiple instances 'duplicate
>> > entries' of the same information. Lets say it's a list of addresses and
>> > list
>> > item i[0] contains city values equal to 'Albany' .
>> >
>> > I am using a second list of one element to provide my filter criteria.
>> > This list contains only cities with no duplicate entries.
>> >
>> > How do I filter my first list based upon the selected position or a
>> > variable equal to the value of the selected position from my second
>> > list?
>>
>> It would help to see more specific examples of the data you have and
>> the desired result, but in general a list comprehension is the easiest
>> way to filter a list.
>> http://docs.python.org/tut/node7.html#SECTION007140000000000000000
>>
>> > Question 2.) If I assign a value to a variable x = "MyVlaue"
>> > How do I put the value of x into a list?
>>
>> > I would think it would be something like:
>> > list[(str(x))]
>> > But I'm not getting the results I expect.
>> > This would probably be considered
>> > macro-substitution in other languages but I cannot find reference to
>> > this in
>> > python.
>>
>> The value of x is the string "MyVlaue", is that what you want? If so
>> what you are doingis close, you can use simply [x] to create a list
>> containing the value of x. Or do you want the value of the variable
>> named MyVlaue? In that case you probably should use a dictionary to
>> hold the value rather than a named variable.
>>
>> Kent
>
>
> ________________________________
> Making the world a better place one message at a time. Check out the i'm
> Talkathon.


More information about the Tutor mailing list