I have a list of lists that contains different characters and values that are assigned to them. So my list is something like:
player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Jessica Jones', 12, 0, 6, 6, 10, 6], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]]
I want to be able to make a loop that goes through each player and prints their name and all of their stats. I have tried to make many loops but they either print the stats from different players or it doesn't print the whole list. The latest one that I have tried prints the first name and then the first 4 numbers and then stops.
index = 0
index1 = 0
while index < len(player_list):
print(player_list[index1][index])
index = index 1
index1 = 1
The output to this code prints:
Bruce Wayne
5
5
0
0
And that's it. I need it to go though the whole thing and print them out like this:
Bruce Wayne 5 5 0 0 100 15
Jessica Jones 12 0 6 6 10 6
Johnny Rose 6 2 0 4 20 10
Gina Linetti 7 4 0 3 300 15
Buster Bluth 3 0 2 1 50 1
Edit: I need to be able to format the characters and their scores to this template:
============================================================
- Player Summary -
============================================================
- P W L D Chips Score -
CodePudding user response:
Iterate over the player_list and unpack each element thus:
player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15], ['Jessica Jones', 12, 0, 6, 6, 10, 6], ['Johnny Rose', 6, 2, 0, 4, 20, 10], ['Gina Linetti', 7, 4, 0, 3, 300, 15], ['Buster Bluth', 3, 0, 2, 1, 50, 1]]
for player in player_list:
print(*player)
Output:
Bruce Wayne 5 5 0 0 100 15
Jessica Jones 12 0 6 6 10 6
Johnny Rose 6 2 0 4 20 10
Gina Linetti 7 4 0 3 300 15
Buster Bluth 3 0 2 1 50 1
CodePudding user response:
If you need to align the columns, then use f-strings with a width specifier:
for name, *stats in player_list:
print(f"{name:15}", *(f"{stat:4}" for stat in stats))
Output:
Bruce Wayne 5 5 0 0 100 15
Jessica Jones 12 0 6 6 10 6
Johnny Rose 6 2 0 4 20 10
Gina Linetti 7 4 0 3 300 15
Buster Bluth 3 0 2 1 50 1
After edit, using template
For getting the specific layout you added to your question, do this:
print("""============================================================
- Player Summary -
============================================================
- P W L D Chips Score -""")
for name, p, w, l, d, chips, score in player_list:
print(f"- {name:27} {p:2} {w:2} {l:2} {d:2} {chips:7} {score:7} -")
print("============================================================")
Output:
============================================================
- Player Summary -
============================================================
- P W L D Chips Score -
- Bruce Wayne 5 5 0 0 100 15 -
- Jessica Jones 12 0 6 6 10 6 -
- Johnny Rose 6 2 0 4 20 10 -
- Gina Linetti 7 4 0 3 300 15 -
- Buster Bluth 3 0 2 1 50 1 -
============================================================
CodePudding user response:
You can transform your list to a dictionary then iterate through it and print each pair of key, values as follows :
res = dict()
res = {tuple(sub[:1]): tuple(sub[1:]) for sub in player_list}
all_players = player_list
for k,v in res.items():
print(k[0], v)
>>>
Bruce Wayne (5, 5, 0, 0, 100, 15)
Jessica Jones (12, 0, 6, 6, 10, 6)
Johnny Rose (6, 2, 0, 4, 20, 10)
Gina Linetti (7, 4, 0, 3, 300, 15)
Buster Bluth (3, 0, 2, 1, 50, 1)