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
11 2257
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: -
strList = ['goodbye', 'cruel', 'world']
-
for i, word in enumerate(strList):
-
print "%d: %s" %(i, word)
-
0: goodbye
1: cruel
2: world
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?
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).
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: -
>>> most = 0
-
>>> least = 100
-
>>> for i, word in enumerate(strList):
-
... most = max(most, len(word))
-
... least = min(least, len(word))
-
... print "%d: %s" %(i, word)
-
...
0: goodbye
1: cruel
2: world
>>> most
7
>>> least
5
>>>
before enumerate, there was the long winded way -
for i in range(len(mylist)):
-
print "%d: %s" % ( i, mylist[i])
-
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: - wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
-
lenList = [len(w) for w in wordList]
-
wordL = wordList[lenList.index(max(lenList))]
-
print "The longest word is '%s'. It is %d characters long." % (wordL, len(wordL))
-
-
wordS = wordList[lenList.index(min(lenList))]
-
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: - def midWord(wList):
-
dd = {}
-
for w in wList:
-
if not dd.has_key(len(w)):
-
dd[len(w)] = w
-
keys = dd.keys()
-
keys.sort()
-
return dd[keys[len(keys)/2]]
-
-
wordM = midWord(wordList)
-
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.
-
wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
-
a = [(len(i),i) for i in wordList]
-
b=sorted(a)
-
print "Max length is ", b[-1]
-
print "Shortest length is " , b[0]
-
i don't understand middle length though.
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
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: -
wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
-
a = [(len(i),i) for i in wordList]
-
b=sorted(a)
-
print "The longest three are: ", b[-3:]
-
print "The shortest three are " , b[:3]
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?
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: - >>> wordList = ['We', 'can', 'use', 'a', 'list', 'comprehension', 'to', 'create', 'the', 'length', 'list']
-
>>> a = [(len(i),i) for i in wordList]
-
>>> a.sort()
-
>>> m = len(wordList)/2
-
>>> print a[m-1:m+2]
-
[(3, 'the'), (3, 'use'), (4, 'list')]
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |