Append Dictionary in List - python - python

I have a list and dictionaries in it.
Where (1,2,3) belongs to Z,
(4,5,6) belongs to X,
(7,8,9) belongs to V.
But it overwrites it because it constantly adds to the list in the dict.
When I use "=", I can only get the last data.
I want the output to be like this.
{'Property1': 'Z', 'Property2': [(1, 2, 3)]}
{'Property1': 'X', 'Property2': [(4, 5, 6)]}
{'Property1': 'V', 'Property2': [(7, 8, 9)]}
A1 = ["Z","X","V"]
A2 = [(1,2,3), (4,5,6), (7,8,9)]
list = []
Dict = {"Property1": "",
"Property2": [""],
}
for a in A1:
Dict["Property1"] = a
for b in A2:
Dict["Property2"].append(A2)
list.append(Dict)
print("-------\n")
print(Dict)
Now Output:
{'Property1': 'Z', 'Property2': ['', [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)]]}
-------
{'Property1': 'X', 'Property2': ['', [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)]]}
-------
{'Property1': 'V', 'Property2': ['', [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)], [(1, 2, 3), (4, 5, 6), (7, 8, 9)]]}
Thanks.

Try this. You can use zip() method
A1 = ["Z","X","V"]
A2 = [(1,2,3), (4,5,6), (7,8,9)]
List=[{"Property1": i,
"Property2": [j],
} for i,j in zip(A1,A2)]
print(List)
Output
[{'Property1': 'Z', 'Property2': [(1, 2, 3)]}, {'Property1': 'X', 'Property2': [(4, 5, 6)]}, {'Property1': 'V', 'Property2': [(7, 8, 9)]}]
For your code, you are appending all items in A2 with every iteration, but not clearing them
Instead, change the loop to this
for b,a in enumerate (A1):
Dict["Property1"] = a
Dict["Property2"]=[A2[b]]
list1.append(Dict)
print("-------\n")
print(Dict)

Since both the lists are of same length, you can iterate over them using len() and update your Dict.
Don't use list as variable names. It's a reserved word in Python.
A1 = ["Z","X","V"]
A2 = [(1,2,3), (4,5,6), (7,8,9)]
lst = []
Dict = {}
n = len(A1)
for i in range(n):
Dict["Property1"] = A1[i]
Dict["Property2"] = [A2[i]]
lst.append(dict(Dict))
for l in lst:
print(l)
{'Property1': 'Z', 'Property2': [(1, 2, 3)]}
{'Property1': 'X', 'Property2': [(4, 5, 6)]}
{'Property1': 'V', 'Property2': [(7, 8, 9)]}

Related

How to zip a list with a list of tuples?

Say I have the following lists:
list_a = [1, 4, 7]
list_b = [(2, 3), (5, 6), (8, 9)]
How do I combine them so that it becomes
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
You can expand the tuple in the second zipped item to build a final tuple
list_a = [1, 4, 7]
list_b = [(2, 3), (5, 6), (8, 9)]
print([(a,*b) for a,b in zip(list_a, list_b)])
Output
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
list_a = [1, 4, 7]
list_b = [(2, 3), (5, 6), (8, 9)]
# FOR LIST_A TO B
for i in range(len(list_a)):
a = list_a[i]
test = list_b[i] + (a,)
print(test)

Backtracking to create squares

a = []
def make_squares(arr, length, nums):
if not nums:
print(arr)
a.append(arr)
return
r = 0
while r < len(arr) and len(arr[r]) == length:
r += 1
for i in nums:
nums.remove(i)
arr[r].append(i)
make_squares(arr, length, nums)
nums.append(i)
arr[r] = arr[r][:-1]
make_squares([[] for i in range(3)], 3, [i+1 for i in range(3**2)])
print(a)
I'm trying to use the above code to create nxn matrices each with a permutation of the numbers i+1...n^2. I have printed every matrix that gets appended to a and they appear correct, but when I print a in the end, I get [[[], [], []], [[], [], []], ...]. It doesn't make any sense to me.
The expected result would be
[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 9, 8]], ...]
The order may not be the same as in your expected result, but this does the trick with the standard library itertools module:
import itertools
def make_squares(w):
size = w * w
for perm in itertools.permutations(range(1, size + 1)):
# "grouper" from the itertools recipe list
yield list(itertools.zip_longest(*[iter(perm)] * w))
for square in make_squares(3):
print(square)
prints
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
[(1, 2, 3), (4, 5, 6), (7, 9, 8)]
[(1, 2, 3), (4, 5, 6), (8, 7, 9)]
[(1, 2, 3), (4, 5, 6), (8, 9, 7)]
[(1, 2, 3), (4, 5, 6), (9, 7, 8)]
[(1, 2, 3), (4, 5, 6), (9, 8, 7)]
[(1, 2, 3), (4, 5, 7), (6, 8, 9)]
[(1, 2, 3), (4, 5, 7), (6, 9, 8)]
[(1, 2, 3), (4, 5, 7), (8, 6, 9)]
[(1, 2, 3), (4, 5, 7), (8, 9, 6)]
[(1, 2, 3), (4, 5, 7), (9, 6, 8)]
[(1, 2, 3), (4, 5, 7), (9, 8, 6)]
[(1, 2, 3), (4, 5, 8), (6, 7, 9)]
[(1, 2, 3), (4, 5, 8), (6, 9, 7)]
[(1, 2, 3), (4, 5, 8), (7, 6, 9)]
...

