Looping over a dictionary to display key and values in order of a value

Wes

I am attempting to loop over a dictionary with a for loop to display keys and values in order of one of the value elements(the batting order number).

I can print the keys and values in the intended format, but I cannot figure out how to get the lines in the correct batting order.

I need to complete this task with logic in a for loop and without the use of lambda or a function. Here is what I have so far:

print ('Rays starters' + "\n")
rays_starters = {
    'DeJesus' : ['DH', 6, 299],
    'Loney' : ['1B', 4, 222],
    'Rivera' : ['C', 9, 194],
    'Forsythe' : ['2B', 5, 304],
    'Souza Jr' : ['RF', 2, 229],
    'Longoria' : ['3B', 3, 282],
    'Cabrera' : ['SS', 7, 214],
    'Kiermaier' : ['CF', 1, 240],
    'Guyer' : ['LF', 8, 274] }
for player in rays_starters:
    print (player + str(rays_starters[player]))
print ('\n' + 'Today\'s lineup' + '\n')
for player in rays_starters:
    batting_order = rays_starters.get(player)
    print('Batting ' + str(batting_order[1]) + ' : ' + str(batting_order[0]) + ' ' + player + ' ,current avg: ' + str(batting_order[2]))   

The output should look like this:

Rays starters

DeJesus ['DH', 6, 299]
Loney ['1B', 4, 222]
Rivera ['C', 9, 194]
Forsythe ['2B', 5, 304]
Souza Jr ['RF', 2, 229]
Longoria ['3B', 3, 282]
Cabrera ['SS', 7, 214]
Kiermaier ['CF', 1, 240]
Guyer ['LF', 8, 274]

Today's lineup

Batting 1 : CF Kiermaier ,current avg: 240
Batting 2 : RF Souza Jr ,current avg: 229
Batting 3 : 3B Longoria ,current avg: 282
Batting 4 : 1B Loney ,current avg: 222
Batting 5 : 2B Forsythe ,current avg: 304
Batting 6 : DH DeJesus ,current avg: 299
Batting 7 : SS Cabrera ,current avg: 214
Batting 8 : LF Guyer ,current avg: 274
Batting 9 : C Rivera ,current avg: 194

My output does in fact look exactly like this with the exception of the batting order being out of order. Please help me get on the right track and remember I am trying to learn here so any helpful criticism is welcome!

Shashank

Here's an efficient way since we know there are going to be 9 batters.

lineup = [None] * 9
for player, stats in rays_starters.items():
    lineup[stats[1]-1] = player, stats
print ('\nToday\'s lineup\n')
for player, batting_order in lineup:
    print('Batting ' + str(batting_order[1]) + ' : ' + str(batting_order[0]) + ' ' + player + ' ,current avg: ' + str(batting_order[2]))

All we are doing is initializing an array of 9 elements and using the batting order to map the player and stats as a tuple to the correct array index. Then we loop through the array of player and statistics tuples, and print the desired formatted output. This is O(n).

This concept is basically derived from Radix sort, or, more specifically, a very simple case of Counting sort where all the frequences are 1 and the "key function" is just subtracting 1 from the batting order to get an array index.

As @PadraicCunningham notes in the comments, this can theoretically be used for any number of batters by using the len function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Looping over key value object with underscore

From Dev

Looping over dictionary

From Dev

Looping over dictionary

From Dev

Looping over dictionary python

From Dev

Nested looping over tuple values in a dictionary with tuple keys python

From Dev

Looping over dictionary while using different values to insert and remove keys

From Dev

iterate over values in a list in a dictionary for a key angularjs

From Dev

Looping over unique values

From Dev

How to display dictionary key/value in loop?

From Dev

How to display dictionary key/value in loop?

From Dev

python sorting a dictionary based on values, and order based on key if values are repeating

From Dev

PHP display key => value array in order

From Java

Initializing a dictionary in python with a key value and no corresponding values

From Java

Remapping dataframe values with dictionary key/value

From Dev

Altering values in a key, value pair in dictionary python

From Dev

Inserting value in List of values of a Key in Dictionary

From Dev

Key with multiple values - check if value is in dictionary python

From Dev

Update value in Dictionary with multiple values per key

From Dev

How to iterate over a dictionary - n key-value pairs at a time

From Dev

incrementing key,value pair inside for loop iterating over dictionary

From Dev

Unpack index, key, and value when enumerating over dictionary items

From Dev

Using .iteritems() to iterate over key, value in Python dictionary

From Dev

incrementing key,value pair inside for loop iterating over dictionary

From Dev

Sorting a Dictionary with multiple Key:values pairs in the original key:value pair

From Dev

Looping over pairs of values in bash

From Dev

Looping over ILookup, accessing values

From Dev

Sorting dictionary in python, value in descending order, if conflict then by key

From Dev

Python - match dictionary value within string & print key in string order

From Dev

Cast While Looping Over Dictionary in Swift

Related Related

  1. 1

    Looping over key value object with underscore

  2. 2

    Looping over dictionary

  3. 3

    Looping over dictionary

  4. 4

    Looping over dictionary python

  5. 5

    Nested looping over tuple values in a dictionary with tuple keys python

  6. 6

    Looping over dictionary while using different values to insert and remove keys

  7. 7

    iterate over values in a list in a dictionary for a key angularjs

  8. 8

    Looping over unique values

  9. 9

    How to display dictionary key/value in loop?

  10. 10

    How to display dictionary key/value in loop?

  11. 11

    python sorting a dictionary based on values, and order based on key if values are repeating

  12. 12

    PHP display key => value array in order

  13. 13

    Initializing a dictionary in python with a key value and no corresponding values

  14. 14

    Remapping dataframe values with dictionary key/value

  15. 15

    Altering values in a key, value pair in dictionary python

  16. 16

    Inserting value in List of values of a Key in Dictionary

  17. 17

    Key with multiple values - check if value is in dictionary python

  18. 18

    Update value in Dictionary with multiple values per key

  19. 19

    How to iterate over a dictionary - n key-value pairs at a time

  20. 20

    incrementing key,value pair inside for loop iterating over dictionary

  21. 21

    Unpack index, key, and value when enumerating over dictionary items

  22. 22

    Using .iteritems() to iterate over key, value in Python dictionary

  23. 23

    incrementing key,value pair inside for loop iterating over dictionary

  24. 24

    Sorting a Dictionary with multiple Key:values pairs in the original key:value pair

  25. 25

    Looping over pairs of values in bash

  26. 26

    Looping over ILookup, accessing values

  27. 27

    Sorting dictionary in python, value in descending order, if conflict then by key

  28. 28

    Python - match dictionary value within string & print key in string order

  29. 29

    Cast While Looping Over Dictionary in Swift

HotTag

Archive