遍历“ for循环”时设置字典值

时间:2019-04-02 14:12:24

标签: python list dictionary for-loop iteration

我正在尝试创建一个嵌套的字典,其中包含从for循环中提取的一组值,以测量各种客户产品配对的增长和收入金额。但是,当我遍历数据帧以设置字典的元素时,每个字典元素最终都具有相同的值。这是怎么回事?

我已经尝试过更改列表构建方式的各种元素,但无济于事。

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

1 个答案:

答案 0 :(得分:0)

字典是一个键值对(我相信您可能知道)。如果您尝试使用字典中已存在的键来写入字典,则字典将覆盖该键的值。

示例:

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

您的项目似乎很好地在python中使用了namedtuplenamedtuple基本上是轻量级/对象。 我的示例代码可能是错误的,因为我不知道您的for循环是如何工作的(注释对每个人都有帮助)。话虽如此,这只是一个例子。

我之所以提出此建议,是因为dictionaries所消耗的内存比它们所保存的对象多33%(尽管它们要快得多)。

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

这里customers是我们可以用作对象的客户namedtuples的列表。 例如,这就是我们可以打印出来的方式。

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.