Nested looping a Python list - python

I have a list of length 6 with L[5] = 3 fixed. I need to loop it around 243 times( or something) to get the combination in a particular order.
The L[4] will keep going 1,2,3 and L[3] would be the same for that time and then it moves right to left. I know it will be some nested loop but can't do it properly. The list can be initialized as anything but L[5] needs to be 3 and the first combination will be L = [1,1,1,1,1,3] and the last combination will be [3,3,3,3,3,3]

IIUC you can use itertools.product:
from itertools import product
vals = [[1, 2, 3]] * 5 + [[3]]
for c in product(*vals):
print(c)
Prints:
(1, 1, 1, 1, 1, 3)
(1, 1, 1, 1, 2, 3)
(1, 1, 1, 1, 3, 3)
(1, 1, 1, 2, 1, 3)
(1, 1, 1, 2, 2, 3)
(1, 1, 1, 2, 3, 3)
(1, 1, 1, 3, 1, 3)
(1, 1, 1, 3, 2, 3)
(1, 1, 1, 3, 3, 3)
(1, 1, 2, 1, 1, 3)
...
(3, 3, 3, 3, 1, 3)
(3, 3, 3, 3, 2, 3)
(3, 3, 3, 3, 3, 3)

Something like this should work:
list = []
for i in range(1,4):
for j in range(1,4):
for k in range(1,4):
for l in range(1,4):
for m in range(3,4):
list.append([i,j,k,l,m])
for elem in list:
print(elem)
Output:
[1, 1, 1, 1, 3]
[1, 1, 1, 2, 3]
[1, 1, 1, 3, 3]
[1, 1, 2, 1, 3]
[1, 1, 2, 2, 3]
[1, 1, 2, 3, 3]
[1, 1, 3, 1, 3]
[1, 1, 3, 2, 3]
[1, 1, 3, 3, 3]
[1, 2, 1, 1, 3]
[1, 2, 1, 2, 3]
[1, 2, 1, 3, 3]
[1, 2, 2, 1, 3]
[1, 2, 2, 2, 3]
[1, 2, 2, 3, 3]
[1, 2, 3, 1, 3]
[1, 2, 3, 2, 3]
[1, 2, 3, 3, 3]
[1, 3, 1, 1, 3]
[1, 3, 1, 2, 3]
[1, 3, 1, 3, 3]
[1, 3, 2, 1, 3]
[1, 3, 2, 2, 3]
[1, 3, 2, 3, 3]
[1, 3, 3, 1, 3]
[1, 3, 3, 2, 3]
[1, 3, 3, 3, 3]
[2, 1, 1, 1, 3]
[2, 1, 1, 2, 3]
[2, 1, 1, 3, 3]
[2, 1, 2, 1, 3]
[2, 1, 2, 2, 3]
[2, 1, 2, 3, 3]
[2, 1, 3, 1, 3]
[2, 1, 3, 2, 3]
[2, 1, 3, 3, 3]
[2, 2, 1, 1, 3]
[2, 2, 1, 2, 3]
[2, 2, 1, 3, 3]
[2, 2, 2, 1, 3]
[2, 2, 2, 2, 3]
[2, 2, 2, 3, 3]
[2, 2, 3, 1, 3]
[2, 2, 3, 2, 3]
[2, 2, 3, 3, 3]
[2, 3, 1, 1, 3]
[2, 3, 1, 2, 3]
[2, 3, 1, 3, 3]
[2, 3, 2, 1, 3]
[2, 3, 2, 2, 3]
[2, 3, 2, 3, 3]
[2, 3, 3, 1, 3]
[2, 3, 3, 2, 3]
[2, 3, 3, 3, 3]
[3, 1, 1, 1, 3]
[3, 1, 1, 2, 3]
[3, 1, 1, 3, 3]
[3, 1, 2, 1, 3]
[3, 1, 2, 2, 3]
[3, 1, 2, 3, 3]
[3, 1, 3, 1, 3]
[3, 1, 3, 2, 3]
[3, 1, 3, 3, 3]
[3, 2, 1, 1, 3]
[3, 2, 1, 2, 3]
[3, 2, 1, 3, 3]
[3, 2, 2, 1, 3]
[3, 2, 2, 2, 3]
[3, 2, 2, 3, 3]
[3, 2, 3, 1, 3]
[3, 2, 3, 2, 3]
[3, 2, 3, 3, 3]
[3, 3, 1, 1, 3]
[3, 3, 1, 2, 3]
[3, 3, 1, 3, 3]
[3, 3, 2, 1, 3]
[3, 3, 2, 2, 3]
[3, 3, 2, 3, 3]
[3, 3, 3, 1, 3]
[3, 3, 3, 2, 3]
[3, 3, 3, 3, 3]

