Changing Every Instance of a tuple - python

I am trying to WRITE A FUNCTION to change the every instance in a list of
tuples. Basically i need to convert the every instance of the list from ('value', number, 'value') to Arc('value', number, 'value')
Input: [('root', 1, 'a'), ('b', 0.0, 'root'), ('b', 2, 'c'), ('a', 5, 'd'), ('b', 7, 'a')]
def Convert(t):
t1=('head', 'weight', 'tail')
t2=namedtuple('Arc', (t1))
return t2
Required Output: [Arc('root', 1, 'a'), Arc('b', 0.0, 'root'), Arc('b', 2, 'c'), Arc('a', 5, 'd'), Arc('b', 7, 'a')]

You can use list-comprehension to convert your list of tuples to list of named-tuples:
t = [ ('root', 1, 'a'), ('b', 0.0, 'root'), ('b', 2, 'c'), ('a', 5, 'd'), ('b', 7, 'a') ]
from collections import namedtuple
Arc = namedtuple('Arc', 'head weight tail')
def Convert(t):
return [Arc(*item) for item in t]
print(Convert(t))
Prints:
[Arc(head='root', weight=1, tail='a'), Arc(head='b', weight=0.0, tail='root'), Arc(head='b', weight=2, tail='c'), Arc(head='a', weight=5, tail='d'), Arc(head='b', weight=7, tail='a')]

Related

List duplicates manipulation

I am very new to python, and I am really lost right now. If anyone can help me, I will appreciate it a lot.
I have a list:
list1 = [((a, b), 2), ((a, b), 5), ((c, d), 1)), ((e, f), 2), ((e, f), 4)]
The output I am looking for is:
output = [((a, b), 7), ((c, d), 1), ((e, f), 6)]
I tried to put it in a dictionary
new_dict = {i: j for i, j in list1}
But it throws me an error
Maybe there are other ways?
Find the explanation in the code comments
list1 = [(('a', 'b'), 2), (('a', 'b'), 5), (('c', 'd'), 1), (('e', 'f'), 2), (('e', 'f'), 4)]
# let's create an empty dictionary
output = {}
# ('a', 'b') is a tuple and tuple is hashable so we can use it as dictionary key
# iterate over the list1
for i in list1:
# for each item check if i[0] exist in output
if i[0] in output:
# if yes just add i[1]
output[i[0]] += i[1]
else:
# create new key
output[i[0]] = i[1]
# finally print the dictionary
final_output = list(output.items())
print(final_output)
[(('a', 'b'), 7), (('c', 'd'), 1), (('e', 'f'), 6)]
You can use {}.get in this fashion:
list1 = [(('a', 'b'), 2), (('a', 'b'), 5), (('c', 'd'), 1), (('e', 'f'), 2), (('e', 'f'), 4)]
di={}
for t in list1:
di[t[0]]=di.get(t[0],0)+t[1]
>>> di
{('a', 'b'): 7, ('c', 'd'): 1, ('e', 'f'): 6}
You can also use a Counter:
from collections import Counter
c=Counter({t[0]:t[1] for t in list1})
>>> c
Counter({('a', 'b'): 5, ('e', 'f'): 4, ('c', 'd'): 1})
Then to turn either of those into a list of tuples (as you have) you use list and {}.items():
>>> list(c.items())
[(('a', 'b'), 5), (('c', 'd'), 1), (('e', 'f'), 4)]
list1 = [(('a', 'b'), 2), (('a', 'b'), 5), (('c', 'd'), 1), (('e', 'f'), 2), (('e', 'f'), 4)]
sorted_dict = {}
for ele in list1:
if ele[0] in sorted_dict:
sorted_dict[ele[0]] += ele[1]
else:
sorted_dict[ele[0]] = ele[1]
print(sorted_dict)

Changing dictionary format

