Python:如何在列表中查找特定元素?

时间:2017-07-26 17:42:03

标签: python list sorting beautifulsoup

我从网站上提取了以下列表,我可以按价格和项目对列表进行排序:

['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 “]

...

现在我需要找出那些元素,其中包括'Needed'和最便宜的元素。所以对于这个例子,我需要的输出是:

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

我真的坚持这一点,所以任何有关如何解决这个问题的想法都会非常感激。

2 个答案:

答案 0 :(得分:2)

如果您可以将列表构建为列表列表:

l = [
        [['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 ']]
    ]

for item in l:
    try:
        if 'Needed' in item[0][2]:
            print item[0]
    except IndexError:
        pass

输出:

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

答案 1 :(得分:1)

您可以使用更少的代码行来完成此操作,具体取决于您的Python编程级别和所需的可读性级别。我把它分解为逐行步骤。

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

输出:

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']]