Python Dictionary / Loop Output
- Make a dictionary for the nine Tampa Bay bays that are given. Use player names as keys and a list for each value.
- Each list of values should contain the position the player is playing, the batting order, and the current average. See above.
- When the dictionary is complete, use a for loop to display the dictionary keys and values. This is what I got for this
-
Next, use a loop to print "lineup" (dictionary in batting order). This is a step I need help with, but not sure how I go about how to order for such a dictionary. The list made more sense to me, but that's not the question.
main(): rays_players = { '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 key in rays_players: print(key, rays_players[key]) main()
This is what I tried, but it doesn't work, I am very new to this:
for key in sorted(rays_players.items(), key=lambda v: (v)):
print ("%s: %s" % (key))
Step 4 should look like this:
Batting 1: CF Kiermaier, current average: 240
Batting 2: RF Souza Jr, Current Average: 229
Batting 3: 3B Longoria, current average: 282
Batting 4: 1B Loney, current average: 222
Batting 5: 2B Forsythe, current average: 304
Batting 6: DH DeJesus, Current Average: 299
Batting 7: SS Cabrera Current Average: 214
Batting 8: LF Guyer, current avg: 274
Batting 9: C Rivera Current Average Speed: 194
+3
source to share
1 answer
Hope this helps:
rays_players = {'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 key, value in sorted(rays_players.items(), key=lambda v: v[1][1]):
print("Batting {}: {} {}, current avg: {}".format(value[1], value[0], key, value[2]))
+7
source to share