0

Is there an easy implementation for when I'm taking the max of several values that have attached value? For example, I'm imagining a function that can, when given a list of values, can take the max but also record attached data because, for my program, where the max came from matters. Currently, I'm doing this manually but that's only for 3 data points, and it could potentially be many data points / origin points.

bottledcaps
  • 123
  • 2
  • Are any of the primary values repeated? Can you add a short example of your data to the question? It would also be nice if you post your code that you use to do the manual attachment. – PM 2Ring Jun 17 '18 at 03:52

1 Answers1

0

You can save your values and attached values as a key and value pair. So something like: 3 : asd 7 : sdf 25 : dfg You iterate through the first column to find the max. Then to find the corresponding attached value (dfg), just find it as the value of that key value (25).

#creating a dict
mydict = {3: "asd", 7:"sdf", 25:"dfg"}

maxKey=0
attachedValue=None

for x in mydict.items():
    key=x[0]
    value=x[1]
    if (key > maxKey):
        maxKey=key
        attachedValue=value

This should return you the max key and its corresponding value.

Reine_Ran_
  • 662
  • 7
  • 25
  • Pytbon has a versatile built-in `max` function that runs at C speed. – PM 2Ring Jun 17 '18 at 03:54
  • Thanks for the help! Dictionaries are exactly what I need, I'll use dictionaries and the built-in max function to do this – bottledcaps Jun 17 '18 at 04:15
  • @bottledcaps I'm glad you like Reine Fang's answer, but there's a faster way to do this. And you won't be able to use a `dict` if any of your primary values are repeated because a `dict` cannot have duplicate keys. – PM 2Ring Jun 17 '18 at 04:24
  • I didn't use Reine Fang's answer verbatim, I used dictionaries along with the built-in max function. The code might not be decipherable (it's for an alignment algorithm but you can look at it if you want... I'm not too worried about duplicate values but I think with some easy modification I could account for that... – bottledcaps Jun 17 '18 at 05:06
  • I tried to paste my code but it didn't keep the formatting, it's here https://paste.ubuntu.com/p/W3T7pXh88G/ – bottledcaps Jun 17 '18 at 05:06