473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

two python beginner questions

26
I am new to programming and am trying to teach myself python. I have two questions how do you use a for loop to print out the lines from a list with the line numbers then the word and then I also was wondering what the tag was to print out the three longest, shortest, and middle length words along with their actual lengths
Jun 10 '07 #1
11 2257
bartonc
6,596 Expert 4TB
I am new to programming and am trying to teach myself python. I have two questions how do you use a for loop to print out the lines from a list with the line numbers then the word and then I also was wondering what the tag was to print out the three longest, shortest, and middle length words along with their actual lengths
Off the top of my head, here's the first part:
Expand|Select|Wrap|Line Numbers
  1. strList = ['goodbye', 'cruel', 'world']
  2. for i, word in enumerate(strList):
  3.     print "%d: %s" %(i, word)
  4.  
0: goodbye
1: cruel
2: world
Jun 10 '07 #2
texas22
26
Thanks, that makes sense I couldn't find an example showing to use enumerate to number the lines of the list anywhere, this really helped. What exactly does the line print "%d: %s"%(i, word) mean?
Jun 10 '07 #3
bartonc
6,596 Expert 4TB
Thanks, that makes sense I couldn't find an example showing to use enumerate to number the lines of the list anywhere, this really helped. What exactly does the line print "%d: %s"%(i, word) mean?
That's called "string formatting". Inside quotes the % has special meaning. Here is the documentation.

Basically, it says print a (d)ecimal followed by a ': ' followed by a (s)tring, where the decimal is in i and the string is in word (actually the (i, word) tuple).
Jun 10 '07 #4
bartonc
6,596 Expert 4TB
I am new to programming and am trying to teach myself python. I have two questions how do you use a for loop to print out the lines from a list with the line numbers then the word and then I also was wondering what the tag was to print out the three longest, shortest, and middle length words along with their actual lengths
Here's a quick one for part of the second part:
Expand|Select|Wrap|Line Numbers
  1. >>> most = 0
  2. >>> least = 100
  3. >>> for i, word in enumerate(strList):
  4. ...     most = max(most, len(word))
  5. ...     least = min(least, len(word))
  6. ...     print "%d: %s" %(i, word)
  7. ...     
0: goodbye
1: cruel
2: world
>>> most
7
>>> least
5
>>>
Jun 10 '07 #5
ghostdog74
511 Expert 256MB
before enumerate, there was the long winded way
Expand|Select|Wrap|Line Numbers
  1. for i in range(len(mylist)):
  2.      print "%d: %s" % ( i, mylist[i])
  3.  
Jun 10 '07 #6
bvdet
2,851 Expert Mod 2GB
I am new to programming and am trying to teach myself python. I have two questions how do you use a for loop to print out the lines from a list with the line numbers then the word and then I also was wondering what the tag was to print out the three longest, shortest, and middle length words along with their actual lengths
The first two items in the 2nd part are easy:
Expand|Select|Wrap|Line Numbers
  1. wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
  2. lenList = [len(w) for w in wordList]
  3. wordL = wordList[lenList.index(max(lenList))]
  4. print "The longest word is '%s'. It is %d characters long." % (wordL, len(wordL))
  5.  
  6. wordS = wordList[lenList.index(min(lenList))]
  7. print "The shortest word is '%s'. It is %d characters long." % (wordS, len(wordS))
>>> The longest word is 'comprehension'. It is 13 characters long.
The shortest word is 'a'. It is 1 characters long.

The middle length word will take a little work:
Expand|Select|Wrap|Line Numbers
  1. def midWord(wList):
  2.     dd = {}
  3.     for w in wList:
  4.         if not dd.has_key(len(w)):
  5.             dd[len(w)] = w
  6.     keys = dd.keys()
  7.     keys.sort()
  8.     return dd[keys[len(keys)/2]]
  9.  
  10. wordM = midWord(wordList)
  11. print "The middle length word is '%s'. It is %d characters long." % (wordM, len(wordM))
>>> The middle length word is 'list'. It is 4 characters long.

Maybe one of the other experts can show us a better way.
Jun 10 '07 #7
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
  2. a = [(len(i),i) for i in wordList]
  3. b=sorted(a)
  4. print "Max length is ", b[-1]
  5. print "Shortest length is " , b[0]
  6.  
i don't understand middle length though.
Jun 10 '07 #8
texas22
26
It really helps seeing some examples I able to understand where everything goes and what everything means....so if I wanted to pick the 3 longest/shortest etc.. out of a list where in the code would I tell it to take 3 of the longest etc.

Thanks
Jun 10 '07 #9
bartonc
6,596 Expert 4TB
It really helps seeing some examples I able to understand where everything goes and what everything means....so if I wanted to pick the 3 longest/shortest etc.. out of a list where in the code would I tell it to take 3 of the longest etc.

Thanks
I really like what ghostdog74 has done here with a list comprehension. The next thing to learn about is "slice"ing of iterable objects. Here we'll slice off 3 instead of using an absolute index to the 1st and last:
Expand|Select|Wrap|Line Numbers
  1. wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
  2. a = [(len(i),i) for i in wordList]
  3. b=sorted(a)
  4. print "The longest three are: ", b[-3:]
  5. print "The shortest three are " , b[:3]
Jun 10 '07 #10
texas22
26
Thanks that really helps make sense out of it all so what would the syntax be to find the 3 middle length words in a list?
Jun 10 '07 #11
bvdet
2,851 Expert Mod 2GB
Thanks that really helps make sense out of it all so what would the syntax be to find the 3 middle length words in a list?
I also like what ghostdog74 did with the list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
  2. >>> a = [(len(i),i) for i in wordList]
  3. >>> a.sort()
  4. >>> m = len(wordList)/2
  5. >>> print a[m-1:m+2]
  6. [(3, 'the'), (3, 'use'), (4, 'list')]
  7. >>> 
Jun 11 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Judi Keplar | last post by:
I am currently taking a course to learn Python and was looking for some help. I need to write a Python statement to print a comma- separated repetition of the word, "Spam", written 511 times...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
25
by: Tor Erik Sønvisen | last post by:
Hi I need to browse the socket-module source-code. I believe it's contained in the file socketmodule.c, but I can't locate this file... Where should I look? regards tores
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
1
by: =?UTF-8?Q?=C5=81ukasz_D=C4=85bek?= | last post by:
Hello! I'm newcomer to Python development and I have some questions (I didn't found answers for these): 1. Some bugs at bugs.python.org are assigned but it didn't changed for many months...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.