Print lines until to a certain number of characters

This is the place for queries that don't fit in any of the other categories.

Print lines until to a certain number of characters

Postby lmr1405 » Wed Oct 30, 2013 3:34 pm

Hi all:

I have the following mini program:

Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"
words=sorted(sentence.split(" "), key=str.lower)

for w in words:
   if len(w) >= 4 and len(w) <=8:
      print w, len(w)



I want to print all of the words that are greater or equal to 4 and less than or equal to 8.
I am not sure where I am going wrong here, because it does not obtain ANY result.
Any suggestions?
Last edited by lmr1405 on Wed Oct 30, 2013 3:55 pm, edited 1 time in total.
lmr1405
 
Posts: 22
Joined: Fri Oct 25, 2013 9:49 am

Re: Print lines until to a certain number of characters

Postby dboxall123 » Wed Oct 30, 2013 3:44 pm

Try:
Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"
words=sentence.split()

for w in words:
   if len(w) >= 4 and len(w) <=6:
      print w, len(w)
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Print lines until to a certain number of characters

Postby lmr1405 » Wed Oct 30, 2013 3:55 pm

Thanks for the response, but I must mention that the sort must be included.
Let me make more clear my question:

For instance: lets consider only one condition first:

Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"
words=sorted(sentence.split(" "), key=str.lower)
for w in words:
   if len(w) >= 4:
      print w, len(w)


produces the following output:

awesome 7
Currently 9
friend. 7
help 4
name 4
PYTHON. 7
studying 8
want 4
your 4


we want to add the second condition to stop at len(w) == 8.

How can the original code be modified to have the following desired output.
awesome 7
Currently 9
friend. 7
help 4
name 4
PYTHON. 7
studying 8
lmr1405
 
Posts: 22
Joined: Fri Oct 25, 2013 9:49 am

Re: Print lines until to a certain number of characters

Postby lmr1405 » Wed Oct 30, 2013 4:01 pm

I have tried "while"

For instance:

Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"
words=sorted(sentence.split(" "), key=str.lower)
for w in words:
   while len(w) >= 4 and len(w) <= 9:
      print w, len(w)
   else:
            break


which also does not seem to produce an error nor an output...
lmr1405
 
Posts: 22
Joined: Fri Oct 25, 2013 9:49 am

Re: Print lines until to a certain number of characters

Postby metulburr » Wed Oct 30, 2013 4:09 pm

your previous example of desired output and the condiition you have do not make sense. Your example of the desired output shows you only want the first 8 words, whereas your code looks like you want only words between 4-8 characters regardless of its position in the sentance. Which one are looking for?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1295
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Print lines until to a certain number of characters

Postby metulburr » Wed Oct 30, 2013 4:19 pm

I want to print all of the words that are greater or equal to 4 and less than or equal to 8.


Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"

for word in sentence.split(' '):
    length = len(word)
    if length >= 4 and length <= 8:
        print('{} {}'.format(word, length))


output:
Code: Select all
name 4
awesome 7
want 4
your 4
friend. 7
studying 8
PYTHON. 7
help 4
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1295
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Print lines until to a certain number of characters

Postby lmr1405 » Wed Oct 30, 2013 4:29 pm

Thank you, metalburr, solved the problem!
lmr1405
 
Posts: 22
Joined: Fri Oct 25, 2013 9:49 am

Re: Print lines until to a certain number of characters

Postby ochichinyezaboombwa » Thu Oct 31, 2013 4:01 am

Actually your very 1st attempt almost worked, except you obviously wanted
Code: Select all
words=sorted(sentence.split(" "), key=str.islower)

instead of
Code: Select all
words=sorted(sentence.split(" "), key=str.lower)


Or better:
Code: Select all
words=sorted(sentence.split(" "), key=str.islower and str)
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Print lines until to a certain number of characters

Postby micseydel » Thu Oct 31, 2013 4:43 am

ochichinyezaboombwa wrote:Or better:
Code: Select all
words=sorted(sentence.split(" "), key=str.islower and str)

Huh?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1110
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Print lines until to a certain number of characters

Postby lmr1405 » Thu Oct 31, 2013 8:41 am

Hi,
I have been looking and I think the best way to solve my problem is actually with a range loop:

To recall: the original code is

Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"

for word in sentence.split(' '):
    length = len(word)
    if length >= 4 and length <= 8:
        print('{} {}'.format(word, length))


However this produces the following output:

Code: Select all
name 4
awesome 7
want 4
your 4
friend. 7
studying 8
PYTHON. 7
help 4


Where our desired output is actually:

Code: Select all
awesome 7
Currently 9
friend. 7
help 4
name 4
PYTHON. 7
studying 8


I have tried the following range loop:

Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"
words=sorted(sentence.split(" "), key=str.lower)
for word in words:
    length = len(word)
    for length in xrange(word):
       if length >= 4 and length == 8:
           print('{} {}'.format(word, length))





