简体   繁体   English

如何使用列表项寻址字典键并附加相应的值

[英]How to use a list item to address a dictionary key and append the respective value

I have got a list holding customer information and each item of the customers list is itself a list of a respective set of information. 我有一个保存客户信息的列表,客户列表的每个项目本身就是一组相应信息的列表。 So: 所以:

customers = [
  [customerID1, NameOfCustomer1, etc., 01 02 03] 
  [customerID2, NameOfCustomer2, etc., 02 05]
  .
  .
  .
  ]

The digits within each customer's info set are categories that I need to assign the customers to. 每个客户的信息集中的数字是我需要将客户分配给的类别。 That is, I have got a dictionary with n keys, one for each category: 也就是说,我有一本包含n个键的字典,每个类别对应一个:

dict = {
  01: [],
  02: [],
  03: [],
  04: [],
  05: []
  }

Now I need the customers to be allocated to their respective categories so that customer 1 ends up in categories one, two and three - while customer two goes into two and five. 现在,我需要将客户分配到各自的类别,以便客户1最终分为类别1、2和3-而客户2进入类别2和5。 Of course I could run n if-statements, one for each existing category, but with increasing number of categories I find that rather disturbing. 当然,我可以运行n个if语句,每个现有类别一个,但是随着类别数量的增加,我发现这相当令人不安。 What I thus wanted to do: get a list of categories from each customer: 因此,我想做的是:获取每个客户的类别列表:

for customer in customers:
  categories = re.findall(r'[0-9]{2}', customer[3])

So much for the easy part. 简单的部分就这么多。 Now I am looking for a way to basically loop through this 'categories'-list: 现在,我正在寻找一种基本遍历此“类别”列表的方法:

for category in categories:
  dict[category].append(customer)

However, python doesn't seem to like me using a variable to select a key. 但是,python似乎不喜欢我使用变量来选择键。 There's probably a stupidly easy solution for this one - I am just not aware of it. 对于这个可能有一个非常简单的解决方案-我只是不知道。

Thank you very much everyone! 非常感谢大家!

Step #1 is to turn those flat lists into a dictionary, which is more useful for accessing properties. 步骤1是将这些平面列表转换为字典,这对于访问属性更有用。 I've had to imagine what your data actually looks like, but you should get the idea: 我不得不想象您的数据实际上是什么样的,但是您应该了解一下:

>>> customers = [
...     ['customerID1', 'NameOfCustomer1', 'e','t','c', '01 02 03'],
...     ['customerID2', 'NameOfCustomer2', 'e','t','c', '02 05']
...     ]
>>> 
>>> cust_keys = ('id', 'name', 'q1','q2','q3','categories')
>>> cdicts = [dict(zip(cust_keys, cust_vals)) for cust_vals in customers]
>>> cdicts
[{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer1', 'id': 'customerID1', 'categories': '01 02 03'}, {'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer2', 'id': 'customerID2', 'categories': '02 05'}]

Better would be to have the categories as lists of codes, and we don't need regex for that: 最好将类别作为代码列表,而我们不需要正则表达式:

>>> for cdict in cdicts:
...     cdict['categories'] = cdict['categories'].split()
... 
>>> cdicts
[{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer1', 'id': 'customerID1', 'categories': ['01', '02', '03']}, {'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer2', 'id': 'customerID2', 'categories': ['02', '05']}]

Now, in order to append to a bunch of category lists, we can either check to see each time whether the key exists and make an empty list if not, or we can use a defaultdict which handles that for us: 现在,为了添加到一堆类别列表中,我们可以检查每次是否存在该键,如果不存在则创建一个空列表,或者我们可以使用defaultdict为我们处理该键:

>>> from collections import defaultdict
>>> by_categories = defaultdict(list)
>>> for customer in cdicts:
...     for category in customer['categories']:
...         by_categories[category].append(customer)
... 

which produces 产生

>>> for k in sorted(by_categories):
...     print 'category', k, 'contains:'
...     for v in by_categories[k]:
...         print v
... 
category 01 contains:
{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer1', 'id': 'customerID1', 'categories': ['01', '02', '03']}
category 02 contains:
{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer1', 'id': 'customerID1', 'categories': ['01', '02', '03']}
{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer2', 'id': 'customerID2', 'categories': ['02', '05']}
category 03 contains:
{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer1', 'id': 'customerID1', 'categories': ['01', '02', '03']}
category 05 contains:
{'q1': 'e', 'q3': 'c', 'q2': 't', 'name': 'NameOfCustomer2', 'id': 'customerID2', 'categories': ['02', '05']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM