Running DFS on a graph whose keys have multiple values - python
1 []
2 []
3 [[3, 4, 7, 5], [3, 10, 6, 10]]
4 [[4, 5, 2, 7]]
5 []
6 [[6, 7, 5, 4]]
7 [[7, 8, 4, 2]]
8 [[8, 9, 10, 4], [8, 9, 1, 10]]
9 [[9, 10, 7, 3], [9, 10, 7, 9], [9, 10, 3, 7]]
10 []
The dictinary in the image has integer keys and there is a list of sublists as values. The goal is to find all sublists of every key such that the second element of the previous list is less than the second element of the next. For example:
[[3, 4, 7, 5],[4, 5, 2, 7],[6, 7, 5, 4],[9, 10, 7, 3]] is an acceptable output.
with 3 being the first key that has non empty list as value and 10 being the final key we reach. I just need to find all possible paths that lead to 10.
You can use simple list manipulation:
import itertools
values = [
[],
[],
[[3, 4, 7, 5], [3, 10, 6, 10]],
[[4, 5, 2, 7]],
[],
[[6, 7, 5, 4]],
[[7, 8, 4, 2]],
[[8, 9, 10, 4], [8, 9, 1, 10]],
[[9, 10, 7, 3], [9, 10, 7, 9], [9, 10, 3, 7]],
[]
]
# Remove the empty items from the list.
filter_empty = [items for items in values if items]
# Append None to each sub-list after the first to allow for later items to not be picked.
values_with_none = [items + ([None] if index > 0 else []) for index, items in enumerate(filter_empty)]
# Generate all combinations of picking one item from each sub-list.
all_combinations = list(itertools.product(*values_with_none))
# Remove all the None elements from the sub-lists.
filter_nones = [[v for v in arr if v is not None] for arr in all_combinations]
# Filter out all the sub-lists where the last element does not have a second element of 10.
ending_with_10 = [arr for arr in filter_nones if arr[-1][1] == 10]
# Filter out all the sub-lists where the second elements are not in order.
filter_lt = [arr for arr in ending_with_10 if all(map(lambda v: v[0][1] < v[1][1], zip(arr[0:-1], arr[1:])))]
for arr in filter_lt:
print(arr)
Which outputs:
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [7, 8, 4, 2], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [6, 7, 5, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [7, 8, 4, 2], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [4, 5, 2, 7], [9, 10, 7, 3]]
[[3, 4, 7, 5], [4, 5, 2, 7], [9, 10, 7, 9]]
[[3, 4, 7, 5], [4, 5, 2, 7], [9, 10, 3, 7]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [9, 10, 7, 3]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [9, 10, 7, 9]]
[[3, 4, 7, 5], [6, 7, 5, 4], [7, 8, 4, 2], [9, 10, 3, 7]]
[[3, 4, 7, 5], [6, 7, 5, 4], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [6, 7, 5, 4], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [6, 7, 5, 4], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [6, 7, 5, 4], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [6, 7, 5, 4], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [6, 7, 5, 4], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [6, 7, 5, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [6, 7, 5, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [6, 7, 5, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [7, 8, 4, 2], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [7, 8, 4, 2], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [7, 8, 4, 2], [9, 10, 7, 3]]
[[3, 4, 7, 5], [7, 8, 4, 2], [9, 10, 7, 9]]
[[3, 4, 7, 5], [7, 8, 4, 2], [9, 10, 3, 7]]
[[3, 4, 7, 5], [8, 9, 10, 4], [9, 10, 7, 3]]
[[3, 4, 7, 5], [8, 9, 10, 4], [9, 10, 7, 9]]
[[3, 4, 7, 5], [8, 9, 10, 4], [9, 10, 3, 7]]
[[3, 4, 7, 5], [8, 9, 1, 10], [9, 10, 7, 3]]
[[3, 4, 7, 5], [8, 9, 1, 10], [9, 10, 7, 9]]
[[3, 4, 7, 5], [8, 9, 1, 10], [9, 10, 3, 7]]
[[3, 4, 7, 5], [9, 10, 7, 3]]
[[3, 4, 7, 5], [9, 10, 7, 9]]
[[3, 4, 7, 5], [9, 10, 3, 7]]
[[3, 10, 6, 10]]
Or you can generate all combinations from a list of lists using:
def product(*args):
pools = map(tuple, args)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
# Generate all combinations of picking one item from each sub-list.
all_combinations = list(product(*values_with_none))
Which outputs the same.
Related
Why does modifying a copy of a list also modify other copies of the same list?
grid = np.empty((10,0)).tolist() coords = grid.copy() distances = grid.copy() print(id(grid), id(coords), id(distances)) for i in range(10): for j in range(10): distances[i].append(i+j) coords[i].append([i, j]) print(distances) print(coords) I created a nested list for turning into a 2D grid. I want to create two different 2D grid, one containing the coordinate of each element, the other containing the distance of each coordinate from (0,0). I used the .copy() method twice, and confirmed that coords and distances are in fact different locations in memory. So why does my code output both as the same incorrect list? Output: [[0, [0, 0], 1, [0, 1], 2, [0, 2], 3, [0, 3], 4, [0, 4], 5, [0, 5], 6, [0, 6], 7, [0, 7], 8, [0, 8], 9, [0, 9]], [1, [1, 0], 2, [1, 1], 3, [1, 2], 4, [1, 3], 5, [1, 4], 6, [1, 5], 7, [1, 6], 8, [1, 7], 9, [1, 8], 10, [1, 9]], [2, [2, 0], 3, [2, 1], 4, [2, 2], 5, [2, 3], 6, [2, 4], 7, [2, 5], 8, [2, 6], 9, [2, 7], 10, [2, 8], 11, [2, 9]], [3, [3, 0], 4, [3, 1], 5, [3, 2], 6, [3, 3], 7, [3, 4], 8, [3, 5], 9, [3, 6], 10, [3, 7], 11, [3, 8], 12, [3, 9]], [4, [4, 0], 5, [4, 1], 6, [4, 2], 7, [4, 3], 8, [4, 4], 9, [4, 5], 10, [4, 6], 11, [4, 7], 12, [4, 8], 13, [4, 9]], [5, [5, 0], 6, [5, 1], 7, [5, 2], 8, [5, 3], 9, [5, 4], 10, [5, 5], 11, [5, 6], 12, [5, 7], 13, [5, 8], 14, [5, 9]], [6, [6, 0], 7, [6, 1], 8, [6, 2], 9, [6, 3], 10, [6, 4], 11, [6, 5], 12, [6, 6], 13, [6, 7], 14, [6, 8], 15, [6, 9]], [7, [7, 0], 8, [7, 1], 9, [7, 2], 10, [7, 3], 11, [7, 4], 12, [7, 5], 13, [7, 6], 14, [7, 7], 15, [7, 8], 16, [7, 9]], [8, [8, 0], 9, [8, 1], 10, [8, 2], 11, [8, 3], 12, [8, 4], 13, [8, 5], 14, [8, 6], 15, [8, 7], 16, [8, 8], 17, [8, 9]], [9, [9, 0], 10, [9, 1], 11, [9, 2], 12, [9, 3], 13, [9, 4], 14, [9, 5], 15, [9, 6], 16, [9, 7], 17, [9, 8], 18, [9, 9]]]
How to transfer List of nested lists into a NumPy array?
I have the following sample object: list_sample = [['N', [], None], ['B:maj', [3, 6, 11], 11], ['C#:maj', [1, 5, 8], 1], ['Bb:min', [1, 5, 10], 10], ['Eb:min/b3', [3, 6, 6, 10], 6], ['B:maj', [3, 6, 11], 11], ['C#:maj', [1, 5, 8], 1], ['F#:maj', [1, 6, 10], 6], ['F#:maj', [1, 6, 10], 6], ['B:maj', [3, 6, 11], 11], ['C#:maj', [1, 5, 8], 1], ['Bb:min', [1, 5, 10], 10], ['Eb:min', [3, 6, 10], 3], ['B:maj', [3, 6, 11], 11]] I would like to transfer this list into a ndarray, so I tried this method suggested in this postDebugging Numpy VisibleDeprecationWarning (ndarray from ragged nested sequences): np.array(formatted_chords,dtype = object) But it gives me undesired result like this: [['N' list([]) None] ['B:maj' list([3, 6, 11]) 11] ['C#:maj' list([1, 5, 8]) 1] ['Bb:min' list([1, 5, 10]) 10] ['Eb:min/b3' list([3, 6, 6, 10]) 6] ['B:maj' list([3, 6, 11]) 11]] My target output would a ndarray which looks like this: [['N',[],None] ['B:maj',[3, 6, 11]),11] ['C#:maj',[1, 5, 8]),1] ['Bb:min',[1, 5, 10]),10] ['Eb:min/b3',[3, 6, 6, 10],6] ['B:maj',[3, 6, 11],11]] Can anybody shares how to do this transformation?
Spliting a list into n uneven buckets with all combinations
I have a list like: lst = [1,2,3,4,5,6,7,8,9,10] and I want to get the combination of all splits for a given n bucket without changing the order of the list. Output exp for n=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], ] Python is the language I use but if you can direct me to an algorithm that would nice as well. I see this problem is usually apllied on strings. But couldn't figure it out on the list. P.S. this is my first question. Any feedback is appreciated on how to improve the question.
Try: from itertools import product def generate(n, l): for c in product(range(1, l), repeat=n - 1): s = sum(c) if s > l - 1: continue yield *c, l - s lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] n = 3 for groups in generate(n, len(lst)): l, out = lst, [] for g in groups: out.append(l[:g]) l = l[g:] print(out) Prints: [[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]] [[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]]
For a manual implementation, you could use a recursive generator function: def parts(lst, n): if 0 < n <= len(lst): if n == 1: yield [lst] else: for i in range(1, len(lst)-n+2): for part in parts(lst[i:], n-1): yield [lst[:i]] + part pprint(list(parts([1,2,3,4], 3))) # [[[1], [2], [3, 4]], # [[1], [2, 3], [4]], # [[1, 2], [3], [4]]] pprint(list(parts([1,2,3,4,5,6], 3))) # [[[1], [2], [3, 4, 5, 6]], # [[1], [2, 3], [4, 5, 6]], # [[1], [2, 3, 4], [5, 6]], # [[1], [2, 3, 4, 5], [6]], # [[1, 2], [3], [4, 5, 6]], # [[1, 2], [3, 4], [5, 6]], # [[1, 2], [3, 4, 5], [6]], # [[1, 2, 3], [4], [5, 6]], # [[1, 2, 3], [4, 5], [6]], # [[1, 2, 3, 4], [5], [6]]]
A slightly shorter recursive approach: lst, n = [1,2,3,4,5,6,7,8,9,10], 3 def group(d, c = []): if not d and len(c) == n: yield c if d and c: yield from group(d[1:], c[:-1]+[c[-1]+[d[0]]]) if d and len(c) < n: yield from group(d[1:], c+[[d[0]]]) print(list(group(lst))) Output: [[[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]], [[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]]]
Is there a better method to create such a numpy array?
I want a numpy array like this: b = np.array([[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]) Is there a faster way to create a NumPy array like this instead of typing them manually?
You can do something like this: >>> np.repeat(np.arange(1, 10).reshape(-1,1), 6, axis=1) array([[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]) Explanation: np.arange(1, 10).reshape(-1,1) creates an array array([[1], [2], [3], [4], [5], [6], [7], [8], [9]]) np.repeat(_, 6, axis=1) repeats this 6 times on the first (or second in human words) axis.
Yes. There are plenty of methods. This is one: np.repeat(np.arange(1,10),6,axis=0).reshape(9,6)
Another method is to use broadcasting: >>> np.arange(1,10)[:,None] * np.ones(6, dtype=int) array([[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]])
For any w*l size, convert a list of lists into an np.array like so: w = 6 l = 9 [np.array([[1+i]*w for i in range(d)]) array([[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]])
np.transpose(np.array(([np.arange(1,10)] * 6))) np.arange(1,10) creates an numpy array from 1 to 9. [] puts the array into a list. *6 augments the array 6 times. np.array() converts the resulting structure (list of arrays) to a numpy array np.transpose() rotates the orientation of the numpy array to get vertical one.
Combination of few list with keeping order
I have a few lists that I want to make combinations the output should have the first item from list1 second from list2 and last from list3. I tried with for and append but it did not work can itertools be used? list1 = [1, 2, 3] list2 = [4, 5, 6] list3 = [7, 8 ,9] Output should be [[1,4,7],[2,4,7]...[3,6,9]]
You can also try considering simple list comprehension. What makes your question unique is the order of elements. The key point here is to have list1 as the inner most loop just to have the output in the order you want. result = [[i, j, k] for k in list3 for j in list2 for i in list1] Output [[1, 4, 7], [2, 4, 7], [3, 4, 7], [1, 5, 7], [2, 5, 7], [3, 5, 7], [1, 6, 7], [2, 6, 7], [3, 6, 7], [1, 4, 8], [2, 4, 8], [3, 4, 8], [1, 5, 8], [2, 5, 8], [3, 5, 8], [1, 6, 8], [2, 6, 8], [3, 6, 8], [1, 4, 9], [2, 4, 9], [3, 4, 9], [1, 5, 9], [2, 5, 9], [3, 5, 9], [1, 6, 9], [2, 6, 9], [3, 6, 9]]
You can use numpy print([list(x) for x in numpy.array(numpy.meshgrid(list1,list2,list3)).T.reshape(-1,len(a))]) Outputs: [[1, 4, 7], [1, 5, 7], [1, 6, 7], [2, 4, 7], [2, 5, 7], [2, 6, 7], [3, 4, 7], [3, 5, 7], [3, 6, 7], [1, 4, 8], [1, 5, 8], [1, 6, 8], [2, 4, 8], [2, 5, 8], [2, 6, 8], [3, 4, 8], [3, 5, 8], [3, 6, 8], [1, 4, 9], [1, 5, 9], [1, 6, 9], [2, 4, 9], [2, 5, 9], [2, 6, 9], [3, 4, 9], [3, 5, 9], [3, 6, 9]]