-1

I have the following list of 12 items of paired ["key", values]. For instance the first item is: ["SN00003025", 0.1]

["SN00003025", 0.1], ["SN00013002", 30000.0], ["SN00037509", 23.7],
["SN00126162", 13560.0], ["SN00155812", 7.8], ["SN00232427", 3000.0], 
["SN00316328", 12.0], ["SN00319987", 5456.0], ["SN00339436", 5600.0],
["SN00399476", 12500.0], ["SN00399477", 32.0], ["SN00399478", 1100.0]

How can I change the order of the items of this list to random?

buran
  • 13,682
  • 10
  • 36
  • 61
Julio Coll
  • 17
  • 1
  • 7

2 Answers2

1
import random

random.shuffle(my_list)
Jacob Philpott
  • 538
  • 1
  • 8
  • 24
  • Thank you Jacob, but I got: TypeError: 'tuple' object does not support item assignment – Julio Coll Feb 04 '21 at 15:08
  • Hey @JulioColl in that case your item must be in the format of a tuple.. is it wrapped in round brackets like this.. (1, 2, 3) or square brackets like this.. [1, 2, 3]? – Jacob Philpott Feb 04 '21 at 15:12
  • Round brackets = tuple. Square brackets = list. – Jacob Philpott Feb 04 '21 at 15:13
  • The whole list is not wrapped at all, only each of the pairs are wrapped by square brakets as shown above. – Julio Coll Feb 04 '21 at 15:22
  • @JulioColl okay, wrap the whole thing in square brackets and then pass it into the random.shuffle function and it should work. – Jacob Philpott Feb 04 '21 at 15:27
  • Something like: `[["SN00003025", 0.1], ["SN00013002", 30000.0], ["SN00037509", 23.7] ... ]` – Jacob Philpott Feb 04 '21 at 15:27
  • @JulioColl it's my pleasure! If you found my answer helpful you can accept it by clicking the check mark below the vote tracker, thanks! :) – Jacob Philpott Feb 09 '21 at 16:07
  • Yes. I will do it, except that I do not see any "vote tracker" !!! :-). Could you indicate to me where is it located? – Julio Coll Feb 10 '21 at 19:37
  • @JulioColl by vote tracker I mean the up and down arrows in the upper left corner of every question and answer. IF you are the questioner you should also see a check mark that you can click to accept an answer. – Jacob Philpott Feb 10 '21 at 19:42
  • 1
    Ok, I clicked up and check the check mark. Thanks for the info!!!!!!!!!! – Julio Coll Feb 10 '21 at 19:50
  • @JulioColl awesome, thanks! I gave you an upvote also! :) But I don't think you can vote until you have at least 15 reputation.. your almost there though! – Jacob Philpott Feb 10 '21 at 19:57
  • 1
    thanks! I am still trying to figure out how the stackholder questioning works!!!. SLOWLY BUT SURELY :-) – Julio Coll Feb 12 '21 at 09:27
  • @JulioColl take a look at the tour page, it will help explain how things work around here: https://stackoverflow.com/tour – Jacob Philpott Feb 24 '21 at 11:02
0

You're probably looking for something like numpy.random.permutation: https://numpy.org/doc/stable/reference/random/generated/numpy.random.permutation.html

import numpy as np
lst = # ... Your list
permuted = np.random.permutation(lst)
MrMikimn
  • 721
  • 1
  • 5
  • 19
  • good try for something new.however, the list did not change either!...???. – Julio Coll Feb 04 '21 at 15:27
  • if I square brack the whole ist, and execute some of your code the printing add round barckets and says tuple object does not support..... – Julio Coll Feb 04 '21 at 15:30
  • THANK YOU JACOB !. I got it to work by squaring bracketing the whole list and using import numpy as np, np.random.shuffle(mylist).......... – Julio Coll Feb 04 '21 at 15:44