Why doesn't addition broadcast over tuples? [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
E.g., why can't I do:
(0,1) + (2,2)
and get:
(2,3)
as a result?

Because the + operator is used to make a new tuple that is the combination of two other tuples. That is just how Python was designed.
To do what you want, you can use zip and a generator expression:
>>> t1 = (0,1)
>>> t2 = (2,2)
>>> tuple(x + y for x, y in zip(t1, t2))
(2, 3)
>>>

'+' operator concatenate tuples . if you want to sum tuples item you can use:
tuple(sum(x) for x in zip((0,1),(2,2)))
or
tuple(map(sum,zip((0,1),(2,2))))

Its because of you add tow tuple and + operation for tuples concatenate them ! you can use map and zip functions for that :
>>> map(sum,zip((0,1),(2,2)))
[2, 3]
or use a generator :
>>> tuple(i+j for i,j in zip((0,1),(2,2)))
(2, 3)
and a better way with operator.add:
>>> from operator import add
>>> map(add,(0,1),(2,2))
[2, 3]

Since Python thinks of tuples as lists that are immutable, adding two tuples is just like adding two lists. So, just as adding two lists will concatenate them:
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
adding two tuples will also concatenate them:
>>> (1, 2) + (3, 4)
(1, 2, 3, 4)
You can create a new tuple that consists of the sum of each pair with a few of Python's built-in functions:
>>> tuple(map(sum, zip((0, 1), (2, 2))))
(2, 3)
This works by using zip() on the two tuples to make a list of pairs:
>>> zip((0, 1), (2, 2))
[(0, 2), (1, 2)]
And using map() to apply sum() on each element of that list, to get a list of sums:
>>> map(sum, zip((0, 1), (2, 2)))
[2, 3]
And, finally, using tuple() to turn that from a list into a tuple.

Element-wise addition is a more specialized operation than concatenation. Fewer tuples could be added together: what would ('a','b') + (1,2,) equal?
Or ('a', 'b') + (1,2,3) for that matter?
Since concatenation is arguably the more commonly desired operation, and importantly, well defined for all tuples, it makes sense that addition of tuples performs concatenation.

Related

Subtracting a tuples of the list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I have a list of tuples
lst = [(1,2,3),(1,2,3),(7,8,9)]
And
I want to find the difference between previous tuple from the next like this:
ans = [(0,0,0), (6,6,6)]
How can I do this in Python?
You can iterate over the data and use the sub method from the operator package to subtract the tuples:
from operator import sub
data = [(1,2,3),(1,2,3),(7,8,9)]
diff = [tuple(map(sub, data[i+1], data[i])) for i in range(0, len(data)-1)]
You could also use a for loop instead of the list comprehension if you prefer. The tuple subtraction is from here: Elegant way to perform tuple arithmetic
from functools import reduce, partial
from operator import sub
my_list = [(1,2,3),(1,2,3),(7,8,9)]
diff_list = list(map(partial(reduce, lambda a, b: tuple(map(sub, a, b))), zip(my_list[1:], my_list)))
To break it down:
zip(my_list[1:], my_list)
This creates a new iterator which yields iterables consisting of every next element of the list followed by the previous.
partial(reduce, lambda a, b: tuple(map(sub, a, b)))
This creates a function which when given an iterable whose elements consist of tuples, will return a new tuple containing the difference between the first and the second tuples. It could be more tuples, but in our case, we only ever pass to this function an iterable consisting of two tuples, it will always do what we want.
Try:
lst = [(1, 2, 3), (1, 2, 3), (7, 8, 9)]
out = [tuple(b - a for a, b in zip(t1, t2)) for t1, t2 in zip(lst, lst[1:])]
print(out)
Prints:
[(0, 0, 0), (6, 6, 6)]

Finding unique list in list of list of tuples [duplicate]

This question already has answers here:
Get unique items from list of lists? [duplicate]
(3 answers)
Closed 2 years ago.
I have a list of lists of tuples:
x = [[(0,0),(0,1)],[(1,2),(2,3)],[(0,0),(0,1)],[(1,2),(2,3)]]
I want all the unique lists present in the list x
My output should be:
x=[[(0,0),(0,1)],[(1,2),(2,3)]]
I tried using x=list(set(x)) but it gives error: 'list is not hashable', I also tried using numpy.unique but it does not give desired output. How can I implement this?
list as mutable and hence can not be used as an element to a set. However, you can type-cast the list to tuple which are immutable, and then you can get unique elements using set. Here I am using map() to covert all sub-lists to tuple:
>>> x = [[(0,0),(0,1)],[(1,2),(2,3)],[(0,0),(0,1)],[(1,2),(2,3)]]
>>> set(map(tuple, x))
{((1, 2), (2, 3)), ((0, 0), (0, 1))}
To type-cast the set back to list and change back nested tuple to list, you can further use map() as:
>>> list(map(list, set(map(tuple, x))))
[[(1, 2), (2, 3)], [(0, 0), (0, 1)]]
i would do something like this:
x = [[(0,0),(0,1)],[(1,2),(2,3)],[(0,0),(0,1)],[(1,2),(2,3)]]
result = []
for i in x:
if not i in result:
result.append(i)
print(result)
maybe it is not the fastest way but certainly it is the simpler.
Otherwise you can use the most "cool" way, you can use sets. Sets are like lists that don't allow equal elements.
x = x = [[(0,0),(0,1)],[(1,2),(2,3)],[(0,0),(0,1)],[(1,2),(2,3)]]
result = list(map(list,set(map(tuple,x))))

How to determine if a tuple is a slice of another tuple (not only a subset!) [duplicate]

This question already has answers here:
elegant find sub-list in list
(7 answers)
Closed 2 years ago.
Is there a Pythonic way of determining if a tuple is a slice of another tuple (and not only a subset)?
For example, if I have the following tuples:
t1 = (2, 3, 4)
t2 = (1, 2, 3, 4, 5)
t3 = (3, 4, 2, 5)
then t1 is a slice of t2 but only a subset of t3. I'm looking for something like "t1 in t2". Window-based shifting of one tuple along another one is "implementable" but there may be a more efficient way that I don't know.
Convert both tuples to strings and then use the in operator to check if one is a substring of another.
' '.join(map(str, t1)) in ' '.join(map(str, t2))
Do note the space in the string on which join().is called. If you don't include the space, this could happen:
''.join(map(str, (1, 2, 3))) in ''.join(map(str, (1, 23, 4)))
# 123 in 1234 == True

Store more than 1 value in python array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to store more than 1 value in a python array(I am open to any other data structure too).
For example :
array[1][2][3] = 1 # (this is what I am able to do now)
But later I also get the value 2, now instead of storing it in another array using the same indices, I want to be able to do this :
array[1][2][3] = 1,2
But I dont want to concatenate the existing result like in a string, and split the string to get the individual values.
Is there a way of doing this without having to introduce another dimension to the array?
edit : I want a neater way to store 2 values in the same cell.
Thanks
I would use defaultdict for this:
from collections import defaultdict
array = defaultdict(list)
array[(1,2,3)].append(1)
array[(1,2,3)].append(2)
Now array at position (1,2,3) is a list containing 1 and 2
If understand correctly, you can store new list with values you need in array[1][2][3] instead of single value:
array[1][2][3] = [1] # instead of = 1
array[1][2][3].append(2) # now it's [1, 2]
array[1][2][3].extend([3,4]) # now it's [1, 2, 3, 4]
all_values = array[1][2][3] # no another dimensions uses to get values
If you know all the contents a priori you may use a tuple. Here I will create pairs of numbers (n, n+1) and store them in a list:
>>> l = [(n,n+1) for n in xrange(0,4)]
>>> l
[(0, 1), (1, 2), (2, 3), (3, 4)]
If you decide you want to append another value, you can. Assume you want to append 3 to the tuple (1,2):
>>> l[1] = l[1] + (3,)
>>> l
[(0, 1), (1, 2, 3), (2, 3), (3, 4)]
Tuples are immutable, so they're faster than lists for construction, but you can't make changes to an instantiated tuple - you must replace it. If you're fairly certain your data won't change, use a tuple. If it will (and often), use a list.
Edit
If you want the values from the tuple, you can do the following:
>>> tup = l[0]
>>> tup
(0, 1)
>>> x, y = tup
>>> x
0
>>> y
1
>>> t, u = l[2] # alternatively
>>> t
2
>>> u
3
array[1][2][3] = 1,2
Seems like you want to have a list in array[1][2][3], which you could actually do with
array[1][2][3] = [1,2]
or alternatively, as already mentionned by others with variations of
array[1][2][3] = []
array[1][2][3].append(1)
array[1][2][3].extend([2])
Mind the fact that use of t-uples is also possible
array[1][2][3] = (1,2)
But in this case, you won't be able to extend your "list" nor to modify its content (t-uples are indeed constant arrays, in size as well as in content.).
Is there a way of doing this without having to introduce another
dimension to the array?
Well... this requirement seems rather antagonistic with your previous one.
array[1][2][3] = 1,2
suggests that your are able to differentiate between "array[1][2][3][0]" (=1) and "array[1][2][3][1]" (=2). So, unless I don't understand your question, the short answer is "no" :)

add a tuple to a tuple in Python

I have a tuple:
a = (1,2,3)
and I need to add a tuple at the end
b = (4,5)
The result should be:
(1,2,3,(4,5))
Even if I wrap b in extra parents:
a + (b), I get (1,2,3,4,5) which is not what I wanted.
When you do a + b you are simply concatenating both the tuples. Here, you want the entire tuple to be a part of another tuple. So, we wrap that inside another tuple.
a, b = (1, 2, 3), (4,5)
print a + (b,) # (1, 2, 3, (4, 5))
>>> a = (1,2,3)
>>> b = (4,5)
>>> a + (b,)
(1, 2, 3, (4, 5))
tuple objects are immutable. The result you're getting is a result of the fact that the + (and +=) operator is overridden to allow "extending" tuples the same way as lists. So when you add two tuples, Python assumes that you want to concatenate their contents.
To add an entire tuple onto the end of another tuple, wrap the tuple to be added inside another tuple.
c = a + (b,) # Add a 1-tuple containing the tuple to be added.
print(c) # >>> (1, 2, 3, (4, 5))

Categories

Resources