Developer Ecofunds | 25 Jun 2012 15:33
Picon

List Methods, String Slices and For loops

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)
  x_list.extend(words)
  return x_list

##############################

The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work.

Following is the solution of the problem. I can understand it, I just can't understand why my code does not work.

#############################
def front_x(words):
  x_list = []
  other_list = []
  for w in words:
    if w.startswith('x'):
      x_list.append(w)
    else:
      other_list.append(w)
  return sorted(x_list) + sorted(other_list)
##############################



_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Developer Ecofunds | 25 Jun 2012 15:35
Picon

Re: List Methods, String Slices and For loops

Soory by the new message with same question. The goup said my subject was messy and the previous message was denied. Please ignore.

2012/6/25 Developer Ecofunds <ecofunds.developer <at> gmail.com>
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)
  x_list.extend(words)
  return x_list

##############################

The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work.

Following is the solution of the problem. I can understand it, I just can't understand why my code does not work.

#############################
def front_x(words):
  x_list = []
  other_list = []
  for w in words:
    if w.startswith('x'):
      x_list.append(w)
    else:
      other_list.append(w)
  return sorted(x_list) + sorted(other_list)
##############################




_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Craig Cesareo | 25 Jun 2012 15:39
Favicon

Re: List Methods, String Slices and For loops

In case you didn't get my reply on the previous message...

Try putting.    [:]      After "words" in the for loop line. What's happening is you're removing words from the list you are iterating through and it's messing with the loop. The empty indice I'm having you insert makes an in place copy of the list so your remove doesn't effect it. 

On Jun 25, 2012, at 6:37 AM, "Developer Ecofunds" <ecofunds.developer <at> gmail.com> wrote:

Soory by the new message with same question. The goup said my subject was messy and the previous message was denied. Please ignore.

2012/6/25 Developer Ecofunds <ecofunds.developer <at> gmail.com>
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)
  x_list.extend(words)
  return x_list

##############################

The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work.

Following is the solution of the problem. I can understand it, I just can't understand why my code does not work.

#############################
def front_x(words):
  x_list = []
  other_list = []
  for w in words:
    if w.startswith('x'):
      x_list.append(w)
    else:
      other_list.append(w)
  return sorted(x_list) + sorted(other_list)
##############################

To download all the exercises, access: http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip 

Thank y'all.




_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Walter Prins | 25 Jun 2012 15:51
Picon

Re: List Methods, String Slices and For loops

Hi,

Just one tangential comment:

On 25 June 2012 14:33, Developer Ecofunds <ecofunds.developer <at> gmail.com> wrote:
> I'm using python 2.5 because it's the one works with Google App Engine
> <https://developers.google.com/appengine/docs/python/gettingstarted/>

GAE works with Python 2.7 also:
https://developers.google.com/appengine/docs/python/gettingstartedpython27/
https://developers.google.com/appengine/docs/python/python27/newin27

(You can in fact see this in the link you posted also in the left-hand
sidebar below the part about Python 2.5 that you referenced.)

As you'll see there's lots of features only available on 2.7, so it's
probably highly preferable to work on 2.7 rather than 2.5.

Cheers,

Walter
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Gmane