Expand a python dictionary to express shared list values - python

Let's say I have the following simple dictionary in Python3.x:
example = {1:[4, 5, 6], 2:[7, 8, 9]}
I would like a way to expand the dictionary as follows:
expanded_example = {1:[4, 5, 6], 2:[7, 8, 9], 4:[5, 6], 5:[4, 6], 6:[4, 5], 7:[8, 9], 8:[7, 9], 9:[7, 8]}
This becomes quite complicated by values shared by multiple keys. As an example,
example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
Here 4 is a value in the lists associated with 1 and 2.
There are two approaches if there are "repeat" value elements:
(1) Only keep values immediately associated with a certain key:
{1:[4, 5, 6], 2:[4, 7, 8, 9], 4:[5, 6], 5:[4, 6], 6:[4, 5], 7:[8, 9], 8:[7, 9], 9:[7, 8]}
(2) Keep all associated values (as '4' is shared between keys '1' and '2'):
{1:[4, 5, 6], 2:[4, 7, 8, 9], 4:[5, 6, 7, 8, 9], 5:[4, 6], 6:[4, 5], 7:[4, 8, 9], 8:[4, 7, 9], 9:[4, 7, 8]}
EDITED:
My thought for this task was to use collections.defaultdict:
from collections import defaultdict
dict1 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
d_dict = defaultdict(list)
for k,l in dict1.items():
for v in l:
d_dict[v].append(l)
print(d_dict)
## defaultdict(<class 'list'>, {4: [[4, 5, 6], [4, 7, 8, 9]], 5: [[4, 5, 6]], 6: [[4, 5, 6]], 7: [[4, 7, 8, 9]], 8: [[4, 7, 8, 9]], 9: [[4, 7, 8, 9]]})
This gets me some of the way, but there are repeat elements in lists of lists...

strategy 2
example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
output = {**example2}
for val in example2.values():
for idx,v in enumerate(val):
if v not in output:
output[v] = val[0:idx]+val[idx+1:]
else:
output[v].extend(val[0:idx]+val[idx+1:])
print(output)
#{1: [4, 5, 6], 2: [4, 7, 8, 9], 4: [5, 6, 7, 8, 9], 5: [4, 6], 6: [4, 5], 7: [4, 8, 9], 8: [4, 7, 9], 9: [4, 7, 8]}
strategy 1
import copy
example2 = {1:[4, 5, 6], 2:[4, 7, 8, 9]}
output = copy.deepcopy(example2)
for val in example2.values():
for num in val:
if num in output:
val.remove(num)
for idx,v in enumerate(val):
output[v] = val[0:idx]+val[idx+1:]
print(output)
#{1: [4, 5, 6], 2: [4, 7, 8, 9], 4: [5, 6], 5: [4, 6], 6: [4, 5], 7: [8, 9], 8: [7, 9], 9: [7, 8]}

Note: This answer only deals with Approach #1.
You can work with copies of your data, as you should not add/remove dictionary items while iterating a view:
d = {1:[4, 5, 6], 2:[7, 8, 9]}
for k, v in list(d.items()):
for w in v:
L = v.copy()
d[L.pop(L.index(w))] = L
print(d)
{1: [4, 5, 6], 2: [7, 8, 9], 4: [5, 6], 5: [4, 6],
6: [4, 5], 7: [8, 9], 8: [7, 9], 9: [7, 8]}

Related

Running DFS on a graph whose keys have multiple values

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.

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]]]

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]]

How to Make my Merge output Horizontally instead of Vertically in python

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

Use max() function to get keys of dict items with max length

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]

Categories

Resources