Re: [Tutor] need a hint

2013-12-03 Thread Oscar Benjamin
Reposting to the list. Please send your response to the tutor list
rather than directly to me. That way you'll get a response more
quickly (from someone else). Also can you please write your response
below mine like below (rather than top-posting)?

On 3 December 2013 06:25, Byron Ruffin  wrote:
> On Mon, Dec 2, 2013 at 4:51 AM, Oscar Benjamin 
> wrote:
>>
>> On 2 December 2013 02:25, Byron Ruffin 
>> wrote:
>> >
>> > The following program works and does what I want except for one last
>> > problem
>> > I need to handle.   The program reads a txt file of senators and their
>> > associated states and when I input the last name it gives me their
>> > state.
>> > The problem is "Udall".  There are two of them.  The txt file is read by
>> > line and put into a dictionary with the names split.  I need a process
>> > to
>> > handle duplicate names.  Preferably one that will always work even if
>> > the
>> > txt file was changed/updated.  I don't want the process to handle the
>> > name
>> > "Udall" specifically.   For a duplicate name I would like to tell the
>> > user
>> > it is not a unique last name and then tell them to enter first name and
>> > then
>> > return the state of that senator.
>>
>> You're currently doing this:
>>
>> > senateInfo = {}
>> > senateInfo[lastName] = state
>>
>> Instead of storing just a state in the dict you could store a list of
>> states e.g.:
>>
>> senateInfo[lastName] = [state]
>>
>> Then when you find a lastName that is already in the dict you can do:
>>
>> senateInfo[lastName].append(state)
>>
>> to append the new state to the existing list of states. You'll need a
>> way to test if a particular lastName is already in the dict e.g.:
>>
>> if lastName in senateInfo:
>
> I tried this but I don't understand how to use it. I'm a beginner.  I
> understand that the brackets puts the states into a list but I don't know
> what to do with that.  I am able to detect if a name appears more than once
> using an "if" but that code runs as many times as the name occurs.

Could you perhaps show the code that you currently have? I don't quite
understand what you mean.

Note that I deliberately didn't give you an exact answer to your
problem because it looks like homework.


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Rafael Knuth
Hej there,

I am writing a little throw away program in order to better understand
how I can loop through a variable and a list at the same time. Here's
what the program does and how it looks like: It counts the number of
backpackers (assuming a growth rate of 15 % year on year) over the
last five years:

Backpackers = 100
for x in range(2009, 2014):
Backpackers = Backpackers*1.15
print("In %d there were %d backpackers worldwide." % (x, Backpackers))

>>>
In 2009 there were 115 backpackers worldwide.
In 2010 there were 1322500 backpackers worldwide.
In 2011 there were 1520874 backpackers worldwide.
In 2012 there were 1749006 backpackers worldwide.
In 2013 there were 2011357 backpackers worldwide.

Now I want to enhance that program a bit by adding the most popular
country in each year. Here's what I want to get as the output:

>>>
In 2009 there were 115 backpackers worldwide and their most
popular country was Brazil.
In 2010 there were 1322500 backpackers worldwide and their most
popular country was China.
In 2011 there were 1520874 backpackers worldwide and their most
popular country was France.
In 2012 there were 1749006 backpackers worldwide and their most
popular country was India.
In 2013 there were 2011357 backpackers worldwide and their most
popular country was Vietnam.

I assume that I need to have a list like this:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]

But I struggle to modify the program above in a way that it loops
properly through "Backpackers" and "PopularCountries".
>From all my iterations there's only one that came at least halfway
close to my desired result:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
for x in range(2009, 2014):
Backpackers = Backpackers*1.15
PopularCountries = PopularCountries.pop()
print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (x, Backpackers, PopularCountries))

It loops only once through "Backpackers" and "PopularCountries"
(starting with the last item on the list though) and then it breaks:

>>>
In 2009 there were 115 backpackers worldwide and their most
popular country was Vietnam.
Traceback (most recent call last):
  File "C:/Users/Rafael_Knuth/Desktop/Python/Backpackers.py", line 6,
in 
PopularCountries = PopularCountries.pop()
AttributeError: 'str' object has no attribute 'pop'

My questions:
Is there a way to use pop() to iterate through the list in a correct
order (starting on the left side instead on the right)?
If not: What alternative would you suggest?
Do I need to rewrite the program in order to iterate through
"Backpackers" and "PopularCountries" at the same time? (for example
using a while instead of a for loop?)
Or is there a way to modify my existing program?
Should "PopularCountries" be a list or do I need a dictionary here?

I am using Python 3.3.0.

Thank you in advance!

All the best,

Rafael
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Dave Angel
On Tue, 3 Dec 2013 13:55:31 +0100, Rafael Knuth 
 wrote:

for x in range(2009, 2014):






PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]


You can zip two iterators together and iterate through the resultant 
iterable of tuples. 


for x, country in zip (range (2009, 2014)):
   Print (x, country)

--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Mark Lawrence

On 03/12/2013 12:55, Rafael Knuth wrote:

Hej there,

I am writing a little throw away program in order to better understand
how I can loop through a variable and a list at the same time. Here's
what the program does and how it looks like: It counts the number of
backpackers (assuming a growth rate of 15 % year on year) over the
last five years:

Backpackers = 100
for x in range(2009, 2014):
 Backpackers = Backpackers*1.15
 print("In %d there were %d backpackers worldwide." % (x, Backpackers))




In 2009 there were 115 backpackers worldwide.
In 2010 there were 1322500 backpackers worldwide.
In 2011 there were 1520874 backpackers worldwide.
In 2012 there were 1749006 backpackers worldwide.
In 2013 there were 2011357 backpackers worldwide.

Now I want to enhance that program a bit by adding the most popular
country in each year. Here's what I want to get as the output:




In 2009 there were 115 backpackers worldwide and their most
popular country was Brazil.
In 2010 there were 1322500 backpackers worldwide and their most
popular country was China.
In 2011 there were 1520874 backpackers worldwide and their most
popular country was France.
In 2012 there were 1749006 backpackers worldwide and their most
popular country was India.
In 2013 there were 2011357 backpackers worldwide and their most
popular country was Vietnam.

I assume that I need to have a list like this:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]

But I struggle to modify the program above in a way that it loops
properly through "Backpackers" and "PopularCountries".
 From all my iterations there's only one that came at least halfway
close to my desired result:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
for x in range(2009, 2014):
 Backpackers = Backpackers*1.15
 PopularCountries = PopularCountries.pop()
 print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (x, Backpackers, PopularCountries))

It loops only once through "Backpackers" and "PopularCountries"
(starting with the last item on the list though) and then it breaks:




In 2009 there were 115 backpackers worldwide and their most
popular country was Vietnam.
Traceback (most recent call last):
   File "C:/Users/Rafael_Knuth/Desktop/Python/Backpackers.py", line 6,
in 
 PopularCountries = PopularCountries.pop()
AttributeError: 'str' object has no attribute 'pop'

My questions:
Is there a way to use pop() to iterate through the list in a correct
order (starting on the left side instead on the right)?
If not: What alternative would you suggest?
Do I need to rewrite the program in order to iterate through
"Backpackers" and "PopularCountries" at the same time? (for example
using a while instead of a for loop?)
Or is there a way to modify my existing program?
Should "PopularCountries" be a list or do I need a dictionary here?

I am using Python 3.3.0.

Thank you in advance!

All the best,

Rafael
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



Loop around your list using the enumerate builtin function and an 
appropriate value for start, see 
http://docs.python.org/3/library/functions.html#enumerate


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Mark Lawrence

On 03/12/2013 13:11, Dave Angel wrote:

On Tue, 3 Dec 2013 13:55:31 +0100, Rafael Knuth 
wrote:

for x in range(2009, 2014):



PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]


You can zip two iterators together and iterate through the resultant
iterable of tuples.
for x, country in zip (range (2009, 2014)):
Print (x, country)



You can't zip one iterator and it's print :)

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Rafael Knuth
Hej there,

> Loop around your list using the enumerate builtin function and an
> appropriate value for start, see
> http://docs.python.org/3/library/functions.html#enumerate

thanks! That hint was very helpful, and I rewrote the program as
follows (I learned how to enumerate just yesterday and I figured out
you can do the same with a range(len(list)) ... so here's how I solved
my issue:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Year = 2009
Backpackers = 100
for Country in range(len(PopularCountries)):
Year += 1
Backpackers = Backpackers*1.15
print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (Year, Backpackers,
PopularCountries[Country]))

>>>
In 2010 there were 115 backpackers worldwide and their most
popular country was Brazil.
In 2011 there were 1322500 backpackers worldwide and their most
popular country was China.
In 2012 there were 1520874 backpackers worldwide and their most
popular country was France.
In 2013 there were 1749006 backpackers worldwide and their most
popular country was India.
In 2014 there were 2011357 backpackers worldwide and their most
popular country was Vietnam.

I will now try to further enhance my program by adding a second list
to the loop.
Again, thank you all!

Raf
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Mark Lawrence

On 03/12/2013 14:11, Rafael Knuth wrote:

Hej there,


Loop around your list using the enumerate builtin function and an
appropriate value for start, see
http://docs.python.org/3/library/functions.html#enumerate


thanks! That hint was very helpful, and I rewrote the program as
follows (I learned how to enumerate just yesterday and I figured out
you can do the same with a range(len(list)) ... so here's how I solved
my issue:


That's very poor coding, if you're given a function that does exactly 
what you want, why rewrite it and worse still, get it wrong?




PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Year = 2009
Backpackers = 100
for Country in range(len(PopularCountries)):
 Year += 1
 Backpackers = Backpackers*1.15
 print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (Year, Backpackers,
PopularCountries[Country]))




In 2010 there were 115 backpackers worldwide and their most
popular country was Brazil.


Whoops, what happened to 2009?


In 2011 there were 1322500 backpackers worldwide and their most
popular country was China.
In 2012 there were 1520874 backpackers worldwide and their most
popular country was France.
In 2013 there were 1749006 backpackers worldwide and their most
popular country was India.
In 2014 there were 2011357 backpackers worldwide and their most
popular country was Vietnam.

I will now try to further enhance my program by adding a second list
to the loop.


What do you mean by "enhance", get the output correct or make it even 
worse? :)



Again, thank you all!

Raf


So here's the code with enumerate.

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
for x, PopularCountry in enumerate(PopularCountries, start=2009):
Backpackers = Backpackers*1.15
print("In %d there were %d backpackers worldwide and their most 
popular country was %s." % (x, Backpackers, PopularCountry))


In 2009 there were 115 backpackers worldwide and their most popular 
country was Brazil.
In 2010 there were 1322500 backpackers worldwide and their most popular 
country was China.
In 2011 there were 1520874 backpackers worldwide and their most popular 
country was France.
In 2012 there were 1749006 backpackers worldwide and their most popular 
country was India.
In 2013 there were 2011357 backpackers worldwide and their most popular 
country was Vietnam.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Rafael Knuth
Hej there,

> That's very poor coding, if you're given a function that does exactly what
> you want, why rewrite it and worse still, get it wrong?

I don't quite understand. I took that advice, tried it - it worked,
and then I figured out there's also another way to get there.
The output from the "for Country in range(len(PopularCountries))" is
exactly the same as with "enumerate", or am I missing something here?

>> PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
>> Year = 2009
>> Backpackers = 100
>> for Country in range(len(PopularCountries)):
>>  Year += 1
>>  Backpackers = Backpackers*1.15
>>  print("In %d there were %d backpackers worldwide and their most
>> popular country was %s." % (Year, Backpackers,
>> PopularCountries[Country]))
>>
>
>> In 2010 there were 115 backpackers worldwide and their most
>> popular country was Brazil.
>
>
> Whoops, what happened to 2009?

Typo ;-)

>> In 2011 there were 1322500 backpackers worldwide and their most
>> popular country was China.
>> In 2012 there were 1520874 backpackers worldwide and their most
>> popular country was France.
>> In 2013 there were 1749006 backpackers worldwide and their most
>> popular country was India.
>> In 2014 there were 2011357 backpackers worldwide and their most
>> popular country was Vietnam.
>>
>> I will now try to further enhance my program by adding a second list
>> to the loop.
>
>
> What do you mean by "enhance", get the output correct or make it even worse?
> :)

Very funny. Couldn't stop laughing ;-)

> So here's the code with enumerate.
>
>
> PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
> Backpackers = 100
> for x, PopularCountry in enumerate(PopularCountries, start=2009):
> Backpackers = Backpackers*1.15
> print("In %d there were %d backpackers worldwide and their most popular
> country was %s." % (x, Backpackers, PopularCountry))
>
> In 2009 there were 115 backpackers worldwide and their most popular
> country was Brazil.
> In 2010 there were 1322500 backpackers worldwide and their most popular
> country was China.
> In 2011 there were 1520874 backpackers worldwide and their most popular
> country was France.
> In 2012 there were 1749006 backpackers worldwide and their most popular
> country was India.
> In 2013 there were 2011357 backpackers worldwide and their most popular
> country was Vietnam.

Thanks. Just one last question: Is there a way to loop through an
arbitrary number of lists at the same time?
Say, if I wanted to loop through the most popular travel guides in
each year in addition to most popular country? I couldn't figure that
out by myself.
Would that be doable with "enumerate" as well?

All the best,

Raf
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Dave Angel
On Tue, 03 Dec 2013 13:23:21 +, Mark Lawrence 
 wrote:

On 03/12/2013 13:11, Dave Angel wrote:
> On Tue, 3 Dec 2013 13:55:31 +0100, Rafael Knuth 


>> PopularCountries = ["Brazil", "China", "France", "India", 

"Vietnam"]

> You can zip two iterators together and iterate through the 

resultant

> iterable of tuples.
> for x, country in zip (range (2009, 2014)):
> Print (x, country)



You can't zip one iterator and it's print :)


for x, country in zip ( range (2009,2014), PopularCountries):
print (x, country)

And yes, Rafael,  you can zip together any number of iterators this 
way.


--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Rafael Knuth
> for x, country in zip ( range (2009,2014), PopularCountries):
> print (x, country)
>
> And yes, Rafael,  you can zip together any number of iterators this way.
>
> --
> DaveA

Thanks Dave. Got it!

Raf
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Mark Lawrence

On 03/12/2013 15:03, Rafael Knuth wrote:

Hej there,


That's very poor coding, if you're given a function that does exactly what
you want, why rewrite it and worse still, get it wrong?


I don't quite understand. I took that advice, tried it - it worked,
and then I figured out there's also another way to get there.
The output from the "for Country in range(len(PopularCountries))" is
exactly the same as with "enumerate", or am I missing something here?



We've already established that you've an "off by one" error in the year, 
but let's do a closer analysis of your code and mine.



PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Year = 2009
Backpackers = 100
for Country in range(len(PopularCountries)):


"Country" here is actually an index into the list of countries.


  Year += 1


Here's your "off by one" error, it should come after the print function.


  Backpackers = Backpackers*1.15
  print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (Year, Backpackers,
PopularCountries[Country]))


To fetch the country and print it you use your poorly named Country 
index to go back into the list of countries.





PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
for x, PopularCountry in enumerate(PopularCountries, start=2009):
 Backpackers = Backpackers*1.15
 print("In %d there were %d backpackers worldwide and their most popular
country was %s." % (x, Backpackers, PopularCountry))


Here we get the properly initialised index and country in one hit and 
print them directly.  Do you see the difference?  Given that code is 
read far more times than it's written I'd much prefer my version, 
although I'm obviously biased :)  It might not make much odds in a small 
example like this, but in a major project running into possibly millions 
of lines of code you're talking a lot of time and hence money.




Thanks. Just one last question: Is there a way to loop through an
arbitrary number of lists at the same time?
Say, if I wanted to loop through the most popular travel guides in
each year in addition to most popular country? I couldn't figure that
out by myself.
Would that be doable with "enumerate" as well?



Yes, I've used enumerate with multiple lists, by using the zip function 
Dave Angel mentioned earlier in this thread.  An alternative is to 
perhaps use a list of lists.  Almost inevitably, there is a data 
structure within the standard library or somewhere online (e.g. pypi) 
that'll do the job you want, including looping, without having to 
reinvent wheels.  And if you have to reinvent wheels, it's usually 
better to make them round :)


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Rafael Knuth
> We've already established that you've an "off by one" error in the year, but
> let's do a closer analysis of your code and mine.

Ok, got it - thank you for the clarification Mark.
No more questions for today, I learned a lot - thank you all!

:-)

All  the best,

Raf
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Steven D'Aprano
On Tue, Dec 03, 2013 at 01:55:31PM +0100, Rafael Knuth wrote:
> Hej there,
> 
> I am writing a little throw away program in order to better understand
> how I can loop through a variable and a list at the same time.

I'm afraid you may be slightly confused as to what is going on with 
for-loops in Python. For-loops in Python *always*, without exception, 
loop over the items of a "list".

I've put list in quotation marks here because it's not necessarily an 
*actual* list, but it can be anything which is list-like. But the 
important thing is that Python doesn't have anything like the C-style 
for loops:

for ( x = 0; x < 10; x++ ) {
  something;
  }


or Pascal:

for i := 1 to 100 do
  something;


So what is going on when you use a for-loop in Python? 

Python for-loops are what some other languages call a "for-each loop", 
it expects a collection or sequence of values, and then sets the loop 
variable to each value in turn. So we can loop over a list:

for item in [2, "three", 23]:
print(item)

will print each of 2, "three" and 23. This is equivalent to this 
pseudo-code:

  sequence = [2, "three", 23]
  get the first item of sequence  # in this case, 2
  set loop variable "item" equal to that item
  execute the body of the loop
  get the second item of sequence  # "three"
  set loop variable "item" to that item
  execute the body of the loop

and so on, until you run out of items.

It isn't just lists that this process works on. It works with strings:

for char in "Hello":
print(char)

will print "H", "e", "l", "l", and "o".

It works with tuples, sets, dicts, and many other objects. It even works 
with range objects.

What is a range object? Range objects are a special sort of object which 
are designed to provide a series of integer values. In Python 2, there 
are two related functions:

range, which actually returns a list

xrange, which is like range except it generates values lazily, 
only when requested.

So in Python 2, range(90) creates a list containing the first 900 
thousand integers. xrange(90) creates a special object which 
prepares to create those 900 thousand integers, but doesn't actually do 
so until needed. So xrange is more memory-efficient in Python 2.

In Python 3, xrange has been renamed to just plain old "range", and the 
old range that creates a list up front is gone.

But the important thing to learn from this is that range (or xrange) is 
just an object, a sequence object like lists and tuples and strings and 
all sorts of other things. You can't do this in Pascal:

values := 1 to 10;
for i := values do
  something;


but in Python we can:

values = list(range(10))
values.append(20)
values.append(30)
for value in values:
print(value)


prints 0, 1, 2, through 9, 20, 30.

The values in the sequence can be anything, including tuples:

py> for item in [(1, 2), (3, 4), (5, 6)]:
... x = item[0]
... y = item[1]
... print(x+y)
...
3
7
11

There's a shorter way to write that:

py> for x,y in [(1, 2), (3, 4), (5, 6)]:
... print(x+y)
...
3
7
11



You don't have to write out the pairs values by hand. Python comes with 
some functions to join two or more sets of values into one. The most 
important is the zip() function, so called because it "zips up" two 
sequences into a single sequence. So we can do this:

py> for x, y in zip([1, 3, 5], [2, 4, 6]):
... print(x+y)
3
7
11

In this case, zip() takes the two lists and returns a single sequence of 
values (1,2), (3,4) and (5,6).

The zip function works with any number of sequences:

zip([1, 2, 3], "abc", "XYZ", (4, 5, 6), range(50, 100))

will give:

(1, "a", "X", 4, 50)
(2, "b", "Y", 5, 51)
(3, "c", "Z", 6, 52)


Here's a modification to your earlier code using zip:


PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
msg = "In %d there were %d backpackers worldwide and their most popular country 
was %s."
for year, country in zip(range(2009, 2014), PopularCountries):
Backpackers = Backpackers*1.15
print(msg % (year, Backpackers, country))




Hope this helps!



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Steven D'Aprano
On Tue, Dec 03, 2013 at 04:03:55PM +0100, Rafael Knuth wrote:
> Hej there,
> 
> > That's very poor coding, if you're given a function that does exactly what
> > you want, why rewrite it and worse still, get it wrong?
> 
> I don't quite understand. I took that advice, tried it - it worked,
> and then I figured out there's also another way to get there.
> The output from the "for Country in range(len(PopularCountries))" is
> exactly the same as with "enumerate", or am I missing something here?

Looping over indexes, then extracting the item you actually want, is 
considered poor style. It is slower, less efficient, more work to write, 
harder to read. Instead of writing this:


for i in range(len(some_list)):
item = some_list[i]
process(item)


it is more "Pythonic" to do this:

for item in some_list:
process(item)



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Mark Lawrence

On 03/12/2013 15:51, Steven D'Aprano wrote:


Here's a modification to your earlier code using zip:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
msg = "In %d there were %d backpackers worldwide and their most popular country was 
%s."
for year, country in zip(range(2009, 2014), PopularCountries):
 Backpackers = Backpackers*1.15
 print(msg % (year, Backpackers, country))



So much for "There should be one-- and preferably only one --obvious way 
to do it." :)


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Steven D'Aprano
On Tue, Dec 03, 2013 at 04:04:33PM +, Mark Lawrence wrote:
> On 03/12/2013 15:51, Steven D'Aprano wrote:
> >
> >Here's a modification to your earlier code using zip:
> >
> >PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
> >Backpackers = 100
> >msg = "In %d there were %d backpackers worldwide and their most popular 
> >country was %s."
> >for year, country in zip(range(2009, 2014), PopularCountries):
> > Backpackers = Backpackers*1.15
> > print(msg % (year, Backpackers, country))
> >
> 
> So much for "There should be one-- and preferably only one --obvious way 
> to do it." :)


Huh? Using zip to iterate over multiple sequences in parallel *is* the 
obvious way to, um, iterate over multiple sequences in parallel.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Mark Lawrence

On 03/12/2013 16:15, Steven D'Aprano wrote:

On Tue, Dec 03, 2013 at 04:04:33PM +, Mark Lawrence wrote:

On 03/12/2013 15:51, Steven D'Aprano wrote:


Here's a modification to your earlier code using zip:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
msg = "In %d there were %d backpackers worldwide and their most popular
country was %s."
for year, country in zip(range(2009, 2014), PopularCountries):
 Backpackers = Backpackers*1.15
 print(msg % (year, Backpackers, country))



So much for "There should be one-- and preferably only one --obvious way
to do it." :)



Huh? Using zip to iterate over multiple sequences in parallel *is* the
obvious way to, um, iterate over multiple sequences in parallel.



Correct, except that there never was a requirement to iterate over 
multiple sequences in parallel.  The OP originally asked for "... how I 
can loop through a variable and a list at the same time."  I immediately 
thought enumerate, both yourself and Dave Angel thought zip.  We get the 
same output with different ways of doing it, hence my comment above.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner's question: Looping through variable & list simultaneously

2013-12-03 Thread Alan Gauld

On 03/12/13 12:55, Rafael Knuth wrote:


Now I want to enhance that program a bit by adding the most popular
country in each year. Here's what I want to get as the output:




