Related
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.
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]]]
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]]
I have this python3 code that merges my sub-list to a single list:
l=[[4, 5, 6], [10], [1, 2, 3], [10], [1, 2, 3], [10], [4, 5, 6], [1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9], [1, 2, 3], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [4, 5, 6], [10], [7, 8, 9], [7, 8, 9]]
import itertools
merged = list(itertools.chain(*l))
from collections import Iterable
def flatten(items):
"""Yield items from any nested iterable; see Reference."""
for x in items:
if isinstance(x, Iterable) and not
isinstance(x, (str, bytes)):
for sub_x in flatten(x):
yield sub_x
else:
yield x
merged = list(itertools.chain(*l))
merged
The Undesired Shape of the Output
Though the output produces what I want but the shape of the output is not what I want
the output comes out in vertical shape as shown bellow:
[4,
5,
6,
10,
1,
2,
3,
10,
1,
2,
3,
10,
4,
5,
6,
1,
2,
3,
4,
5,
6,
4,
5,
6,
7,
8,
9,
1,
2,
3,
7,
8,
9,
1,
2,
3,
4,
5,
6,
7,
8,
9,
4,
5,
6,
10,
7,
8,
9,
7,
8,
9]
The Desirable Shape of the Output as I Would Want It
I would rather want the output to come out horizontally as I present bellow:
[4, 5, 6, 10, 1, 2, 3, 10, 1, 2, 3, 10, 4, 5, 6, 1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9, 1, 2, 3, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 10, 7, 8, 9, 7, 8, 9]
Please help me out, I will not mind if there is a way to make this happen different from my code.
Instead just use list comprehention:
l=[[4, 5, 6], [10], [1, 2, 3], [10], [1, 2, 3], [10], [4, 5, 6], [1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9], [1, 2, 3], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [4, 5, 6], [10], [7, 8, 9], [7, 8, 9]]
new_l=[j for i in l for j in i]
print(new_l)
Output :
C:\Users\Desktop>py x.py
[4, 5, 6, 10, 1, 2, 3, 10, 1, 2, 3, 10, 4, 5, 6, 1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9, 1, 2, 3, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 10, 7, 8, 9, 7, 8, 9]
You can do it like this:
In [5]: for x in merged:
...: print(x, end=' ')
...:
4 5 6 10 1 2 3 10 1 2 3 10 4 5 6 1 2 3 4 5 6 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 7 8 9 4 5 6 10 7 8 9 7 8 9
from collections import Counter
import itertools
import operator
list1=[[4, 5, 6], [10], [1, 2, 3], [10], [1, 2, 3], [10], [4, 5, 6], [1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9], [1, 2, 3], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [4, 5, 6], [10], [7, 8, 9], [7, 8, 9]]
dd = [item for sublist in list1 for item in sublist]
print(dd) # method 1
out1 = reduce(operator.concat,list1)
print(out1) # method 2
merged1 = list(itertools.chain.from_iterable(list1))
print(merged1) # method 3
merged2 = list(itertools.chain(*list1))
print(merged2) # method 4
I have two dictionaries like so:
concave relations : {6: [2, 3, 4, 5], 2: [6], 3: [6], 4: [6], 5: [6]}
convex relations : {1: [2, 3, 4, 5], 2: [1, 3, 5], 3: [1, 2, 4], 4: [1, 3, 5], 5: [1, 2, 4], 6: [7, 8, 9, 10], 7: [6, 8, 10, 11], 8: [6, 7, 9, 11], 9: [6, 8, 10, 11], 10: [6, 7, 9, 11], 11: [7, 8, 9, 10]}
Previously I could find the key which corresponds to the item of max length using this code:
bottom_face = max(concave, key=lambda x:len(concave[x]))
Since the concave dict doesn't contain any items of the same length
Since this is not the case in the convex dict, and I want to return all of the keys which have max length items, I tried using the following:
possible_top_faces = [i for i, x in enumerate(convex) if x == max(convex, key=lambda x:len(convex[x]))]
But it is just returning:
[0]
Instead of the keys 1, 6, 7, 8, 9, 10, 11.
Can anyone help me out?
You can get the largest length of any key in convex, and use that as your standard for filtering out other keys in convex:
convex = {
1: [2, 3, 4, 5],
2: [1, 3, 5],
3: [1, 2, 4],
4: [1, 3, 5],
5: [1, 2, 4],
6: [7, 8, 9, 10],
7: [6, 8, 10, 11],
8: [6, 7, 9, 11],
9: [6, 8, 10, 11],
10: [6, 7, 9, 11],
11: [7, 8, 9, 10]
}
longest_len = max(map(len, convex.values()))
max_lens = [k for k, v in convex.items() if len(v) == longest_len]
print(max_lens) # [1, 6, 7, 8, 9, 10, 11]