7 Replies - 1637 Views - Last Post: 04 March 2013 - 09:09 PM Rate Topic: -----

#1 klaus1216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 13

While loop help with lists

Posted 04 March 2013 - 07:59 PM

Make a list of words in a sentence. No punctuation should be attached to a “word” in your list, e.g., “end.” is not a correct word, but “end” is.
(a) Use a while loop.
import string

astring = "what up homie."
astring = astring.lower()
alist = astring.split()

t = 0
s = []
while t <= len(alist) - 1:
    i = 0
    while i <= len(alist[t]) - 1:
        if alist[t][i] in string.punctuation:
            v = alist[t].strip(string.punctuation)
            s = s + [v]
            i = i + 1
        else:
        s = s + [alist[t]]
        t = t + 1
        
            
print(s)


I am getting no output

This post has been edited by jon.kiparsky: 04 March 2013 - 08:30 PM
Reason for edit:: code tags, bitte!


Is This A Good Question/Topic? 0
  • +

Replies To: While loop help with lists

#2 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: While loop help with lists

Posted 04 March 2013 - 08:18 PM

Sorry if the usage of translation is confusing but its fast and it works.

Give this a go, with comments for explanation:
import string

astring = "what up homie."
# Now split the string and perform lower on each part.
# with no arguments split will split at each whitespace
# character so strip should not be needed. If it still 
# is then just add it after .lower()
alist = [word.lower() for word in astring.split()]

pos = 0
words = []
while pos < len(alist):
    # Use a fast C run translation of the string that 
    # replaces all punction with empty strings.
    word = alist[pos].translate(string.maketrans("",""), string.punctuation)
    # add the word to the words list
    # you should generally always use append or extend 
    # or even insert to add things to lists.
    words.append(word)
    pos += 1
    
print(words)



It works on my end, here is some info on translate for you http://www.tutorials...g_translate.htm i hope that helps, there are other ways to replace the punctuation but this one works quite well i think.

Hope it helps mate!
Nekroze
Was This Post Helpful? 0
  • +
  • -

#3 klaus1216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 13

Re: While loop help with lists

Posted 04 March 2013 - 08:21 PM

i'm in a intro python course, so what you have written is not acceptable for the class. thanks anyway
Was This Post Helpful? 0
  • +
  • -

#4 klaus1216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-March 13

Re: While loop help with lists

Posted 04 March 2013 - 08:28 PM

also i am only allowed to use while loop
Was This Post Helpful? 0
  • +
  • -

#5 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: While loop help with lists

Posted 04 March 2013 - 08:33 PM

the list comprehension can easily be re-written to:
alist = []
for word in astring.split():
    alist.append(word.lower())


and the translation part i found with a simple google of literally "Python string remove punctuation" so i don't see why that is to advanced.

But it can also be simplified or done other ways. The next most obvious way is to use a regular expression but that's much more advanced.

Just because your main loop has to be a while loop doesn't mean you have to use ONLY while loops right?

So you can always do this inside the while loop:
word = alist[pos]
for punc in string.punctuation:
    word = word.replace(punc, '')



This will be much slower however but that shouldn't matter in your case. The complete rewrite would look like this:
import string

astring = "what up homie."
# Now split the string and perform lower on each part.
# with no arguments split will split at each whitespace
# character so strip should not be needed. If it still
# is then just add it after .lower()
alist = []
for word in astring.split():
    alist.append(word.lower())

pos = 0
words = []
while pos < len(alist):
    # replaces all punction with empty strings.
    word = alist[pos]
    for punc in string.punctuation:
        word = word.replace(punc, '')
    # add the word to the words list
    # you should generally always use append or extend
    # or even insert to add things to lists.
    words.append(word)
    pos += 1
    
print(words)


Also those for loops in the recent update can easily be changed to while loops.

EDITED: small error in code, fixed it.

This post has been edited by Nekroze: 04 March 2013 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: While loop help with lists

Posted 04 March 2013 - 08:57 PM

I am rather bored at the moment, here is the rewrite of the last edit but using all while loops... I don't like just giving out all the answers but like i said, nothing else to do atm.

import string

astring = "what up homie."
# Now split the string and perform lower on each part.
# with no arguments split will split at each whitespace
# character so strip should not be needed. If it still
# is then just add it after .lower()
alist = []
split = astring.split()
pos = 0
while pos < len(split):
    alist.append(split[pos].lower())
    pos += 1

pos = 0
words = []
while pos < len(alist):
    # replaces all punction with empty strings.
    word = alist[pos]
    puncpos = 0
    while puncpos < len(string.punctuation):
        word = word.replace(string.punctuation[puncpos], '')
        puncpos += 1
    # add the word to the words list
    # you should generally always use append or extend
    # or even insert to add things to lists.
    words.append(word)
    pos += 1
    
print(words)


EDIT: interestingly enough this is barely slower then the first implementation i posted with translation... didn't expect that.

This post has been edited by Nekroze: 04 March 2013 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7626
  • View blog
  • Posts: 12,856
  • Joined: 19-March 11

Re: While loop help with lists

Posted 04 March 2013 - 09:05 PM

A while loop is completely the wrong tool for this job in python - you should be sure you understand that before proceeding: you won't do it this way in real life.

But assuming that you're doing this in order to get a grasp on the way we did it in the bad old days, let's continue.

The first thing you need to know is that there is no tax on characters in variable names. They are completely free, so you should use them freely. Make your variable names mean something, especially if you're asking someone else to read your code.

This is not needed:
alist = astring.split()



You can treat a string pretty much as a list of characters, for example:
while i < len(somestring):
    print somestring[i]
    i += 1



This line is correct, but wrong:
while t <= len(alist) - 1:


This is equivalent to the simpler expression
while t < len(alist):


so use that. Make your code clear, and it will reward you by showing you where it's not correct.


    while i <= len(alist[t]) - 1:
        if alist[t][i] in string.punctuation:
            v = alist[t].strip(string.punctuation)
            s = s + [v]
            i = i + 1
        else:
        s = s + [alist[t]]
        t = t + 1



I think I see what you're trying to do here, but wouldn't it be simpler to do without the inner while loop?

Think of this as a very simple parser: You have two types of inputs and two states: either a char is in the set [a-zA-Z] or it's not, and either you're currently building a word or you're not. If it is, add the character to the word you're currently building and move on. If it isn't, close off the current word (if any) and put it on your list of words in the sentence, and move on.

This may help you simplify things so your errors pop out at you.
I have to admit, as written the errors did not leap out at me, so the simplification is probably necessary.

View PostNekroze, on 04 March 2013 - 10:57 PM, said:

I am rather bored at the moment,


Evidently, but that doesn't mean it makes sense to pelt the guy with implementations - let him do his own homework!


Quote

EDIT: interestingly enough this is barely slower then the first implementation i posted with translation... didn't expect that.


You'd have to screw that pooch pretty badly to see a timing difference on handling a 14-character string.
Was This Post Helpful? 0
  • +
  • -

#8 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

Re: While loop help with lists

Posted 04 March 2013 - 09:09 PM

Oh yeah i forgot that split can take a sequence... Last re-write, without comments this time. I promise i will stop and find something else to do :sweatdrop:

import string

astring = "what up homie."
alist = []
split = astring.split()
pos = 0
while pos < len(split):
    alist.append(split[pos].lower())
    pos += 1

pos = 0
words = []
while pos < len(alist):
    word = alist[pos].strip(string.punctuation)
    words.append(word)
    pos += 1
    
print(words)


jon is write, as always, you should use more descriptive variable names.

EDIT: as jon says, in real python you can almost always avoid using while loops because python for loops are so powerful especially with list comprehensions.

In the real word this would be the way i would do it (just for reference and learning):
output = [word.lower().strip(string.punctuation) for word in astring.split()]


This post has been edited by Nekroze: 04 March 2013 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1