Python 3: How to recover key names after a Sorted

Asked

Viewed 62 times

3

Hello someone can help me recover values from that dictionary list?

sellers = [
            {"name": "Joaquina", "store": 2, "value": 1200.00},
            {"name": "Pedro", "store": 2, "value": 120.00},
            {"name": "Maria", "store": 1, "value": 450.00},
            {"name": "Fernanda", "store": 1, "value": 4000.00},
            {"name": "Patricia", "store": 1, "value": 100.00},

        ]

When I do this operation on Pythontutor

def sales_goals(self, sellers):
        abaixo_da_meta = sorted([vendedor["value"] for vendedor in sellers if vendedor["value"] < 500])
        vendedores_falta_meta = [vendedor["name"] for vendedor in sellers if vendedor["value"] == abaixo_da_meta]
        return vendedores_falta_meta

The seller variable_falta_meta does not retrieve the names of the variable lower_da_meta but an empty list, thanks!

  • Why did you use the sorted? Need the rated values?

  • Yes I need it in ascending order in this case and there is another case very similar but I need of unbelievable order

2 answers

3


You don’t need to create a list with just the values of value to sort them in ascending order and then get them again. You can always filter and sort keeping your original dictionary and sort them using the parameter key.

from operator import itemgetter

sellers = [
  {"name": "Joaquina", "store": 2, "value": 1200.00},
  {"name": "Pedro", "store": 2, "value": 120.00},
  {"name": "Maria", "store": 1, "value": 450.00},
  {"name": "Fernanda", "store": 1, "value": 4000.00},
  {"name": "Patricia", "store": 1, "value": 100.00}
]

abaixo_da_meta = filter(lambda seller: seller['value'] < 500, sellers)
vendedores_falta_meta = sorted(abaixo_da_meta, key=itemgetter('value'))

print(vendedores_falta_meta)

The exit will be:

[
  {'name': 'Patricia', 'store': 1, 'value': 100.0}, 
  {'name': 'Pedro', 'store': 2, 'value': 120.0}, 
  {'name': 'Maria', 'store': 1, 'value': 450.0}
]

Which are the below-target sellers ranked in ascending order by the value of value.

  • very good, I didn’t know you could do so

  • Anderson How do I get out like this? [Patricia, Pedro, Maria]

  • @Margotpaongarcia Can do vendedor['name'] for vendedor in vendedores_falta_meta, or map(itemgetter('name'), vendedores_falta_meta)

  • Thanks for the tip!

  • 1

    I make an invitation here to explain better what is the itemgetter, and how could be used a lambda in place.

0

abaixo_da_meta is a list, you have to go through it to be able to compare the values correctly.

sellers = [
            {"name": "Joaquina", "store": 2, "value": 1200.00},
            {"name": "Pedro", "store": 2, "value": 120.00},
            {"name": "Maria", "store": 1, "value": 450.00},
            {"name": "Fernanda", "store": 1, "value": 4000.00},
            {"name": "Patricia", "store": 1, "value": 100.00},

        ]

def sales_goals(sellers):
    abaixo_da_meta = sorted([vendedor["value"] for vendedor in sellers if vendedor["value"] < 500])
    vendedores_falta_meta = []
    for m in abaixo_da_meta:
        for vendedor in sellers:
            if(m == vendedor["value"]):
                vendedores_falta_meta.append(vendedor["name"])

    return vendedores_falta_meta
  • Hi Gabriel your code gives this exit ['Pedro', 'Maria', 'Patricia'] in Pythontutor and would need this one [Patricia, Pedro, Maria]

  • only reverse the order of the

  • That way? for vendedor in sellers:&#xA; for m in abaixo_da_meta:&#xA; if(m == vendedor["value"]):&#xA; vendedores_falta_meta.append(vendedor["name"])

  • I’ve already edited now this giving out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.