Related

Shift individual list element to the right

I've been having this problem (below).
What I want to achieve is to shift each individual element in the list to the right and create a list/dictionary of lists of the possible combinations.
This is what I would like to achieve:
{[1,2,3,4,5],
[2,1,3,4,5],
[2,3,1,4,5],
[2,3,4,1,5],
[2,3,4,5,1],
[1,3,2,4,5],
[1,3,4,2,5],
[1,3,4,5,2],
[1,2,4,3,5],
.
.
.
#until final possible combination
}
It doesn't have to be in the same order as the example above. I just want to be able to get a long list/dictionary of all possible order of combinations of [1,2,3,4,5]. Thanks in advance!
You can use permutations() from itertools:
from itertools import permutations
l = [1,2,3,4,5]
for i in permutations(l):
print(list(l))
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 5, 4]
[1, 2, 4, 3, 5]
[1, 2, 4, 5, 3]
[1, 2, 5, 3, 4]
[1, 2, 5, 4, 3]
[1, 3, 2, 4, 5]
[1, 3, 2, 5, 4]
[1, 3, 4, 2, 5]
[1, 3, 4, 5, 2]
[1, 3, 5, 2, 4]
[1, 3, 5, 4, 2]
[1, 4, 2, 3, 5]
[1, 4, 2, 5, 3]
[1, 4, 3, 2, 5]
[1, 4, 3, 5, 2]
[1, 4, 5, 2, 3]
[1, 4, 5, 3, 2]
[1, 5, 2, 3, 4]
[1, 5, 2, 4, 3]
[1, 5, 3, 2, 4]
[1, 5, 3, 4, 2]
[1, 5, 4, 2, 3]
[1, 5, 4, 3, 2]
[2, 1, 3, 4, 5]
[2, 1, 3, 5, 4]
[2, 1, 4, 3, 5]
[2, 1, 4, 5, 3]
[2, 1, 5, 3, 4]
[2, 1, 5, 4, 3]
[2, 3, 1, 4, 5]
[2, 3, 1, 5, 4]
[2, 3, 4, 1, 5]
[2, 3, 4, 5, 1]
[2, 3, 5, 1, 4]
[2, 3, 5, 4, 1]
[2, 4, 1, 3, 5]
[2, 4, 1, 5, 3]
[2, 4, 3, 1, 5]
[2, 4, 3, 5, 1]
[2, 4, 5, 1, 3]
[2, 4, 5, 3, 1]
[2, 5, 1, 3, 4]
[2, 5, 1, 4, 3]
[2, 5, 3, 1, 4]
[2, 5, 3, 4, 1]
[2, 5, 4, 1, 3]
[2, 5, 4, 3, 1]
[3, 1, 2, 4, 5]
[3, 1, 2, 5, 4]
[3, 1, 4, 2, 5]
[3, 1, 4, 5, 2]
[3, 1, 5, 2, 4]
[3, 1, 5, 4, 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1]
[5, 4, 3, 1, 2]
[5, 4, 3, 2, 1]

how to apply filter function to items in multiple list?

I wanna get values from multiple(?) list, which is not 0(I think filter can be one of solutions).
list as below:
>>ls = [[i for i in np.random.randint(0, 5, 5)] for _ in range(7)]
>>ls
>>
[[2, 3, 3, 0, 0],
[4, 2, 4, 3, 2],
[1, 2, 4, 2, 4],
[2, 3, 4, 3, 1],
[0, 1, 0, 3, 0],
[3, 4, 4, 4, 3],
[3, 4, 3, 3, 2]]
Expected result is:
[2,3,3,4,2,4,3,2,1,2,4,2,4,2,3,4,3,1,1,3,3,4,4,4,3,3,4,3,3,2]
I tried using filter function, wanted expand this idea, but I failed:
>> [elem for elem in filter(lambda x: x if x != 0 else False, ls[0])]
>>
[2, 3, 3]
I wanna find fastest way to get expected result, not using for loop.
Would you suggest any good idea?
Edit:
Oops, Sorry for confusing you.
I saying 'not usting for loop' means, I wanna use list comprehension instead of for loop, because I heard list comprehension fater than for loop.
Use a nested list comprehension:
[j for i in ls for j in i if j != 0]
ls = [[i for i in np.random.randint(0, 5, 5)] for _ in range(7)]
[[1, 0, 3, 0, 0],
[1, 2, 2, 3, 0],
[1, 1, 1, 4, 3],
[1, 0, 3, 0, 4],
[2, 0, 3, 0, 2],
[1, 0, 4, 4, 0],
[2, 4, 1, 1, 2]]
[j for i in ls for j in i if j != 0]
# [1, 3, 1, 2, 2, 3, 1, 1, 1, 4, 3, 1, 3, 4, 2, 3, 2, 1, 4, 4, 2, 4, 1, 1, 2]
If you want to avoid any explicit looping here's an option using itertools.chain and filter:
from itertools import chain
list(filter(lambda x: x != 0, chain(*ls)))
# [1, 3, 1, 2, 2, 3, 1, 1, 1, 4, 3, 1, 3, 4, 2, 3, 2, 1, 4, 4, 2, 4, 1, 1, 2]
Looks like you're also using NumPy for creating the list. Note that this would be way simpler and more efficient using np.nonzero:
import numpy as np
a = np.random.randint(0, 5, (7,5))
a[np.nonzero(a)]
# [1, 3, 1, 2, 2, 3, 1, 1, 1, 4, 3, 1, 3, 4, 2, 3, 2, 1, 4, 4, 2, 4, 1, 1, 2]
Adding to #yatu's answer, since it's 0, you can just do if x:
print([x for i in ls for x in i if x])
Or without loop:
print(np.array(ls).flatten()[np.array(ls).flatten() != 0].tolist())
Both output:
[1, 4, 4, 2, 1, 3, 3, 4, 4, 1, 3, 3, 2, 2, 2, 1, 4, 2, 2, 1, 4, 1, 2, 3, 2, 4]

Converting a heatmap image in the JPEG format to categories in python

I have a massive heatmap photo in the JPEG format and I'd like to convert the entries to 5 different categories based on the color of each one, and store the final result in three 2D-numpy arrays (One for section A, one for section B, one for section C).
The categories are as follows:
Red: 5,
Dark brown: 4,
Light brown: 3,
Light orange: 2,
White: 1
You can see the cropped photo that shows only a few top rows in the JPEG format in the following image:
How can I do this in python?
I would recommend you install and use OpenCV to help with the processing. This also provides a Python library to work with.
The following works for the heatmap you have given. You will need to modify it accordingly for your massive heatmap.
It first crops the image into 3, one for A, B and C respectively. Next it extracts the pixels for each category colour, converts them first to white and then resizes each to a 16 x 16 greyscale image. Next it converts non zero values to the current category value and adds this to your array.
import numpy as np
import cv2
def parse_image(img):
# Start with all zeros in a 16x16 array
data = np.zeros((16, 16), dtype=int)
# List holding min and max BGR values for each category
categories = [
(5, (0, 0, 250), (5, 5, 255)), # Red
(4, (5, 60, 152), (12, 78, 160)), # Dark brown
(3, (9, 105, 225), (18, 120, 240)), # Light brown
(2, (220, 235, 250), (230, 245, 255)), # Light orange
(1, (250, 250, 250), (255, 255, 255)), # White
]
for category, bgr_min, bgr_max in categories:
# Extract pixels in the required range and convert them to 255
mask = cv2.inRange(img, bgr_min, bgr_max)
image_cat = cv2.bitwise_or(img, np.full(img.shape, 255, dtype=np.uint8), mask=mask)
# Convert the image into greyscale
image_grey = cv2.cvtColor(image_cat, cv2.COLOR_BGR2GRAY)
# Resize the image to 16x16
values = cv2.resize(image_grey, (16, 16))
# Convert non black values into the current category value
values[values > 0] = category
# Add the values to the data array
data = data + values
return data
# Load the heatmap
image_src = cv2.imread("heatmap.jpg")
cv2.imshow("Source", image_src)
# Crop into 3 sub images
starty = 28
cropx = 234
cropy = 184
images = []
for number, startx in enumerate([30, 303, 572], start=1):
images.append(image_src[starty:starty+cropy,startx:startx+cropx])
# Parse A, B and C
abc = [parse_image(img) for img in images]
print abc
So for the heatmap you provided you would get the following output (which could be reshaped into a single array):
[array([[2, 2, 5, 3, 5, 3, 5, 4, 2, 5, 2, 2, 5, 2, 2, 2],
[2, 2, 2, 3, 2, 2, 4, 3, 2, 4, 2, 2, 5, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 3, 2, 3, 3, 2, 2, 5, 2, 2, 2],
[4, 3, 2, 5, 4, 2, 5, 5, 5, 5, 2, 2, 5, 2, 2, 2],
[2, 3, 2, 4, 2, 2, 5, 3, 3, 5, 2, 2, 5, 2, 2, 2],
[3, 4, 3, 5, 4, 2, 5, 5, 5, 5, 2, 2, 5, 2, 2, 2],
[4, 4, 5, 5, 4, 2, 5, 5, 5, 5, 2, 2, 5, 2, 2, 2],
[5, 5, 5, 5, 5, 3, 2, 2, 5, 2, 2, 2, 5, 3, 5, 5],
[3, 4, 5, 4, 5, 2, 5, 5, 5, 5, 2, 2, 5, 2, 3, 5],
[3, 3, 5, 5, 4, 2, 5, 5, 3, 5, 2, 2, 5, 2, 2, 3],
[4, 4, 5, 5, 5, 5, 2, 2, 5, 2, 2, 2, 5, 2, 2, 5],
[3, 2, 2, 2, 3, 2, 4, 5, 5, 3, 2, 2, 5, 2, 2, 5],
[1, 1, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 2],
[2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2],
[4, 3, 2, 5, 5, 2, 5, 5, 2, 5, 2, 2, 5, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2]]), array([[2, 3, 3, 3, 2, 2, 2, 2, 0, 3, 2, 3, 2, 2, 2, 3],
[2, 3, 2, 3,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2, 0, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2],
[2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5],
[2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 3, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2],
[1, 1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 3, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2]])]
Note, working with JPG images means there will be artefacts in image, so colours will not have exact values. This is why there are min and max values. Also normally you will see RGB values. But here you will be working with BGR values instead. You will need to tweak the min max values to ensure you do not get any 0 values.

Create histogram from dict of lists

I am trying to graph the frequency of the numbers 1, 2, and 3 that occur for certain keys in a dictionary (titled 'hat1' through 'hat10') and am having trouble converting my data (shown below) into a format that I might be able to graph.
data = {'hat9': [[1, 2, 3, 1, 2]], 'hat8': [[1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1]], 'hat2': [[1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'hat5': [[1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3]], 'hat4': [[1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1]], 'hat7': [[1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]], 'hat6': [[1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 1, 3]], 'hat10': [[1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 2, 1, 3, 2, 1, 3, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 3, 2, 2, 1, 2, 3, 3, 1, 2, 3, 2, 3, 3, 1, 3, 3, 3, 3]]}
When I ran DataFrame.from_dict(data) I received output that looked like this:
In [100]: DataFrame.from_dict(data)
Out[100]:
hat1 hat10 \
0 [1, 2, 3] [1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, ...
hat2 \
0 [1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
hat3 \
0 [1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, ...
hat4 \
0 [1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, ...
hat5 \
0 [1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, ...
hat6 \
0 [1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ...
hat7 \
0 [1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]
hat8 hat9
0 [1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ... [1, 2, 3, 1, 2]
I was hoping someone might be able to help me get the data into a more workable format that can be converted into a graph relatively easily. Thanks for your help.
If you want to create you histogram with Matplotlib, you don't really need to do much more than call its hist method with each hat you want to show. For example,
import pylab
pylab.hist(data['hat4'][0], bins=(1,2,3,4), align='left')
(You need to index at [0] because for some reason each of your dictionary values is a list of length 1, the single item itself being a list of data values).
If you need to aggregrate the hats in some way, you need to say how.
You can do the same with a pandas DataFrame if you prefer:
import pandas as pd
df = pd.DataFrame(data)
pylab.hist(df['hat4'], bins=(1,2,3,4), align='left')
Try this out:
data = {'hat9': [[1, 2, 3, 1, 2]], 'hat8': [[1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1]], 'hat2': [[1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'hat5': [[1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3]], 'hat4': [[1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1]], 'hat7': [[1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]], 'hat6': [[1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 1, 3]], 'hat10': [[1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 2, 1, 3, 2, 1, 3, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 3, 2, 2, 1, 2, 3, 3, 1, 2, 3, 2, 3, 3, 1, 3, 3, 3, 3]]}
keys = []
values = []
for key,value in data.iteritems():
keys.append(key)
a = 0
b = 0
c = 0
for x in value[0]:
if x==1: a+=1;
elif x ==2: b+=1;
elif x==3: c+=1;
values.append([a,b,c])
print keys
print values
Hopefully that helps. Keys is ['hat9', 'hat8', etc.,..] and values = [[freq of 1 in 'hats9', freq of 2 in 'hats9', freq of 3 in 'hats9'], [freq of 1 in 'hats8', freq of 2 in 'hats8', freq of 3 in 'hats8'],..] (a list of 3 item lists)

Permutations of a list of input numbers in Python

I'm supposed to write a program in Python that will get a list of numbers from the user until the user inputs 0, then print all the permutations of this list of numbers.
I was working on the code, which seemed right at the begining, but for some reason I'm getting weird output.
It seems like the list is static, and every time the function returns it adds objects to the latest list. This doesn't happen the way it was before the function was called recursively.
Here is what I have so far:
def permutation(numberList,array,place):
if (place==len(numberList)):
print array
else:
x=0
while (x < len(numberList)):
array.append(numberList[x])
permutation(numberList,array,place+1)
x+=1
def scanList():
numberList=[];
number=input()
#keep scanning for numbers for the list
while(number!=0):
numberList.append(number)
number=input()
return numberList
permutation(scanList(),[],0)
This is the output for the input 1 2 3 0, for example:
[1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1]
[1, 1, 1, 2, 3, 2, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3]
[1, 1, 1, 2, 3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 3, 1, 1, 2, 3, 2, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3]
I would appreciate any help.
The thing is, the list [] in Python is dynamic. So when you add elements to it with array.append(numberList[x]), they stay there forever. Just remove the added element after your recursive call:
def permutation(numberList,array,place):
if (place==len(numberList)):
print(array)
else:
x=0
while (x < len(numberList)):
array.append(numberList[x])
permutation(numberList,array,place+1)
array.pop()
x+=1
That's actually the common way to write depth-first search algorithms: modify your structure, make recursive call, undo modifications. The result of your program doesn't seem to be permutations of the input though.
The Python way is to use itertools.
from itertools import permutations
for permutation in permutations([1,2,3]):
print(permutation)
Now whats wrong with your algorithm. As you noticed, the list is static (well not really, but your using the same list every time)
A simple fix would be to copy the list each time.
def permutation(numberList,array,place):
if (place==len(numberList)):
print array
else:
x=0
while (x < len(numberList)):
array2 = array[:] // here happens the copy
array2.append(numberList[x])
permutation(numberList,array2,place+1)
x+=1

Categories