Hướng dẫn subtract tuples python
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Sometimes, while working with records, we might have a common problem of subtracting the contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let’s discuss certain ways in which this task can be performed. Method #1: Using map() + lambda Combination of the above functionalities can solve the problem for us. In this, we compute the subtraction using lambda functions and extend the logic to keys using map(). Python3
Output : The original tuple 1 : (10, 4, 5) The original tuple 2 : (2, 5, 18) Resultant tuple after subtraction : (8, -1, -13) Method #2: Using map() + sub() The combination of above functions can help us achieve this task. In this, we first extend the logic to all using map() and then perform subtraction of each index using sub(). Python3
Output : The original tuple 1 : (10, 4, 5) The original tuple 2 : (2, 5, 18) Resultant tuple after subtraction : (8, -1, -13) Method #3 : Using for loop and tuple() method Python3
Output The original tuple 1 : (10, 4, 5) The original tuple 2 : (2, 5, 18) Resultant tuple after subtraction : (8, -1, -13) I have 2 sets of tuples with strings inside.
I want to subtract tuple_1 from tuple_2 and get the output such that answer will contain the tuples which does not contain in tuple_2. answer for the above one should be
Thank you in advance. |