which gives me the following traceback:

Code: Select all
Traceback (most recent call last):
  File "Program.py", line 5, in <module>
    for length in xrange(word):
TypeError: an integer is required



Am I correctly using the range() loop?
Can someone give me insight as to what I am doing wrong?
lmr1405
 
Posts: 22
Joined: Fri Oct 25, 2013 9:49 am

Re: Print lines until to a certain number of characters

Postby metulburr » Thu Oct 31, 2013 10:32 am

Where our desired output is actually:

Code: Select all
awesome 7
Currently 9
friend. 7
help 4
name 4
PYTHON. 7
studying 8
want 4
your 4

What is the condition of this desired output? I see the output, but that doesnt make any sense. When you said:
I want to print all of the words that are greater or equal to 4 and less than or equal to 8.

that makes sense. However, this desired output does not qualify in that condition as the word "Currently" has 9 characters (outside the range of "greater or equal to 4 and less than or equal to 8"). So i am confused as to what the condition is suppose to be to get this desired output?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1295
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Print lines until to a certain number of characters

Postby lmr1405 » Thu Oct 31, 2013 2:25 pm

The condition is to make a loop to print until it reaches a word of a certain character length.

I want to print ANYTHING that is at least four characters -- and continue to print UNTIL it reaches a word with the character length of 9)
lmr1405
 
Posts: 22
Joined: Fri Oct 25, 2013 9:49 am

Re: Print lines until to a certain number of characters

Postby gplayersv » Thu Oct 31, 2013 2:42 pm

Code: Select all
sentence = "Hi my name is awesome and I want to be your friend. Currently I am studying PYTHON. Can you help me"
clear_sentence = "".join([c for c in sentence if c in string.ascii_letters or c == ' ']) # remove special chars
words = [word for word in clear_sentence.split()]
for word in words:
   if len(word) == 9:
      break
   elif len(word) >=4:
      print word
User avatar
gplayersv
 
Posts: 15
Joined: Mon Apr 15, 2013 10:56 am

Re: Print lines until to a certain number of characters

Postby ochichinyezaboombwa » Thu Oct 31, 2013 8:18 pm

micseydel wrote:
ochichinyezaboombwa wrote:Or better:
Code: Select all
words=sorted(sentence.split(" "), key=str.islower and str)

Huh?


Code: Select all
>>> words1=sorted(sentence.split(" "), key=str.lower)
>>> words2=sorted(sentence.split(" "), key=str.islower)
>>> words3=sorted(sentence.split(" "), key=str.islower and str)
>>> words1
['am', 'and', 'awesome', 'be', 'Can', 'Currently', 'friend.', 'help', 'Hi', 'I', 'I', 'is', 'me', 'my', 'name', 'PYTHON.', 'studying', 'to', 'want', 'you', 'your']
>>> words2
['Hi', 'I', 'Currently', 'I', 'PYTHON.', 'Can', 'my', 'name', 'is', 'awesome', 'and', 'want', 'to', 'be', 'your', 'friend.', 'am', 'studying', 'you', 'help', 'me']
>>> words3
['Can', 'Currently', 'Hi', 'I', 'I', 'PYTHON.', 'am', 'and', 'awesome', 'be', 'friend.', 'help', 'is', 'me', 'my', 'name', 'studying', 'to', 'want', 'you', 'your']


I of course cannot read the OP's mind and don't know how (s) would like to sort, but that would be my own preferable way (sort by case and then, inside case, - alphabetically).

Does it make sense?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Print lines until to a certain number of characters

Postby micseydel » Thu Oct 31, 2013 8:33 pm

No, it doesn't
Code: Select all
>>> str.islower and str
<type 'str'>
>>> str.islower
<method 'islower' of 'str' objects>
>>> str and str.islower
<method 'islower' of 'str' objects>

Do you say what I'm saying?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1110
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Print lines until to a certain number of characters

Postby ochichinyezaboombwa » Thu Oct 31, 2013 8:45 pm

Well I am using
Code: Select all
str.islower and str

and not
Code: Select all
str and str.islower

Anyhow: take a look at my words2 vs words3: how do you explain this?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Print lines until to a certain number of characters

Postby micseydel » Thu Oct 31, 2013 9:13 pm

Here's how and works in Python
Code: Select all
def python_and(first, second):
    if not first:
        return first
    return second

So when you say
Code: Select all
str.islower and str

that expression returns the str object.

The expression I quoted is equivalent to plain old sorted.
Code: Select all
>>> words3=sorted(sentence.split(" "), key=str.islower and str)
>>> words4=sorted(sentence.split(" "), key=str)
>>> words5=sorted(sentence.split(" "))
>>> words3 == words4 == words5
True
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1110
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests