-5

I've been stuck for hours please help me find a way to compare the keys in the queue to dictionary and finding the minimum value.

Below is the code I have so far. I try using min() but it doesnt work.

def find_min(label,queue): 
  
  for i in queue:
    for l in label:
      for s in label[i]:
        list1 = []
        return min(l[1])

bellow is the label and queue input

  • 1
    Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – krmogi Apr 22 '21 at 01:21
  • no it doesnt I know how to sort a dictionary by value – Josue Nunez Apr 22 '21 at 01:33

1 Answers1

1

You can use the following.

Code

def find_min(labels, queue):
    # Sort labels dictionary  based upon last item in values list 
    # which will be a number (kv[1] is value list, kv[1][-1] is last value in list)
    sorted_labels = dict(sorted(labels.items(), key = lambda kv: kv[1][-1]))
    
    #  Get the keys in order from sorted_labels that are also in the queue
    options = [k for k in sorted_labels if k in queue]
    
    # Return the first one (will be the smallest)
    return options[0] if options else None

Test

print(find_min({"A" : [0], "B" : ["A",10], "C" : ["B",10], "D" : ["C",15]}, ["A", "D"]))  
# Output: A

print(find_min({"A" : [0], "B" : ["A",10], "C" : ["B",10], "D" : ["C",15]}, ["B", "C", "D"]))  
# Output: B

Simpler Alternative

def find_min(labels, queue):
    # Initialize min value
    k_min, v_min = None, None

    for k, v in labels.items():
        if k in queue:
            # Only check keys in queue
            if v_min is None or v[-1] < v_min:
                # Don't have a min yet, or less than current min
                v_min = v[-1]
                k_min = k
    return k_min

Test

print(find_min({"A" : [0], "B" : ["A",10], "C" : ["B",10], "D" : ["C",15]}, ["A", "D"]))  
# Output: A

print(find_min({"A" : [0], "B" : ["A",10], "C" : ["B",10], "D" : ["C",15]}, ["B", "C", "D"]))  
# Output: B

Using Poster's Code

def find_min(label,queue): 
    kmin = None
    value_min = None
    for l in label:
      if l in queue:
        value = label[l]
        n = len(value)
        last_value = value[n-1]
        if kmin is None or last_value < value_min:
            kmin = l
            value_min = last_value
    return kmin
      
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • thank you, the way you did it with lambda is too advance for me right now. Is there any way to do it in the way I was doing it above? – Josue Nunez Apr 22 '21 at 02:17
  • @JosueNunez--Hi Josue. When you mentioned "I know how to sort a dictionary by value", I thought this meant you where familiar with using lambda expressions for the sort key (i.e. customary method). Would it help if I used a regular function for the key or is a key function in general out? – DarrylG Apr 22 '21 at 02:29
  • @JosueNunez--never mind--thought of a simpler alternative. – DarrylG Apr 22 '21 at 02:34
  • simpler alternative would be awesome – Josue Nunez Apr 22 '21 at 02:36
  • @JosueNunez--added an alternative. Does it make sense? – DarrylG Apr 22 '21 at 02:37
  • what is the v[-1] for/ signify – Josue Nunez Apr 22 '21 at 02:54
  • @JosueNunez--in general it is the last item in a list. In your case the list is either 1 or 2 elements, but in either case we want the last element. `v[-1]` thus provides an easy way of denoting the last element without worrying about the length. Otherwise you would have to have v[1] when the list is one element or v[2] when the list has two elements. Checkout [last element in list](https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list) – DarrylG Apr 22 '21 at 03:00
  • that makes perfect sense thank you – Josue Nunez Apr 22 '21 at 03:18
  • @JosueNunez--added another solution that attempts to use your posted code (i.e. minimal modification). – DarrylG Apr 22 '21 at 03:22
  • I get this error : if l in queue ^ SyntaxError: invalid syntax also when I fit it by putting a colon after queue the result is -> 15 15 not "A" "B" – Josue Nunez Apr 22 '21 at 03:27
  • @JosueNunez--fixed. Sloppy error on my part. Must be time to sleep. – DarrylG Apr 22 '21 at 03:35