I have two related questions.
First, i have a some information such as:
(732, 2378) (233, 52172) 23 ['Mona'] 7
There are two tuples, two integers and one list. Let us index the five items as x[0] : x[4]
My first question is what kind of data structure can I use to store these different items (sorry, I’m new to python).
The second is question is based on the first. Suppose I have such a structure (let’s call it a list for now), my main objective then is to do the following:
Say in the first and second ‘list’ respective, I have:
x[0][1] x[1][1] x[3]
V V V
(732, 2378) (233, 23) 23 ['jamie'] 7
(732, 2378) (233, 43) 24 ['jamie'] 3
(732, 2378) (233, 56) 23 ['jamie'] 2
(3434, 2378) (45, 23) 23 ['hello'] 1
(3445, 2378) (76, 43) 23 ['hello'] 2
(7834, 2378) (90, 56) 24 ['hello'] 5
My objective is to traverse these two structures and match first on x[1][1] and x[2], then, for each matching record, find the corresponding x[4] that minimizes the sum of the values in all the matched items from both lists. Return the matched items from both structures.
In this case, it should return:
(3445, 2378) (76, 43) 23 [‘hello’] 2
AND (732, 2378) (233, 43) 24 [‘jamie’] 3
Since the sum for this match, 5, is the minimum.
I know it’s a bit involved but I’d appreciate any pointers in the right direction. Thanks!
It looks like the data structures in your question are already reasonable — just put them in list form. If you use named tuples, the search code will tend to be more readable than otherwise. For the search/match code look at operator.attrgetter and operator.itemgetter.
For example, here is a convenient form to arrange the data (and depending on what the data actually represents, you can choose better names):
List comprehensions can be useful for the matching/filtering step:
It isn’t clear to me what you want to do for the step to minimize the sums, but hopefully there is enough in this answer to get you closer to the desired result 🙂