简体   繁体   中英

dictionary to list of tuples with smallest value

Ok so I am trying to write a function that takes a dictionary as a parameter and returns a list of tuples with the first item as the keys and the second values as the lowest value item. Also if there is a values for the dictionary that is empty then the tuple should have a False in that place. example of how it should work:

>>> storeDict = {"socks" : [6.99, 4.50, 5.00, 4.99], "shoes" : [14.99,19.99], 
"napkin" : [],"shirt" : [8.99, 7.00, 6.50, 10.25, 8.99]}
>>> LowestPrice(storeDict)
[('shoes', 14.99), ('napkin', False), ('shirt', 6.5), ('socks', 4.5)]

so what I am trying to do is turn the dictionary into a list and them from there get the minimum value, but I don't know how to do this because I get a list of list for all the values. I also don't know how to the convert that into tuples. Please help me!! Here is the code I have:

def LowestPrice(storeDict):
valueslist =[]
temp = []
for value in storeDict.values():
    temp=[value]
    valueslist.append(temp)

    for x in valueslist:
        if type(x) == list:
            valueslist.append(min(x))
            minimum = min(values list)

with this that I have so far I know I would have to use the .items() to turn a dictionary into a list but how do it turn this list back into a dictionary and the into tuples or what is the best way to do this?

You say -

I don't know how to do this because I get a list of list for all the values

This is because you are enclosing the value list inside another list before adding it to valueslist . So it is a list of list. This is happening in -

temp=[value]
valueslist.append(temp)

A Simple way to do this , through list comphrension and dict.items()` is -

def lowest_price(store_dict):
    return [(k,min(v)) if len(v) > 0 else (k,False) for k,v in store_dict.items()]

Explanation -

  1. dict.items() returns the key/value pairs of the dictionary as list of tuple (or in Python 3.x as a view ) , so we are first iterating over it in the for loop .

  2. min(v) returns the minimum value from the list , it is a built-in function in Python.

  3. In the list comprehension , we are looping through the items of the dictionary , getting the key/value each time , and in each loop , we are checking if the length of value list is greater than 0 , if so we are taking the minimum of it and (key, minimum of value) is getting added to the list otherwise we are adding (key , False) to the list.

The for loop method for above would be -

def lowest_price(store_dict):
    retlist = []
    for k,v in store_dict.items():
        if len(v) > 0:
            retlist.append((k,min(v)))
        else:
            retlist.append((k,False))
    return retlist

Demo -

>>> def lowest_price(store_dict):
...     return [(k,min(v)) if len(v) > 0 else (k,False) for k,v in store_dict.items()]
...
>>> storeDict = {"socks" : [6.99, 4.50, 5.00, 4.99], "shoes" : [14.99,19.99],
... "napkin" : [],"shirt" : [8.99, 7.00, 6.50, 10.25, 8.99]}
>>>
>>> lowest_price(storeDict)
[('napkin', False), ('shirt', 6.5), ('shoes', 14.99), ('socks', 4.5)]

Since you requested a for loop approach

storeDict = {"socks" : [6.99, 4.50, 5.00, 4.99], "shoes" : [14.99,19.99], 
"napkin" : [],"shirt" : [8.99, 7.00, 6.50, 10.25, 8.99]}

def lowestPrice(storeDict):
    valueslist =[] # init empty list
    for k,v in storeDict.iteritems(): # iterate dict
        if len(v) > 0: # if list has elements
            valueslist.append((k,min(v))) # find min element and append
        else:
            valueslist.append((k,False)) # append with value = False
    return valueslist

print lowestPrice(storeDict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM