In Python, how can I search a list of elements for a value then also take the element before it? -


i have list of elements created by:

if x > 2:             priceperunit.append(name)             priceperunit.append(price/quantity) bestvalue = list.count(min(priceperunit)) 

how take element preceding each value @ same time take value?

the best thing structure priceperunit list differently, storing both values tuple:

if x > 2:     priceperunit.append((name, price / quantity)) 

this way, when find best value item, find name. find minimum value in list, need supply key argument min. key function should extract value compare find minimum, in case 1st item in tuple (name 0th item in tuple):

bestvaluename, bestvalue = min(priceperunit, key=lambda ppu: ppu[1]) 

to find of items have best value, should find best value, , find items have value:

_, bestvalue = min(priceperunit, key=lambda ppu: ppu[1]) bestvalueitems = filter(lambda ppu: ppu[1] == bestvalue, priceperunit) 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -