O'Reilly Forums: Printing Contents Of Hash - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Printing Contents Of Hash

#1 User is offline   M.Hoes 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 21-December 12

Posted 30 December 2012 - 02:45 AM

Hi. I have trouble understanding the program in chapter 5, that reads the scores and names of surfers, stores the contents in an array, and then prints the array:

scores = {}
result_f = open("results.txt")
for line in result_f:
(name, score) = line.split()
scores[score] = name
result_f.close()
print("The highest scores were: ")
for each_score in scores.keys():
print('Surfer ' + scores[each_score] + ' scored ' + each_score)

What I basically dont understand, is how the for loop at the end of the program works, and how 'scores[each_score]' and 'each_score' get filled with the names and scores of the surfers ?

PS:
I do understand the 'for score, surfer in scores.items():' example, I guess I have troubles with scores.keys().

This post has been edited by M.Hoes: 30 December 2012 - 03:01 AM

0

#2 User is offline   hutuxian 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 11-August 12

Posted 01 January 2013 - 08:01 AM

In face,"scores.keys()" returns a list which contain all the keys in the hash 'scores'.Therefore,'each_score' will loop through the list's keys,and 'scores[each_score]' is the names of the surfers.
0

#3 User is offline   M.Hoes 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 21-December 12

Posted 01 January 2013 - 08:48 AM

View Posthutuxian, on 01 January 2013 - 08:01 AM, said:

In face,"scores.keys()" returns a list which contain all the keys in the hash 'scores'.Therefore,'each_score' will loop through the list's keys,and 'scores[each_score]' is the names of the surfers.

Thanks, but I still dont fully understand. When I look at the code it appears to me that 'each_score' contains *both* the name *and* the score of the surfer, which obviously isnt possible or happening, like this :

Quote

for 1 2 3 4 5 6 in scores.keys():
print('Surfer ' + scores[1] + ' scored ' + 1)
print('Surfer ' + scores[2] + ' scored ' + 2)
print('Surfer ' + scores[3] + ' scored ' + 3)
print('Surfer ' + scores[4] + ' scored ' + 4)
print('Surfer ' + scores[5] + ' scored ' + 5)
print('Surfer ' + scores[6] + ' scored ' + 6)


PS: "results.txt" looks like this :

Johnny 8.65
Juan 9.12
Joseph 8.45
Stacey 7.81
Aideen 8.05
Zack 7.21
Aaron 8.31

This post has been edited by M.Hoes: 01 January 2013 - 09:08 AM

0

#4 User is offline   #TM# 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 10-December 12

Posted 02 January 2013 - 04:37 AM

What we basically understand by hash is that

its a variable or more technically a data structure which contains only two columns and can have many more rows of data.

see the below pic, i hope you will be able to understand.

Posted Image

So,

1. when you call only scores.keys() and assign it to each_score; the values (8.65, 9.12, ....) stored in keys are assigned to each_score, and as they are linked/pointed to names of surfers they will be printed by scores[each_score].
Note: Here the values(8.65, 9.12, ....) are itself keys, i.e., they are unique for each surfer(here it was assumed, but it may fail in cases where the scores of two surfers are equal).

2. when you call only scores.values() and assign it to each_score; only surfers names will be displayed and it will not be possible to access the scores of each surfers.

3. when you call scores.items() and assign it to each_score; it returns lists of pair of keys and values,i.e., surfer names and his/her score will be returned in pair to the variable each_score.
note: try displaying the variable each_score by using "print (each_score)" in the last for loop.


u can read more about the hash/dictionary data structure, different kind of operations on it and its properties in the below link.
link to hash

:) B)

This post has been edited by #TM#: 02 January 2013 - 04:52 AM

0

#5 User is offline   M.Hoes 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 21-December 12

Posted 02 January 2013 - 05:02 AM

Thank you very much for the picture. Why isnt this in the book ?
Anyway, I hope I understand now. The 'index' is not a (random) consecutive value: its the first value of the hash, the score in this case.

So 'scores[each_score] ' contains the index, which is the score in this case, and ' scored ' + each_score' prints the first value in the hash which is the name?

My mistake would then be that :
the 'index' is a random consecutive number (1,2,3,4,5), the first value being the score, and the second value the name of the surfer. ?

So it's not : 'index,score,name'
but: 'index, name' (with index being the 1st value, the score)

This post has been edited by M.Hoes: 02 January 2013 - 05:10 AM

0

#6 User is offline   #TM# 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 10-December 12

Posted 02 January 2013 - 05:08 AM

that's right !!

I think u understood it well.

and the pic is drawn by me myself, hence it cannot be in the book !! :).

try to grasp not only the syntax but also the concept in anything u learn.
then practise it to hold the grasp.
:)

This post has been edited by #TM#: 02 January 2013 - 05:09 AM

0

#7 User is offline   M.Hoes 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 21-December 12

Posted 02 January 2013 - 05:14 AM

View Post#TM#, on 02 January 2013 - 05:08 AM, said:

that's right !!

I think u understood it well.

Thank you

View Post#TM#, on 02 January 2013 - 05:08 AM, said:

and the pic is drawn by me myself, hence it cannot be in the book !! :).

It should be in the book. You should be an author of the book. The pic made me understand something that wasnt clear to me by reading the text.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users