Re: [Tutor] Recursive assignment in nested lists

2012-08-04 Thread Puneeth Chaganti
On Sat, Aug 4, 2012 at 12:28 PM, Alonzo Quijote
alonzo.quij...@gmail.com wrote:
 Is there a way to define a function which takes
a list (of lists),
a position specified by a list of integers [i0,i1,...,in], and
a value
 and returns the result of setting
 list[i0][i1]...[in]=value

 The following function works for positions up to length 3 only.
 Is it possible to write a general function that does this?

 def setValueAtPosition(list,pos,value):
 if len(pos)==1:
 list[pos[0]]=value
 elif len(pos)==2:
 list[pos[0]][pos[1]]=value
 elif len(pos)==3:
 list[pos[0]][pos[1]][pos[2]]=value
 return list

Something like this, should work :

def setValueAtPosition(my_list, pos, value):
sub_list = my_list
for i in pos[:-1]:
sub_list = sub_list[i]
sub_list[pos[-1]] = value
return my_list

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


Re: [Tutor] Help please!

2012-07-30 Thread Puneeth Chaganti
On Mon, Jul 30, 2012 at 9:35 PM, Victoria Homsy victoriaho...@yahoo.com wrote:
 Hi! I am a new Python user, and would really appreciate some help. My code
 is as follows:

 from sys import argvs
 script, mum_mood, dad_mood = argvs

 # my own function
 def dad_and_mum_mood(mum_mood, dad_mood):
 print If both mum and dad are in a good mood, all is good.
 print If one is and one isn't, all is good.
 print If both are in a bad mood, not so good.
 print Mum is in a %s mood % (mum_mood)
 print Dad is in a %s mood % (dad_mood)
 print Where does that leave us?


 dad_and_mum_mood(mum_mood, dad_mood)


 I am just trying to get get the information mum_mood and dad_mood from the
 argvs (written into the command line), but I get the error ImportError:
 cannot import name argvs.

The problem is exactly what the error message says.  argvs should
really be argv.

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


Re: [Tutor] Working with lists - why does my script not work?

2012-06-25 Thread Puneeth Chaganti
On Mon, Jun 25, 2012 at 6:51 PM, Developer Ecofunds
ecofunds.develo...@gmail.com wrote:
 Hello guys,

 I'd like to ask you a little question about a script I did and isn't working
 properly.
 It is one excercise from googles python classes
 http://code.google.com/edu/languages/google-python-class/set-up.html

 I'm using python 2.5 because it's the one works with Google App Engine
 https://developers.google.com/appengine/docs/python/gettingstarted/

 This is the problem:

 ##3

 # B. front_x
 # Given a list of strings, return a list with the strings
 # in sorted order, except group all the strings that begin with 'x' first.
 # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
 # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
 # Hint: this can be done by making 2 lists and sorting each of them
 # before combining them.

 # This is the code I did -- Romulo.

 def front_x(words):
   x_list = []
   for string in words:
     if string[0]=='x':
       x_list.append(string)
       words.remove(string)
   sorted(words)
   sorted(x_list)

`sorted`[0] returns a new list unlike `sort`[1], which sorts a list in-place.

Something like this, would work:

words = sorted(words)
x_list = sorted(x_list)

Also, in your code, you are changing the `words` list, while you are
iterating over it, and this will cause problems while you iterate over
the list.  Generate a copy of the list and iterate over it by
iterating over `words[:]` instead of `words`.

Hope that helps,
Puneeth

[0] - http://docs.python.org/library/functions.html#sorted
[1] - http://docs.python.org/tutorial/datastructures.html#more-on-lists
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to interact with users on IRC using Python

2012-04-16 Thread Puneeth Chaganti
On Mon, Apr 16, 2012 at 8:41 PM, Surya K sur...@live.com wrote:
[snip]
 So, what IRC commands should I use and how do I typically implement them?

I found this useful --

http://www.irchelp.org/irchelp/rfc/rfc.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] write list to columns

2012-04-11 Thread Puneeth Chaganti
On Thu, Apr 12, 2012 at 8:01 AM, questions anon
questions.a...@gmail.com wrote:
 I am trying to simply write a list of values to txt file in one column.
 I would then like to add another list in a second column.
 Somehow they only appear as one long row.
 Any feedback will be greatly appreciated.

You will need to write each line of the file separately, with the
formatting you desire for each line.
The following example may help.


A = range(5)
B = range(5, 10)

records = zip(A, B)
output_format = '%s %s'

with open('output.txt', 'w') as f:
for record in records:
f.write(output_format %(record))
f.write('\n')


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