In 2009 there were 115 backpackers worldwide and their most
popular country was Brazil.

...

 From all my iterations there's only one that came at least halfway
close to my desired result:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 100
for x in range(2009, 2014):
 Backpackers = Backpackers*1.15
 PopularCountries = PopularCountries.pop()
 print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (x, Backpackers, PopularCountries))

It loops only once through "Backpackers" and "PopularCountries"
(starting with the last item on the list though) and then it breaks:


Others have pointed to better solutions. However, the reason this one 
breaks is that you are using the same variable name for your country 
inside the loop as for the collection.


  PopularCountries = PopularCountries.pop()

This replaces the collection with the last country; a string.
That's why you get the error. Now, if you use a different name
like:

  PopularCountry = PopularCountries.pop()

And print the new name the problem should go away.


Is there a way to use pop() to iterate through the list in a correct
order (starting on the left side instead on the right)?


Yes, just provide an index of zero:

  PopularCountry = PopularCountries.pop(0)


If not: What alternative would you suggest?


See the other answers. zip is probably better.

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need a hint

2013-12-03 Thread Byron Ruffin
What I am having trouble with is finding a way to say:  if lastName appears
more than once, print something.

I ran a bit of code:
For x in lastname
  If lastname = udall
   Print something

This prints x twice.

I think what I might be hung up on is understanding the ways that I can use
a loop.  I know I need to loop through the list of names, which I have, and
set a condition dor the apppearance of a string occurring more than once in
a list but I don't know how to translate this to code.   How do I say: if
you see it twice, do something?
On 2 December 2013 02:25, Byron Ruffin  wrote:
>
> The following program works and does what I want except for one last
problem
> I need to handle.   The program reads a txt file of senators and their
> associated states and when I input the last name it gives me their state.
> The problem is "Udall".  There are two of them.  The txt file is read by
> line and put into a dictionary with the names split.  I need a process to
> handle duplicate names.  Preferably one that will always work even if the
> txt file was changed/updated.  I don't want the process to handle the name
> "Udall" specifically.   For a duplicate name I would like to tell the user
> it is not a unique last name and then tell them to enter first name and
then
> return the state of that senator.

You're currently doing this:

> senateInfo = {}
> senateInfo[lastName] = state

Instead of storing just a state in the dict you could store a list of
states e.g.:

senateInfo[lastName] = [state]

Then when you find a lastName that is already in the dict you can do:

senateInfo[lastName].append(state)

to append the new state to the existing list of states. You'll need a
way to test if a particular lastName is already in the dict e.g.:

if lastName in senateInfo:


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need a hint

2013-12-03 Thread Steven D'Aprano
On Tue, Dec 03, 2013 at 11:55:30AM -0600, Byron Ruffin wrote:
> What I am having trouble with is finding a way to say:  if lastName appears
> more than once, print something.
> 
> I ran a bit of code:
> For x in lastname
>   If lastname = udall
>Print something

You most certainly did not run that. That's not Python code. Precision 
and accuracy is vital when programming. Please tell us what you 
*actually* ran, not some vague summary which may or may not be in the 
right ballpark.

Copy and paste is your friend here: copy and paste the block of code you 
ran, don't re-type it from memory.

> This prints x twice.
> 
> I think what I might be hung up on is understanding the ways that I can use
> a loop.  I know I need to loop through the list of names, which I have, and
> set a condition dor the apppearance of a string occurring more than once in
> a list but I don't know how to translate this to code.   How do I say: if
> you see it twice, do something?

How do you know you've seen it twice? You have to remember the things 
you've seen before. The best way to do this is with a set, if possible, 
or if not, a list.

already_seen = set()
for name in last_names:
if name in already_seen:
print("Already seen", name)
else:
already_seen.add(name)



Here's another way, not recommended because it will be slow for large 
numbers of names. (But if you only have a few names, it will be okay.

for name in last_names:
n = last_names.count(name)
print(name, "appears %d times" % n)


Can you combine the two so that the number of times a name appears is 
only printed the first time it is seen?



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor