Kimsbrand Kimsbrand - 1 year ago 58
Python Question

Python: How to find out specific elements in a list with groups?

I have extracted the following list from a website and I was able to sort the list by price and item:

['ITEM 1', '155.80EUR ', 'Needed'], ['ITEM 1', '164.00EUR '], ['ITEM 1', '165.30EUR '], ['ITEM 1', '170.05EUR '], ['ITEM 1', '174.00EUR '], ['ITEM 1', '179.00EUR '], ['ITEM 1', '179.00EUR '],

['ITEM 2', '122.55EUR '], ['ITEM 2', '129.00EUR ', 'Needed'], ['ITEM 2', '132.05EUR '], ['ITEM 2', '136.80EUR '], ['ITEM 2', '139.00EUR '], ['ITEM 2', '144.00EUR '], ['ITEM 2', '144.00EUR '], ['ITEM 2', '144.00EUR '], ['ITEM 2', '154.00EUR '], ['ITEM 2', '154.00EUR '],

['ITEM 3', '246.05EUR ', 'Needed'], ['ITEM 3', '249.00EUR '], ['ITEM 3', '250.80EUR '], ['ITEM 3', '259.00EUR '], ['ITEM 3', '264.00EUR '], ['ITEM 3', '264.00EUR '], ['ITEM 3', '264.00EUR '], ['ITEM 3', '274.00EUR ']

...

Now I need to find out those elements, which include the 'Needed' AND are the cheapest ones. So for this example my needed output would be:

['ITEM 1', '155.80EUR ', 'Needed'], ['ITEM 3', '246.05EUR ', 'Needed'],

I really stuck on this, so any ideas on how to solve this problem would be much appreciated.

Answer Source

You can do this with fewer lines of code, depending on your level of Python programming and the readability level you want. I've left it broken down to line-by-line steps.

order_list = [
    ['ITEM 1', '155.80EUR ', 'Needed'], ['ITEM 1', '164.00EUR '], ['ITEM 1', '165.30EUR '],
    ['ITEM 1', '170.05EUR '], ['ITEM 1', '174.00EUR '], ['ITEM 1', '179.00EUR '], 
    ['ITEM 1', '179.00EUR '],

    ['ITEM 2', '122.55EUR '], ['ITEM 2', '129.00EUR ', 'Needed'], ['ITEM 2', '132.05EUR '], 
    ['ITEM 2', '136.80EUR '], ['ITEM 2', '139.00EUR '], ['ITEM 2', '144.00EUR '], 
    ['ITEM 2', '144.00EUR '], ['ITEM 2', '144.00EUR '], ['ITEM 2', '154.00EUR '], 
    ['ITEM 2', '154.00EUR '],

    ['ITEM 3', '246.05EUR ', 'Needed'], ['ITEM 3', '249.00EUR '], ['ITEM 3', '250.80EUR '], 
    ['ITEM 3', '259.00EUR '], ['ITEM 3', '264.00EUR '], ['ITEM 3', '264.00EUR '], 
    ['ITEM 3', '264.00EUR '], ['ITEM 3', '274.00EUR ']
]

# Collect items only in a list
item_list = [line[0] for line in order_list]
# Collect only distinct items
product_list = set(item_list)
print "item list:", item_list
print "product list:", product_list

best_order = []

# Find the first (cheapest) occurence of each item;
#   See whether it's needed
for product in product_list:
    pos = item_list.index(product)
    line_item = order_list[pos]
    if 'Needed' in line_item:
        best_order.append(line_item)

print best_order

Output:

item list: ['ITEM 1', 'ITEM 1', 'ITEM 1', 'ITEM 1', 'ITEM 1', 'ITEM 1', 'ITEM 1', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 2', 'ITEM 3', 'ITEM 3', 'ITEM 3', 'ITEM 3', 'ITEM 3', 'ITEM 3', 'ITEM 3', 'ITEM 3']
product list: set(['ITEM 3', 'ITEM 2', 'ITEM 1'])
[['ITEM 3', '246.05EUR ', 'Needed'], ['ITEM 1', '155.80EUR ', 'Needed']]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download