0

I had two tuple of tuples ("one" and "two") and I need to join them. If the subelements of the first tuple(for example, one[0]) in the positions 1, 2, 3, 4 and 5 are equal to the first five elements in the tuple two (for example if one[0][0] == two[0][0] and one[0][1] == two[0][1] ... and one[0][5] == two[0][5]), then they need to be combined:

one = (
(25, "sample", "x", "y", "z", 200, 50, 45),
)
two = (
(25, "other", "p", "q", "r", 0),
)
result = (
(25, "sample", "x", "y", "z", 200, 50, 0, 45),
)

For example, with this two tuples

one = (
(25, "sample", "x", "y", "z", 200, 50, 45),
(25, "other", "p", "q", "r", 100, 25, 0),
(27, "sample", "x", "y", "z", 200, 50, 45),
(28, "other", "p", "q", "r", 100, 25, 0),
)

two = (
(1, "sample", "x", "y", "z", 45),
(25, "other", "p", "q", "r", 0),
(27, "sample", "x", "y", "z", 20),
(28, "other", "p", "q", "r", 1),
)

The result needs to be something like this (it could be a list of list, a list of dicts or just a tuple of tuples:

result = (
(1, "sample", "x", "y", "z", 0, 0, 45, 0),
(25, "sample", "x", "y", "z", 200, 50, 0, 45),
(25, "other", "p", "q", "r", 100, 25, 0, 0),
(27, "sample", "x", "y", "z", 200, 50, 20, 45),
(28, "other", "p", "q", "r", 100, 25, 1, 0),
)

I don't know if the best way is to change this tuples for lists or maybe iterate each tuples and add each element as a list in a new one.

eduzen
  • 131
  • 6
  • Can you clarify your sentence: **But If the first five elements are equal need to be combined, If not only add the tuple**. – Tanmay Jul 09 '16 at 20:05
  • Can you tell me if it is more clear now? – eduzen Jul 09 '16 at 20:39
  • Your expected results don't really match up with your specifications. For example, how does the first element of the results get there? `(1, "sample", "x", "y", "z", 0, 0, 45, 0)` – juanpa.arrivillaga Jul 09 '16 at 23:01
  • One can't tell from the question or smaple data how the remaining elements are to be combined. – jsbueno Jul 12 '16 at 16:50
  • Although once that is clarified, the problem is easy to answer: just take the irst 5 elements of each tuple as keys to a dictionary. – jsbueno Jul 12 '16 at 16:52

0 Answers0