Creating specific permutations Python

I have values for a 2-dimensional list:
# 4 x 4, 2-dimensional list
values = [[4, 7, 8],
[1, 3, 4],
[7, 5, 6],
[2, 9, 1]]
I want to create tuples containing all possible permutations of these values (Cartesian product) for each list.
# example for the list at index 0 of values
args0 = [(4, 7, 8), (7, 4, 8), (4, 8, 7), (8, 4, 7), (7, 8, 4), (8, 7, 4)]
Is there an easy way to go about this? I have tried itertools but cannot get it to work with "specific values".
What you want is the permutations of each element in the list, so just map itertools.permutations:
import itertools
values = [[4, 7, 8],
[1, 3, 4],
[7, 5, 6],
[2, 9, 1]]
perms = map(itertools.permutations, values)
for v in perms:
print(list(v))
Result:
[(4, 7, 8), (4, 8, 7), (7, 4, 8), (7, 8, 4), (8, 4, 7), (8, 7, 4)]
[(1, 3, 4), (1, 4, 3), (3, 1, 4), (3, 4, 1), (4, 1, 3), (4, 3, 1)]
[(7, 5, 6), (7, 6, 5), (5, 7, 6), (5, 6, 7), (6, 7, 5), (6, 5, 7)]
[(2, 9, 1), (2, 1, 9), (9, 2, 1), (9, 1, 2), (1, 2, 9), (1, 9, 2)]
Here you have a live example

How to iterate few elements in Python arrays?

For example I have a list of objects like this:
[[{1},{2},{3}],[{4},{5}],[{6},{7},{8}]]
I need to iterate through them all to get on each iteration objects like:
1,4,6
1,4,7
1,4,8
1,5,6
1,5,7
1,5,8
2,4,6
2,4,7
2,4,8
2,5,6
2,5,7
2,5,8
Basically each result is like a sub array of the input lists.
You can easily use itertools.product
>>> import itertools
>>> x = list(itertools.product([1,2,3],[4,5],[6,7,8]))
[(1, 4, 6), (1, 4, 7), (1, 4, 8), (1, 5, 6), (1, 5, 7), (1, 5, 8), (2, 4, 6), (2, 4, 7), (2, 4, 8), (2, 5, 6), (2, 5, 7), (2, 5, 8), (3, 4, 6), (3, 4, 7), (3, 4, 8), (3, 5, 6), (3, 5, 7), (3, 5, 8)]
Note that the output of every combination you are looking for is called the Cartesian product of your input lists.

generating tuples using n-lists with itertools.product

How can I use itertools.product function if I don't know the number of the lists? I have list and it has lists inside of it.
Like,
lis = [[1,2,3],[6,7,8],[2,4,5]]
Normally I need to do,
product([1,2,3],[6,7,8],[2,4,5])
How do I do that if the input is a list like in the example?
Try the following:
product(*lis)
It's called argument unpacking.
Short note: you can use argument unpacking with named parameters also, with double star:
def simpleSum(a=1,b=2):
return a + b
simpleSum(**{'a':1,'b':2}) # returns 3
Use argument unpacking:
>>> lis = [[1,2,3],[6,7,8],[2,4,5]]
>>> list(itertools.product(*lis))
[(1, 6, 2), (1, 6, 4), (1, 6, 5), (1, 7, 2), (1, 7, 4), (1, 7, 5), (1, 8, 2),
(1, 8, 4), (1, 8, 5), (2, 6, 2), (2, 6, 4), (2, 6, 5), (2, 7, 2), (2, 7, 4),
(2, 7, 5), (2, 8, 2), (2, 8, 4), (2, 8, 5), (3, 6, 2), (3, 6, 4), (3, 6, 5),
(3, 7, 2), (3, 7, 4), (3, 7, 5), (3, 8, 2), (3, 8, 4), (3, 8, 5)]
>>>

Categories

Resources