[Tutor] multidemisional arrays

Jacob S. keridee at jayco.net
Sat Nov 20 04:09:28 CET 2004


Okay.
--------------------------------------------------------------------
def rfill(stri,length):                                # Step 1
   if len(stri) < length:                            # 2
       stri = stri+' '*(length-len(stri))        # 3
   return stri                                        # 4

howMany = input('How many artists will you enter? ')    # 5
artists = {}
# 6
for i in range(howMany):                                               # 7
  artist = raw_input('Who is the artist? ')                           # 8
  painting = raw_input('What is the painting called? ')           # 9
  value = raw_input('What is the value of the painting? ')    # 10
  artists[artist] = (painting,value)
# 11
artists.keys().sort()
# 12
l = 15
# 13
a = rfill("Artist",l)
# 14
p = rfill("Painting",l)
# 15
v = rfill("Value",l)
# 16
print "".join([a,p,v])
# 17
for x in artists.keys():
# 18
   a = rfill(x,l)
# 19
   p = rfill(artists[x][0],l)
# 20
   v = rfill(artists[x][1],l)
# 21
   print "".join([a,p,v])
# 22
----------------------------------------------

Steps:
1) We define a function named rfill. It needs the required variables stri
and length
2) This says that if the stri (string) is less than the length specified,
don't do anything but return the string
3) This tells appends spaces to the end of the string, stri. The '
'*(length-len(stri)) means to multiply the string " " (space) times (The
specified length minus the length of the string that we are editing). Okay,
if that doesn't make sense, all we are doing is right padding the given
string with spaces until we have a string of the given length.
4) This returns the string whether we modified it or not.
5) Asks the user how many artists there are
6) Makes the variable "artists" refer to an empty dictionary so we can
append to it.
7) for i in range(howMany): -- Alright...  range([start,]end[,step]) (The
brackets mean it is optional)

for i in range(start,end,step): # The start is defaulted to zero, the step
defaulted to one
    print "Hello"

is equivalent to

i = start
while i < end:
    print "Hello"
    i = i + step

8 - 10) Ask the user for each different thing involved.
11) Now we come to the definition of key. In dictionaries, you have key :
value pairs.
Such as a telephone directory.
a = {"Library":"726-7890","Jacob":"726-2998"}
where "Library" and "Jacob" are 'keys' of the dictionary. "726-7890" and
"726-2998" are there corresponding values, respectively. Now.    L.keys() is
a list of all the keys in the dictionary L
L.values() is a list of all the values in the dictionary L    L.items() is a
list of all the items in dictionary L

above ex.

a = {"Library":"726-7890","Jacob":"726-2998"}
print a.keys()
print a.values()
print a.items()

yields

["Library","Jacob"]
["726-7890","726-2998"]
["Library","726-7890","Jacob","726-2998"]

Okay, so now we know a little about dictionaries -- Oh! I almost forgot.

print a["Library"]

yields

"726-7890"

What I did in step 11 was to make a new key that is artist (What the user
entered for artist) and have the corresponding value equal to a tuple of
(painting, value) so that when I enter

>>> a = {"Jacob":("Flowers",10),"Gary":("People",15)}
>>> print a["Jacob"]
("Flowers":10)
>>> print a["Gary"]
("People",15)
>>> print a["Jacob"][0]
Flowers
>>> print a["Jacob"][1]
10
>>>

12) artists.keys().sort() means sort the dictionary by the keys in place.
So, we take all of the information and sort alphabetically by artist.

13) This predefines the length as 15. It's just a constant that I put in
there. If your artist names or paintings are longer that 15 characters, you
might want to set that higher.

14 - 17) Okay, Our titles are automatically right padded with spaces to the
preset length by our predefined function. Then with step 17, we print an odd
thing. We take a list of our titles and we join them together with the
seperator "" (an empty string). You can read more of this in the
documentation under String Methods in the index.

18) This runs through every thing in the list of keys to the dictionary of
artist : (painting,value) pairs.
IOW, we take all of the artists in the dictionary, and in

19) we take each artist and right pad it with spaces and give it its own
variable name for this loop through

20) we do the same with the corresponding painting

21) we do the same with the corresponding value

22) we join the three corresponding strings defined in steps 19-21 with an
empty string as a seperator as before, and print the result. Each successive
loop prints each corresponding values on its own line. If you count each
word and the spaces to the right of it, you would get the value of length,
since we did the right padding with the function rfill.

Okay! Hope this is not too long winded an explanation.
Feel free to ask more questions!
Jacob Schmidt

----- Original Message ----- 
From: "Diana Furr" <dleigh0 at carolina.rr.com>
To: "Jacob S." <keridee at jayco.net>
Sent: Friday, November 19, 2004 12:09 AM
Subject: Re: [Tutor] multidemisional arrays


> Thank you for your response. As you can tell I'm new at this. I enjoy
> learning but find it
> difficult because even reading the tutorials seems like reading a foreign
> language. I can
> occasionally pick up something I understand. If you wouldn't mind could
you
> please explain
> each thing you did to the program and maybe why it works, so that I will
> have an understanding
> of it.
> Thank you,
>  Diana
> P.S. One thing I see alot in the tutorials is 'key' what is the meaning of
> key, what does it do.
>
>
> ----- Original Message ----- 
> From: "Jacob S." <keridee at jayco.net>
> To: "Lloyd Kvam" <pythonTutor at venix.com>; "Diana Furr"
> <dleigh0 at carolina.rr.com>
> Cc: "Tutor Python" <tutor at python.org>
> Sent: Thursday, November 18, 2004 9:13 PM
> Subject: Re: [Tutor] multidemisional arrays
>
>
> > I'm surprised that nobody has corrected the excessive code involved in
the
> > first loop.
> >
> >>i=0
> >>howMany=input('How many artists will you enter? ')
> >>paintings=[]#painting
> >>artists=[]#artist
> >>values=[]#value
> >>for i in range(0,howMany,1):
> >>    painting=raw_input('Painting Name ')
> >>    artist=raw_input('Artist Name ')
> >>    value=input('Painting Value ')
> >>    paintings+=[painting]
> >>    artists+=[artist]
> >>    values+=[value]
> >>    artists=zip(artists,paintings,values)
> >>    artists.sort()
> >>    i=i+1
> >
> > You do not need to define i as 0, you do not need to increment i at the
> > end
> > of loop, that is what
> > for i in range(0,howMany,1): means.
> > Also, you can shorten that to: for i in range(howMany):
> >
> >>n=0
> >>for n in range(0,howMany,1):
> >>    print artists[n]
> >
> > If you want to figure this out as a learning experience, DO NOT READ ANY
> > FURTHER!!!
> >
> > I would do this.
> >
> > def rfill(stri,length):
> >    if len(stri) < length:
> >        stri = stri+' '*(length-len(stri))
> >    return stri
> >
> > howMany = input('How many artists will you enter? ')
> > artists = {}
> > for i in range(howMany):
> >    artist = raw_input('Who is the artist? ')
> >    painting = raw_input('What is the painting called? ')
> >    value = raw_input('What is the value of the painting? ')
> >    artists[artist] = (painting,value)
> > artists.keys().sort()
> > l = 15
> > a = rfill("Artist",l)
> > p = rfill("Painting",l)
> > v = rfill("Value",l)
> > print "".join([a,p,v])
> > for x in artists.keys():
> >    a = rfill(x,l)
> >    p = rfill(artists[x][0],l)
> >    v = rfill(artists[x][1],l)
> >    print "".join([a,p,v])
> >
> >
>
>
>



More information about the Tutor mailing list