Python list unique - python-3.x
How can you access the element '5' from this list = [0 ,[ 6, 3], [4, 6, [7, 5] ] ] ?
The options we have :
1) return list[1][2]
2) return list[len(list)-1][1]
3) return list[2][len(list[2])-1][1]
4) list[2][2][2]
List:
[0 ,[ 6, 3], [4, 6, [7, 5] ] ]
list[2]:
[4, 6, [7, 5] ]
list[2][2]:
[7, 5]
list[2][2][1]:
5
list[2][len(list[2])-1][1] == list[2][2][1]
Related
Transponse a list of lists from 2nd element in python
list_of_lists = [[1, 2, 3, 4], [1, 5, 6, 7], [1, 8, 9, 10]] I would like to get to: transposed_list = [[1, 2, 5, 8], [1, 3, 6, 9], [1, 4, 7, 10]] In other words, only transpose from the 2nd element in the list, keeping the first element in place.
Try: list_of_lists = [[1, 2, 3, 4], [1, 5, 6, 7], [1, 8, 9, 10]] out = [ [list_of_lists[i][0]] + list(l) for i, l in enumerate(zip(*(l[1:] for l in list_of_lists))) ] print(out) Prints: [[1, 2, 5, 8], [1, 3, 6, 9], [1, 4, 7, 10]]
Python3 recursive, for-loop, function returns strange output
My function combinations(ary, p) is suposed to return every possible combination, of order of items in list. But it always returns the first value multiple times, even tho it finds all the possible orders, which I know because it prints them. EDIT: I would like to make it work my self, learn something and not use any external libs. def combinations(ary, p): ln = len(ary) bary=[] for a in range(ln-p): if p<ln-2: bb = combinations(ary, p+1) for be in bb: bary.append(be) if p>=ln-2: bary.append(ary) ary.append(ary.pop(p)) return bary Another version with debug print() functions. I will give its sample output. def combinations(ary, p): ln = len(ary) bary=[] for a in range(ln-p): if p<ln-2: bb = combinations(ary, p+1) for be in bb: bary.append(be) if p>=ln-2: --> bary.append(ary) --> print('ary', ary, 'bary', bary) ary.append(ary.pop(p)) return bary Console output after running with combinations([7,3,2], 0):: ## There is clearly every possible combination: ## ## || ## || \/ ary [7, 3, 2] bary [[7, 3, 2]] ary [7, 2, 3] bary [[7, 2, 3], [7, 2, 3]] ary [3, 2, 7] bary [[3, 2, 7]] ary [3, 7, 2] bary [[3, 7, 2], [3, 7, 2]] ary [2, 7, 3] bary [[2, 7, 3]] ary [2, 3, 7] bary [[2, 3, 7], [2, 3, 7]] [[7, 3, 2], [7, 3, 2], [7, 3, 2], [7, 3, 2], [7, 3, 2], [7, 3, 2]] The last list is suposed to include every possible order, but it has only the input value order even tho it prints every order. So where did I mess up the return?
Your issue is there is only one copy of ary. You append it to bary many times. So bary is full of the same one list. Any time you make a change to ary. That will be reflected in all of them cause they are all the same list. Instead when you append to bary assign a copy of ary but as a new list of its own so it wont be affected when you change ary def combinations(ary, p): ln = len(ary) bary=[] for a in range(ln-p): if p<ln-2: bb = combinations(ary, p+1) for be in bb: bary.append(be) if p>=ln-2: bary.append(ary[:]) #Changed this line to take a shallow copy of ary print('ary', ary, 'bary', bary) ary.append(ary.pop(p)) return bary print(combinations([7,3,2], 0)) OUTPUT ary [7, 3, 2] bary [[7, 3, 2]] ary [7, 2, 3] bary [[7, 3, 2], [7, 2, 3]] ary [3, 2, 7] bary [[3, 2, 7]] ary [3, 7, 2] bary [[3, 2, 7], [3, 7, 2]] ary [2, 7, 3] bary [[2, 7, 3]] ary [2, 3, 7] bary [[2, 7, 3], [2, 3, 7]] [[7, 3, 2], [7, 2, 3], [3, 2, 7], [3, 7, 2], [2, 7, 3], [2, 3, 7]]
How can I have a python function that effectively divide a list to all possible number of chunks?
I want to split a list (float or integer) according to the following conditions: Splitting the list into all possible subsamples. No duplication. A unit sample cannot be a subsample. I have what splits a list into equal sizes by giving the number of subsamples. The code I have laid my hand on which worked but does not give me what I want import numpy as np x = [1,2,3,4,5,6,7,8,9,10] l = np.array_split(x,3) output [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]] I desire to have a list of subsample of all possibilities without duplication. that is, a combination of all unique possibilities when the list is split into 2, 3, 4, etc (no two or more subsamples will have the same element)). I do not what to specify chunk number so that it will not be limited to such number. Here is what I did manually From the following series [1,2,3,4,5,6,7,8,9,10] I sliced it into all possible blocks as follows: when splitting into 2 [1][2,3,4,5,6,7,8,9,10] [1,2][3,4,5,6,7,8,9,10] [1,2,3][4,5,6,7,8,9,10] [1,2,3,4][5,6,7,8,9,10] [1,2,3,4,5][6,7,8,9,10] [1,2,3,4,5,6][7,8,9,10] [1,2,3,4,5,6,7,8][9,10] [1,2,3,4,5,6,7,8,9][10] when splitting into 3 [1][2][3,4,5,6,7,8,9,10] [1][2,3][4,5,6,7,8,9,10] [1][2,3,4][5,6,7,8,9,10] [1][2,3,4,5][6,7,8,9,10] [1][2,3,4,5,6][7,8,9,10] [1][2,3,4,5,6,7][8,9,10] [1][2,3,4,5,6,7,8][9,10] [1][2,3,4,5,6,7,8,9][10] [1,2][3][4,5,6,7,8,9,10] [1,2][3,4][5,6,7,8,9,10] [1,2][3,4,5][6,7,8,9,10] [1,2][3,4,5,6][7,8,9,10] [1,2][3,4,5,6,7][8,9,10] [1,2][3,4,5,6,7,8][9,10] [1,2][3,4,5,6,7,8,9][10] [1,2,3][4][5,6,7,8,9,10] [1,2,3][4,5][6,7,8,9,10] [1,2,3][4,5,6][7,8,9,10] [1,2,3][4,5,6,7][8,9,10] [1,2,3][4,5,6,7,8][9,10] [1,2,3][4,5,6,7,8,9][10] [1,2,3,4][5][6,7,8,9,10] [1,2,3,4][5,6][7,8,9,10] [1,2,3,4][5,6,7][8,9,10] [1,2,3,4][5,6,7,8][9,10] [1,2,3,4][5,6,7,8,9][10] [1,2,3,4,5][6][7,8,9,10] [1,2,3,4,5][6,7][8,9,10] [1,2,3,4,5][6,7,8][9,10] [1,2,3,4,5][6,7,8,9][10] [1,2,3,4,5,6][7][8,9,10] [1,2,3,4,5,6][7,8][9,10] [1,2,3,4,5,6][7,8,9][10] when splitting into 4 [1,2,3,4,5,6,7][8][9,10] [1,2,3,4,5,6,7][8,9][10] [1,2,3,4,5,6,7,8][9][10] After all possible splitting into blocks, I removed all the single digit blocks and also removed all duplicated blocks. [2,3,4,5,6,7,8,9,10] [1,2,3,4,5,6,7,8,9] [3,4,5,6,7,8,9,10] [2,3] [2,3,4] [2,3,4,5] [2,3,4,5,6] [2,3,4,5,6,7] [2,3,4,5,6,7,8] [2,3,4,5,6,7,8,9] [4,5,6,7,8,9,10] [3,4] [3,4,5] [3,4,5,6] [3,4,5,6,7] [3,4,5,6,7,8] [1,2][3,4,5,6,7,8,9] [5,6,7,8,9,10] [4,5] [4,5,6] [4,5,6,7] [4,5,6,7,8] [1,2,3][4,5,6,7,8,9] [6,7,8,9,10] [5,6] [5,6,7] [5,6,7,8] [1,2,3,4][5,6,7,8,9] [1,2,3,4,5][7,8,9,10] [6,7] [6,7,8] [6,7,8,9] [8,9,10] [7,8] [1,2,3,4,5,6][7,8,9] [9,10] [1,2,3,4,5,6,7][8,9] [1,2,3,4,5,6,7,8] Here, I gather all the possible chunks together. This is what I desire as an output. [[2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9], [3,4,5,6,7,8,9,10], [2,3], [2,3,4], [2,3,4,5], [2,3,4,5,6], [2,3,4,5,6,7], [2,3,4,5,6,7,8], [2,3,4,5,6,7,8,9], [4,5,6,7,8,9,10],[3,4], [3,4,5], [3,4,5,6], [3,4,5,6,7], [3,4,5,6,7,8], [1,2], [3,4,5,6,7,8,9], [5,6,7,8,9,10], [4,5], [4,5,6], [4,5,6,7], [4,5,6,7,8], [1,2,3], [4,5,6,7,8,9], [6,7,8,9,10], [5,6], [5,6,7], [5,6,7,8], [1,2,3,4], [5,6,7,8,9], [1,2,3,4,5], [7,8,9,10], [6,7], [6,7,8], [6,7,8,9], [8,9,10], [7,8], [1,2,3,4,5,6], [7,8,9], [9,10], [1,2,3,4,5,6,7], [8,9], [1,2,3,4,5,6,7,8]]
It looks to me like the problem reduces to finding all substrings of length 2 or greater that leave at least one fragment of length 1. In other words, you won't have to enumerate every partition to find them. def parts(thing): result = [] for i in range(len(thing)): for j in range(i + 1, len(thing) + 1): if 1 < len(thing[i:j]) < len(thing): result.append(thing[i:j]) return result res = parts([*range(1,11)]) # res # [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], # [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8], # [1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3], [2, 3, 4], [2, 3, 4, 5], # [2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], # [2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 10], [3, 4], [3, 4, 5], # [3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], # [3, 4, 5, 6, 7, 8, 9, 10], [4, 5], [4, 5, 6], [4, 5, 6, 7], [4, 5, 6, 7, 8], # [4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 10], [5, 6], [5, 6, 7], [5, 6, 7, 8], # [5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10], [6, 7], [6, 7, 8], [6, 7, 8, 9], # [6, 7, 8, 9, 10], [7, 8], [7, 8, 9], [7, 8, 9, 10], [8, 9], [8, 9, 10], # [9, 10]]
Groovy: create a two-dimensional array from two ranges
I want to create this: [ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ] ... [ 6, 3 ]] Using GroovyConsole I've being trying stuff like this: def blob = (1..6).collect{ i -> (1..3).collect{ j -> [ i, j ] } } println "$blob ${blob.class.simpleName}" ... with various permutations of calls to flatten, as you might surmise. Is there a way to achieve this?
It's easier to do using the combinations method: l1 = [1,2,3,4,5,6] l2 = [1,2,3] [l, l2].combinations() Which outputs: [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3]]
How to find a column in a ndarray
Say i have a array x = array([[ 0, 1, 2, 5], [ 3, 4, 5, 5], [ 6, 7, 8, 5], [ 9, 10, 11, 5]]) I need to find the position/index of [3, 4, 5, 5]. In this case, it should return 1.
Create an array y that has all rows equal to the one you are looking for. Then, do an elementwise comparison x == y and find the rows where you get all True. import numpy as np x1 = np.array([[0, 1, 2, 5], [3, 4, 5, 5], [6, 7, 8, 5], [9, 10, 11, 5]]) y1 = np.array([[3, 4, 5, 5]] * 4) print(np.where(np.all(x1 == y1, axis=1))[0]) # => [1] This approach returns an array of the indices where the desired row appears. y2 = np.array([[1, 1, 1, 1]] * 4) print(np.where(np.all(x1 == y2, axis=1))[0]) # => [] x2 = np.array([[3, 4, 5, 5], [3, 4, 5, 5], [6, 7, 8, 5], [9, 10, 11, 5]]) print(np.where(np.all(x2 == y1, axis=1))[0]) # => [0 1]