简体   繁体   中英

Setting dictionary values while iterating through a 'for-loop'

I'm trying to create a nested dictionary with a set of values that are pulled from a for-loop, to measure growth and revenue amounts for various customer-product pairings. However, when I loop through a dataframe to set elements of the dictionary, each dictionary element ends up with the same values. What's going on, here?

I have already tried changing various elements of how the lists are built, but to no avail.

'''
TP_Name = customer name

Service_Level_1 = service name

100.2014 is just a marker to show that someone has started consuming the service

tpdict is already created with necessary nesting below with empty values at each endpoint
'''

for col in pivotdf.columns:
  growthlist = []
  amountlist = []
  first = True
  TP_Name, Service_Level_1 = col.split('___')
  for row in pivotdf[col]:
    if first == True:
      past = row+.00001
      first = False
    if row == 0 and past <.0001 :
      growth = 0
    elif row != 0 and past == .00001:
      growth = 100.2014
    else:
      current = row
      growth = (current-past)/past
    growth = round(growth,4)
    growthlist.append(growth)
    past = row +.00001
    amountlist.append(row)
  tpdict[TP_Name][Service_Level_1]['growth'] = growthlist
  tpdict[TP_Name][Service_Level_1]['amount'] = amountlist

'''
problem: Each value ends up being the same thing
'''

Expected results:

{'CUSTOMER NAME': {'PRODUCT1': {'growth': [unique_growthlist],   'amount': [unique_amountlist]},  'PRODUCT2': {'growth': [unique_growthlist],'amount': [unique_amountlist]}}}

A dictionary is a key value pair (as I am sure you may know). If you ever try to write to a dictionary with a key that already exists in the dictionary then the dictionary will overwrite the value for that key.

Example:

d = dict()
d[1] = 'a' # d = {1: 'a'}
d[1] = 'b' # d = {1: 'b'}

Your project seems like it may be a good use of a namedtuple in python. A namedtuple is basically a light weight class/object. My example code may be wrong because I don't know how your for loop is working (commenting helps everyone). That being said here is an example.

I only make this recommendation as dictionaries consume ~33% more memory then the objects they hold (though they are much faster).

from collections import namedtuple

Customer = namedtuple('Customer', 'name products')
Product = namedtuple('Product', 'growth amount')

customers = []
for col in pivotdf.columns:
    products = []
    growthlist = []
    amountlist = []
    first = True
    TP_Name, Service_Level_1 = col.split('___')
    for row in pivotdf[col]:
        if first == True:
            past = row + .00001
            first = False
        if row == 0 and past < .0001 :
            growth = 0
        elif row != 0 and past == .00001:
            growth = 100.2014
        else:
            current = row
            growth = (current - past) / past
        growth = round(growth, 4)
        growthlist.append(growth)
        past = row + .00001
        amountlist.append(row)

    cur_product = Product(growth=growthlist, amount=amountlist) # Create a new product
    products.append(cur_product) # Add that product to our customer

# Create a new customer with our products
cur_customer = Customer(name=TP_Name, products=products)
customers.append(cur_customer) # Add our customer to our list of customers

Here customers is a list of Customer namedtuples that we can use as objects. For example this is how we can print them out.

for customer in customers:
    print(customer.name, customer.products) # Print each name and their products
    for growth, amount in customer.products:
        print(growth, amount) # Print growth and amount for each product.

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