jeudi 14 décembre 2017

Why does the implementation of set() in Python involve randomization?

I already known when I convert list A to set, then to list again (called B), the order will be changed. But I'm quite confused why the order of the list B also changes every time the code is run.

This is my code:

if __name__ == '__main__':
    my_list = ['1', '2', '3', '4', '5']
    print(my_list)
    print(list(set(my_list)))

And this is the result: 1st run:

['1', '2', '3', '4', '5']
['2', '3', '5', '1', '4']

2nd run:

['1', '2', '3', '4', '5']
['2', '4', '3', '5', '1']




Aucun commentaire:

Enregistrer un commentaire