Random shuffle multiple lists python - python

I have a set of lists in Python and I want to shuffle both of them but switching elements in same positions in both lists like
a=[11 22 33 44] b = [66 77 88 99]
*do some shuffeling like [1 3 0 2]*
a=[22 44 11 33] b = [77 99 66 88]
Is this possible?

Here's a solution that uses list comprehensions:
>>> a = [11, 22, 33, 44]
>>> b = [66, 77, 88, 99]
>>> p = [1, 3, 0, 2]
>>>
>>> [a[i] for i in p]
[22, 44, 11, 33]
>>>
>>> [b[i] for i in p]
[77, 99, 66, 88]
>>>

You can use zip in concert with the random.shuffle operator:
a = [1,2,3,4] # list 1
b = ['a','b','c','d'] # list 2
c = zip(a,b) # zip them together
random.shuffle(c) # shuffle in place
c = zip(*c) # 'unzip' them
a = c[0]
b = c[1]
print a # (3, 4, 2, 1)
print b # ('c', 'd', 'b', 'a')
If you want to retain a,b as lists, then just use a=list(c[0]). If you don't want them to overwrite the original a/b then rename like a1=c[0].

Expanding upon Tom's answer, you can make the p list easily and randomize it like this:
import random
p = [x for x in range(len(a))]
random.shuffle(p)
This works for any size lists, but I'm assuming from your example that they're all equal in size.
Tom's answer:
Here's a solution that uses list comprehensions:
a = [11, 22, 33, 44]
b = [66, 77, 88, 99]
p = [1, 3, 0, 2]
[a[i] for i in p]
[22, 44, 11, 33]
[b[i] for i in p]
[77, 99, 66, 88]

a=[11,22,33,44]
order = [1,0,3,2] #give the order
new_a = [a[k] for k in order] #list comprehension that's it
You just give the order and then do list comprehension to get new list

Related

Having trouble replacing elements in a 2d list with a 1d list using for-loops

I am asked to replace some elements in a 2d list with elements from a 1D list. The rule is to replace the first row of list1 with list2. The elements in the next row will be replaced by 3 times each element from the previous row. So the second row will contain 33, 39, 45,and 51, and the third row 99, 117, 135, and 153... all the way to the 10th row.
Here is my code:
list1 = [[0]*4]*10
list2 = [11,13,15,17]
list1[0] = list2
i = 1
for i in range(9):
j = 0
for j in range(4):
list1[i][j] = list2[j]*(3**i)
j+=1
i+=1
The result I got from this code basically only contains the correct first row, but the rest of the rows after that are all 72171, 85293, 98415, and 111537 (which is 3 to the 8th). I am not sure which part is giving me the error.
Here are some comments on your code and examples of how to make it do what the question describes:
(1.) References vs copies: list1 = [[0]*4]*10 creates a single 4-element list and populates list1 with 10 references to it (NOT 10 copies of it).
For an example of what this implies, watch this:
list1 = [[0]*4]*10
list1[1][0] = 33
list1[1][1] = 39
list1[1][2] = 45
list1[1][3] = 51
print(list1)
... gives this:
[[33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51], [33, 39, 45, 51]]
In other words, updating one list element within list1 updates them all, since each element of list1 is just a reference to the same list.
If this is not what you want, you can use list1 = [[0]*4 for _ in range(10)] instead to give you a distinct list (10 in total) for each index in list1:
list1 = [[0]*4 for _ in range(10)]
list1[1][0] = 33
list1[1][1] = 39
list1[1][2] = 45
list1[1][3] = 51
print(list1)
... gives:
[[0, 0, 0, 0], [33, 39, 45, 51], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Your code as written would tend to imply that the second approach above is what is needed.
(2.) Code fix assuming nested lists cannot be replaced: It's unclear from the question whether whether you are allowed to replace each list in list1 to get the desired values, or if you are expected to leave the nested lists in place and simply modify their numerical contents.
If list replacement is not allowed, then your code can be rewritten like this:
list1 = [[0]*4 for _ in range(10)]
list2 = [11,13,15,17]
list1[0][:] = list2
for i in range(9):
for j in range(4):
list1[i + 1][j] = list2[j]*(3**(i + 1))
print(list1)
... giving:
[[11, 13, 15, 17], [33, 39, 45, 51], [99, 117, 135, 153], [297, 351, 405, 459], [891, 1053, 1215, 1377], [2673, 3159, 3645, 4131], [8019, 9477, 10935, 12393], [24057, 28431, 32805, 37179], [72171, 85293, 98415, 111537], [216513, 255879, 295245, 334611]]
Note that these changes were made to your code:
Changed the initialization of list1 to allocate 10 distinct nested lists.
Eliminated i = 1 (not needed because the for loop takes care of initializing i), j = 0 (similar reason), j+=1 (not needed because the for loop takes care of updating j) and i+=1 (similar reason).
Changed list1[i][j] to list1[i + 1][j] to fix the indexing.
Changed 3**i to 3**(i + 1) to calculate the correct multiplier.
(3.) Code fix assuming nested lists can be replaced: In this case, your looping logic can be simplified and you don't need to use nested lists when initializing list1.
Here is a long-hand way to do what you ask which will overwrite the contents of list1 with new nested lists that have the desired values:
list1 = [None] * 10
list2 = [11,13,15,17]
list1[0] = list2
mul = 3
for i in range(1, len(list1)):
temp = [0] * len(list2)
for j in range(len(list2)):
temp[j] = mul * list2[j]
list1[i] = temp
mul *= 3
print(list1)
Here is a way that uses a list comprehension inside a loop:
list1 = [None] * 10
list2 = [11,13,15,17]
list1[0] = list2
mul = 3
for i in range(1, len(list1)):
list1[i] = [mul * v for v in list2]
mul *= 3
print(list1)
And finally, here is a very compact nested list comprehension approach:
list1 = [None] * 10
list2 = [11,13,15,17]
list1 = [[(3 ** i) * v for v in list2] for i in range(len(list1))]
the reason that happened is that you are working on a copy of 0 elements of list1 , so whenever you modify further everything will be replaced with the new value, it's not a copy its the same object in reference.
for i in range(9):
tmp = []
for j in range(4):
tmp.append(list2[j]*(3**i))
list1[i] = tmp
this is all you need
You can make this much easier using numpy arrays:
import numpy as np
list1 = np.zeros((10,4), dtype=int)
list1[0] = [11,13,15,17]
for i in range(1,10):
list1[i] = 3*list1[i-1]

Indices of the N smallest elements of a list

i am trying to find 3 lowest number from a list and using those indexes to find corresponding value from another list.
this is an example of what i have tried
a = [12, 83, 22, 30, 57, 32, 88, 46, 20, 26, 78, 65, 45, 56, 74]
b = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
lowest = lowest2 = lowest3 = max(a)
indexes = []
for i in range(0,len(a)):
if a[i] < lowest :
lowest = a[i]
print(b[i])
elif a[i] < lowest2 and a[i] > lowest:
lowest2 = a[i]
print(b[i])
elif a[i] < lowest3 and a[i] > lowest2:
lowest3 = a[i]
print(b[i])
print(lowest,lowest2,lowest3)
i can only supposed to use anaconda library and no this is not assignment this is a very small part of a program i have been trying to do.
output: a
b
c
d
i
j
12 20 26
What you can do is sort the list a and return it's index values,
>>> a = [12, 83, 22, 30, 57, 32, 88, 46, 20, 26, 78, 65, 45, 56, 74]
>>> n_min_values = 3
>>> sorted(range(len(a)), key=lambda k: a[k])[:n_min_values]
[0, 8, 2] # index values
and iterate through this index values list to get corresponding values from list b,
>>> b = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
>>> for i in sorted(range(len(a)), key=lambda k: a[k])[:n_min_values ]:
print(b[i])
a
i
c
OR
Using list comprehesion,
>>> [b[i] for i in sorted(range(len(a)), key=lambda k: a[k])[:n_min_values ]]
['a', 'i', 'c'] # your output
Heaps are good at doing this sort of thing, especially if the order of the smallest items isn't important.
Here's a small example using heapq.nsmallest on a with its zipped indices.
from heapq import nsmallest
from operator import itemgetter
n = 3
idx, _ = zip(*nsmallest(n, enumerate(a), key=itemgetter(1)))
idx
# (0, 8, 2)
[b[i] for i in idx] # your result
# ['a', 'i', 'c']

can i join three list in python (string list and two number list)

is there a way that i can concatenate a string list and 2 number list such that the concatenated list would take them one after the order while joining them.
Input:
a = [john, bob, ted, Paul]
b = [22, 34, 56, 12]
c = [13, 98, 78, 60]
Expected Ouput:
[john, 22, 13, bob, 34, 98, ted, 56, 78, Paul, 12, 60]
You can use the zip function with list comprehension:
a = ['john', 'bob', 'ted', 'Paul']
b = [22, 34, 56, 12]
c = [13, 98, 78, 60]
outcome = [i for t in zip(a, b, c) for i in t]
outcome would become:
['john', 22, 13, 'bob', 34, 98, 'ted', 56, 78, 'Paul', 12, 60]
Note that the c list in your question has an extra 90 that is not in your expected output, so I removed it believing it's a typo on your part.
Using formatted string and enumerate with list comprehension
l = ['{}, {}, {}'.format(a[idx], b[idx], c[idx]) for idx, item in enumerate(a)]
['john, 22, 13', 'bob, 34, 98', 'ted, 56, 78', 'Paul, 12, 60']
Your lists :
Make Sure all lists have same dimensions.
a = ['john', 'bob', 'ted', 'Paul']
b = [22, 34, 56, 12]
c = [13, 98, 78, 90]
Import numpy a great module for handling arrays
import numpy as np
Make numpy array and reshape them as you want
a = np.array(a).reshape(-1,1)
b = np.array(b).reshape(-1,1)
c = np.array(c).reshape(-1,1)
Concate them along the required axis. In your case it will be 1 (Column wise as we have reshaped the arrays)
f= np.concatenate((a,b,c),axis=1)
Flatten them in 1D array
f = f.flatten()
print(f)

how to split a list into multiple lists with certain ranges?

I have a list that needs to be split into 4 separate lists with maximum size of 21 depending on the amount of items in the original list.
The master list can have from 1 to 84 items.
I want the items to start in a and fill up to a maximum of 21 in a, b, c, d
I have the following code that can split the items up no problem but I want to know if there is a shorter way to do this. I am repeating code a lot except the range.
codes = [x for x in range(80)] # range anywhere between 1-84
print(len(codes))
a = []
b = []
c = []
d = []
for i in range(0, 21):
try:
a.append(codes[i])
except IndexError:
pass
for i in range(21, 42):
try:
b.append(codes[i])
except IndexError:
pass
for i in range(42, 63):
try:
c.append(codes[i])
except IndexError:
pass
for i in range(63, 84):
try:
d.append(codes[i])
except IndexError:
pass
print(len(a), len(b), len(c), len(d))
print(a)
print(b)
print(c)
print(d)
Before that I had this code that works great for the whole 84 items as the order is not important..
a = []
b = []
c = []
d = []
for a1, b1, c1, d1 in zip(*[iter(codes)]*4):
a.append(a1)
b.append(b1)
c.append(c1)
d.append(d1)
However if i have say 4 items, it will add 1 to each
a = [0]
b = [1]
c = [2]
d = [3]
What I would like to obtain is
a = [0, 1, 2, 3]
b = []
c = []
d = []
You can simply use sublist
a = codes[0: 21]
b = codes[21:42]
c = codes[42:63]
d = codes[63:84]
This will be fine for your requirement
#SajalPreetSinghs answer is correct for a simple use case like OP's but it also has some disadvantages when it comes to scalability.
For example:
When you need 20 sublists instead of the actual 4. You would have to add 16 more lines and specify 2 different numbers per line each!
Now imagine you already extended the code to 20 sublists but now you want the maximum item count per sublist to be 37 instead of 21 - you would have to change 2*20 = 40 numbers!
Improved scalability with generators
So if you want something with a better scalability you could use the following code which makes usage of generators:
Code
def sublist(orig_list, list_of_subs, max_items_per_list):
def sublist_generator():
for sublist in list_of_subs:
yield sublist
sublist = sublist_generator()
current_sublist = next(sublist)
for element in orig_list:
current_sublist.append(element)
if len(current_sublist) == max_items_per_list: # current list is full
current_sublist = next(sublist) # so let current point to the next list
Setup and usage
import random
start = 1
stop = random.randint(2, 85) # generate random int inclusively 2 and 85
codes = [x for x in range(start, stop)] # stop is exclusive: range(1, 85) == [1, 2, ..., 84]
a, b, c, d = [], [], [], []
sublists = [a, b, c, d] # *1
sublist(codes, sublists, 21)
for sublist in sublists:
print(sublist)
Better scalability because
If you want to change the number of items per sublist you only have to pass in the new maximum number.
If you want to increase the number of sublists you only have to add more of them to the sublists variable which you pass to the function (see *1).
If you need this code more often it's no problem because you can comfortably call the function.
I hope this helps someone!
Cheers
winklerrr
You could use the zip_longest function from itertools
from itertools import zip_longest
master_list = range(0, 84)
iterables = [iter(master_list)] * 21
slices = zip_longest(*iterables, fillvalue=None)
for slice in slices:
print("slice", slice)
# slice (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
# slice (21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41)
# slice (42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62)
# slice (63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83)
You can do it with using a list comprehension:
MAX_SIZE = 21
l = list(range(80))
l1,l2,l3,l4 = [l[i*MAX_SIZE: (i+1)*MAX_SIZE] for i in range(4)]
#l1=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,20],
# ....
#l4=[63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
What I would do would be just put it in one loop.
if index is 0 to 83
for i in range(0,84):
if i>=0 and i<=20:
a.append(codes[i])
elif i>20 and i<=41:
b.append(codes[i])
elif i>41 and i<=62:
c.append(codes[i])
elif i>62 and i<=83:
d.append(codes[i])
A solution using a list comprehension:
i_list = range(0,84)
r = 21
res = [i_list[i:i+r] for i in range(0,len(i_list),r)]

finding top k elements in array python without heapq or sorting

I am trying to find the top k elements of a list in python without using heapq or sorting the list.
This is what I tried,
list = [20,4,67,22,445,1,34]
k = 3
newList=[]
for i in range(0,k):
newList.append(list[i])
for i in list:
mini = min(newList)
if i <= mini:
continue
else:
newList.remove(mini)
newList.append(i)
print newList
But i am getting 67,67,445. What am I doing wrong here?
The problem is apparent when you add some tracing:
>>> list = [20,4,67,22,445,1,34]
>>> k = 3
>>> newList=[]
>>>
>>> for i in range(0,k):
... newList.append(list[i])
...
>>> for i in list:
... mini = min(newList)
... if i <= mini:
... continue
... else:
... print newList
... print "Replacing {0} with {1}".format(mini, i)
... newList.remove(mini)
... newList.append(i)
... print newList
... print '-' * 20
...
[20, 4, 67]
Replacing 4 with 20
[20, 67, 20]
--------------------
[20, 67, 20]
Replacing 20 with 67
[67, 20, 67]
--------------------
[67, 20, 67]
Replacing 20 with 22
[67, 67, 22]
--------------------
[67, 67, 22]
Replacing 22 with 445
[67, 67, 445]
You already have 67 in the list when you iterate over it and add it a second time.
I would rewrite it as:
>>> numbers = [20,4,67,22,445,1,34]
>>> k = 3
>>> newList = numbers[:k]
>>>
>>> for i in numbers[k:]:
... mini = min(newList)
... if i > mini:
... print "Replacing {0} with {1}".format(mini, i)
... newList.remove(mini)
... newList.append(i)
...
Replacing 4 with 22
Replacing 20 with 445
Replacing 22 with 34
>>> print newList
[67, 445, 34]
I would not name your list list, though.
You can simply do it as:
a = [20,4,67,22,445,1,34]
k = 3
newList=[]
for i in range(k):
pos = a.index(max(a))
newList.append(a[pos])
a.pop(pos)
>>> print newList
[67, 445, 34]
you have 67 in your newlist at beginning and the 67 never pop out
The solution from hughdbrown has one bug that I noticed.
If the list has similar entries, then the result would just display one of those entries.
For example, if the list is [1, 2, 3, 4, 5, 5], the result would display [3, 4, 5] instead of [4, 5, 5]

Categories

Resources