I want to change a dictionary below ...
dict = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)] }
into other form like this:
dict = [
('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 7),
('B', 'D', 5), ('C', 'D', 12)]
This is what I done.
dict = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)] }
if(i[0] in dict):
value = dict[i[0]]
newvalue = i[1],i[2]
value.append(newvalue)
dict1[i[0]]=value
else:
newvalue = i[1],i[2]
l=[]
l.append(newvalue)
dict[i[0]]=l
print(dict)
Thanks
Python tuple is an immutable object. Hence any operation that tries to modify it (like append) is not allowed. However, following workaround can be used.
dict = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)] }
new_dict = []
for key, tuple_list in dict.items():
for tuple_item in tuple_list:
entry = list(tuple_item)
entry.append(key)
new_dict.append(tuple(entry))
print(new_dict)
Output:
[('B', 1, 'A'), ('C', 3, 'A'), ('D', 7, 'A'), ('D', 5, 'B'), ('D', 12, 'C')]
A simple aproach could be
new_dict = []
for letter1, list in dict.items():
for letter2, value in list:
new_dict.append([letter1, letter2, value])
With list comprehension;
dict_ = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)] }
result = [(key, value[0], value[1]) for key, list_ in dict_.items() for value in list_]
Output;
[('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 7), ('B', 'D', 5), ('C', 'D', 12)]
You can iterate through the dictionary using .items(). Notice that each value is by itself a list of tuples. We want to unpack each tuple, so we need a nested for-loop as shown below. res is the output list that we will populate within the loop.
res = []
for key, values in dict.items():
for value in values:
res.append((key, value[0], value[1]))
Sample output:
>>> res
[('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 7), ('B', 'D', 5), ('C', 'D', 12)]
EDIT: If value is a tuple of more than two elements, we would modify the last line as follows, using tuple unpacking:
res.append((key, *value))
This effectively unpacks all the elements of value. For example,
>>> test = (1, 2, 3)
>>> (0, *test)
(0, 1, 2, 3)

n-fold Cartesian product on a single list in Python [duplicate]

This question already has answers here:
Generating permutations with repetitions
(6 answers)
Closed 8 months ago.
How to compute the n-fold Cartesian product on a list, that is, A × ... × A (n times), in an elegant (concise) way in Python?
Examples:
>>> l = ["a", "b", "c"]
>>> cart_prod(l, 0)
[]
>>> cart_prod(l, 1)
[('a',), ('b',), ('c',)]
>>> cart_prod(l, 2)
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> cart_prod(l, 3)
[('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'a'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'a'), ('a', 'c', 'b'), ('a', 'c', 'c'),
('b', 'a', 'a'), ('b', 'a', 'b'), ('b', 'a', 'c'), ('b', 'b', 'a'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'c', 'a'), ('b', 'c', 'b'), ('b', 'c', 'c'),
('c', 'a', 'a'), ('c', 'a', 'b'), ('c', 'a', 'c'), ('c', 'b', 'a'), ('c', 'b', 'b'), ('c', 'b', 'c'), ('c', 'c', 'a'), ('c', 'c', 'b'), ('c', 'c', 'c')]
I came up with the following iterative solution:
def cart_prod(l, n):
if n == 0:
return [] # compute the result for n = 0
# preliminarily, create a list of lists instead of a list of tuples
res = [[x] for x in l] # initialize list with singleton tuples (n = 1)
for i in range(n-1):
res = [r + [x] for r in res for x in l] # concatenate each n-1 tuple with each element from a
res = [tuple(el) for el in res] # turn the list of lists into a list of tuples
return res
This code does the job, but is there a shorter, possibly one-liner definition, maybe a nested list comprehension or a lambda expression? I am interested in more compact solutions, not necessarily more readable ones.
This question is not a duplicate of Get the cartesian product of a series of lists?. I do not want the Cartesian product of a series of lists crossed with each other. I want the Cartesian product of a single list crossed n-times with itself, where n is a parameter given to the function.
itertools.product takes a keyword argument to indicate the given arguments should be repeated.
>>> from itertools import product
>>> list(product([1,2], repeat=0))
[()]
>>> list(product([1,2], repeat=1))
[(1,), (2,)]
>>> list(product([1,2], repeat=2))
[(1, 1), (1, 2), (2, 1), (2, 2)]
This works with multiple iterables as well.
# Equivalent to list(product([1,2], ['a', 'b'], [1,2], ['a', 'b']))
>>> list(product([1,2], ['a', 'b'], repeat=2))
[(1, 'a', 1, 'a'), (1, 'a', 1, 'b'), (1, 'a', 2, 'a'), (1, 'a', 2, 'b'), (1, 'b', 1, 'a'), (1, 'b', 1, 'b'), (1, 'b', 2, 'a'), (1, 'b', 2, 'b'), (2, 'a', 1, 'a'), (2, 'a', 1, 'b'), (2, 'a', 2, 'a'), (2, 'a', 2, 'b'), (2, 'b', 1, 'a'), (2, 'b', 1, 'b'), (2, 'b', 2, 'a'), (2, 'b', 2, 'b')]

creating tuple with repeating elements

I am trying to create tuple of following kind:
('a', 0), ('b', 0), ('a', 1), ('b', 1), ('a', 2), ('b', 2), ('a', 3), ('b', 3)
from arrays:
A = ['a','b'] and numbers 0 through 3.
What is good pythonic representation as I am ending with a real for loop here.
Use itertools.product.
from itertools import product
tuples = list(product(['a', 'b'], [0, 1, 2, 3]))
print(tuples) # [('a', 0), ('a', 1), ..., ('b', 0), ('b', 1), ...]
If you need them in the exact order you originally specified, then:
tuples = [(let, n) for n, let in product([0, 1, 2, 3], ['a', 'b'])]
If your comment that "I am ending with a real for loop here" means you ultimately just want to iterate over these elements, then:
for n, let in product([0, 1, 2, 3], ['a', 'b']):
tup = (let, n) # possibly unnecessary, depending on what you're doing
''' your code here '''
You could opt for itertools.product to get the Cartesian product you're looking for. If the element order isn't of significance, then we have
>>> from itertools import product
>>> list(product(A, range(4)))
[('a', 0),
('a', 1),
('a', 2),
('a', 3),
('b', 0),
('b', 1),
('b', 2),
('b', 3)]
If you need that particular order,
>>> list(tuple(reversed(x)) for x in product(range(4), A))
[('a', 0),
('b', 0),
('a', 1),
('b', 1),
('a', 2),
('b', 2),
('a', 3),
('b', 3)]
L = range(0, 4)
K = ['a', 'b']
L3 = [(i, j) for i in K for j in L]
print(L3)
OUTPUT
[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2), ('b', 3)]
If you wish to use list comprehension... other answers are correct as well
Use list comprehension
>>> [(a,n) for a in list1 for n in range(4)]
[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2), ('b', 3)]
If order matters:
>>> [(a,n) for n in range(4) for a in list1]
[('a', 0), ('b', 0), ('a', 1), ('b', 1), ('a', 2), ('b', 2), ('a', 3), ('b', 3)]

Python: Generating a "set of tuples" from a "list of tuples" that doesn't take order into consideration

If I have the list of tuples as the following:
[('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
I would like to remove duplicate tuples (duplicate in terms of both content and order of items inside) so that the output would be:
[('a', 'b'), ('c', 'd')]
Or
[('b', 'a'), ('c', 'd')]
I tried converting it to set then to list but the output would maintain both ('b', 'a') and ('a', 'b') in the resulting set!
Try this :
a = [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
b = list(set([ tuple(sorted(t)) for t in a ]))
[('a', 'b'), ('c', 'd')]
Let's break this down :
If you sort a tuple, it becomes a sorted list.
>>> t = ('b', 'a')
>>> sorted(t)
['a', 'b']
For each tuple t in a, sort it and convert it back to a tuple.
>>> b = [ tuple(sorted(t)) for t in a ]
>>> b
[('a', 'b'), ('c', 'd'), ('a', 'b'), ('a', 'b')]
Convert the resulting list b to a set : values are now unique. Convert it back to a list.
>>> list(set(b))
[('a', 'b'), ('c', 'd')]
Et voilà !
Note that you can skip the creation of the intermediate list b by using a generator instead of a list comprehension.
>>> list(set(tuple(sorted(t)) for t in a))
[('a', 'b'), ('c', 'd')]
If you did not mind using a frozenset with a set:
l = [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
print(set(map(frozenset,l)))
{frozenset({'a', 'b'}), frozenset({'c', 'd'})}
You can convert back to tuple if preferable:
l = [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
print(list(map(tuple,set(map(frozenset ,l)))))
[('a', 'b'), ('d', 'c')]
Or using a set and reversing the order of the tuples:
l = [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
seen, pairs = set(), []
for a,b in l:
if (a,b) not in seen and (b,a) not in seen:
pairs.append((a,b))
seen.add((a,b))
This can solve your problem if order is not important.
a=[('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
a=map(tuple,[sorted(i) for i in a])
print list(set(a))
Output:
[('a', 'b'), ('c', 'd')]
Just wanted to add a potential second solution if anyone has a use case where "first come, first serve" might matter.
For example, say we take three lists and merge them into a list of tuples:
# Make some lists (must be same size)
a = [1,1,1,2,8,6,1]
b = [2,4,6,1,4,21,69]
c = [2,8,21,2,1,1,8]
# Lists to list of tuples
arr = []
for i in range(len(a)):
new_row = (a[i],b[i],c[i])
arr.append(new_row)
Such that our original array looks like:
(1, 2, 2)
(1, 4, 8)
(1, 6, 21)
(2, 1, 2)
(8, 4, 1)
(6, 21, 1)
(1, 69, 8)
In our case, we want to remove items like (2,1,2) and (8,4,1) as they're equivalent to (1,2,2) and (1,4,8) respectively.
To do this, we can use a new empty list called filtered or something, and itertools.permutations() on each tuple in the original array.
First, we check if any permutation of each item is present in the filtered list.
If not, we add. If it is, we skip the duplicate.
filtered = []
for i in range(len(arr)):
it = itertools.permutations(arr[i])
perms = []
for p in it:
perms.append(p)
check = any(item in perms for item in filtered)
if not check:
filtered.append(arr[i])
Now if we iterate over filtered and print, we see our truncated list of tuples:
(1, 2, 2)
(1, 4, 8)
(1, 6, 21)
(1, 69, 8)
Note that we're left with the first instance of each tuple of numbers, and not working via a set guarantees the same order of elements when iterating over the filtered list.
Only thing I'm not 100% on is the time/space complexity of doing it this way -- if anyone has feedback I'd love to hear about it.
Built-in types to the rescue:
data = [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')]
set(map(frozenset, data))
{frozenset({'a', 'b'}), frozenset({'c', 'd'})}

Categories

Resources