mardi 30 octobre 2018

Python - Appending array element to dictionary value

# Create an array of integers from 1–16.
random_rankings = list(range(1, 17))
# Shuffle the array.
random.shuffle(random_rankings)
for ranking in random_rankings:
    all_players = {k: ranking if not v else v for k, v in all_players.items()}
print(all_players)

I am trying to change the empty values ('') in the dictionary to random numbers (without duplicates). So I made an array 1-16 and shuffled it. When trying to append a single integer from that to the individual values, the same number is being appended, e.g.

{'Rick Sanchez': 16, 'Morty Smith': 16, 'Beth Smith': 16, 'Jerry Smith': 16, 'Summer Smith': 16, 'Scary Terry': 16, 'Xenon Bloom': 16, 'Abradolf Lincler': 16, 'Krombopulos Michael': 16, 'Blim Blam ': 16, 'Alan Rails': 16, 'Tophat Jones': 16, 'Doofus Rick': 16, 'Million Ants': 16, 'Scroopy Noopers': 16, 'Pichael Thompson': 16}

I wanted each name to have a different number from the array. What am I doing wrong?

EDIT: I also tried...

if any(all_players.values()) == False:
        print("No rankings in csv file\nAssigning random rankings")
        # Create an array of integers from 1–16.
        random_rankings = list(range(1, 17))
        # Shuffle the array.
        random.shuffle(random_rankings)
        for value in all_players.values():
            for ranking in random_rankings:
                value = ranking
    print(all_players)

But now I get an error




Aucun commentaire:

Enregistrer un commentaire