简体   繁体   中英

How to create a new list from other two lists, apply a function and append the output to each list?

thank you in advance for the help.

I need to retrieve some data (clients and products) from a WebService. This code gets the data and transforms it into lists with dictionaries within them.

    consumidores = requests.get(url + 'all_consumers', headers=custom_header)  # lista
    con = consumidores.json()
    productos = requests.get(url + 'all_products', headers=custom_header)  # lista
    prod = productos.json()

    c = []

    for key in con:
        c = [key['genero'], key['complexion'], key['tallaCamisa'], key['tallaPantalon'], key['edad'], key['ubicacion'],
         key['valorComercial'], key['valorCompra']]

    p = []
    for index in prod:
        p = [index['genero'], index['precio']]

Whats I need to do is to create two lists, one for costumers and one for products. Choose some specific elements for each costumer and product and create a new list that have to look like this

new_list = [[costumer_1, costumer_element1, costumer_element2 , ... , product_1, product_element1, product_element2, ...], [costumer_1, costumer_element1, ..., product_1, product_element1, ...], [costumer_2, costumer_elementn, ... product_1, product_element1 ,...] , ...]

Then apply a function that will relate costumers with products and append the result to the list that produced that output:

results = [[costumer_1, costumer_element1, costumer_element2 , ... , product_1, product_element1, product_element2, RESULT], etc]

    for key in con:
    index=0
    param_relcp = c[index][key['genero'],key['edad']]
    index=index + 1

This returns an error: IndexError: list index out of range and using this

c = []
for key in con:
    c = [key['genero'], key['complexion'], key['tallaCamisa'], key['tallaPantalon'], key['edad'], key['ubicacion'],
         key['valorComercial'], key['valorCompra'], key['id']]

Only takes the elements only from the first one of the list of lists. Any help will be appreciated.

Your loops all appear to do nothing, eg

for key in con:
    c = [key['genero'], key['complexion'], key['tallaCamisa'], key['tallaPantalon'], key['edad'], key['ubicacion'],
         key['valorComercial'], key['valorCompra']]

replaces c every time, and you only get the last value from the last time through the loop. You do the same for p and the same for param_relcp .

Either:

c = []
for key in con:
    c.append([key['genero'], key['complexion'], key['tallaCamisa'], key['tallaPantalon'], key['edad'], key['ubicacion'],
             key['valorComercial'], key['valorCompra']])

or

c = [[key['genero'], key['complexion'], key['tallaCamisa'], key['tallaPantalon'], key['edad'], key['ubicacion'],
             key['valorComercial'], key['valorCompra'] for key in con]

and the same for the others.

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