简体   繁体   English

使用 .items() 遍历字典时不可散列的类型列表错误

[英]Unhashable type list error when iterating through a dictionary using .items()

I am trying to iterate through a dictionary, where the keys are regular expressions, and the values are categories.我正在尝试遍历字典,其中键是正则表达式,值是类别。 My goal is to go through a column, iterate through each of the different regular expressions, and assign a new column flagged for that category.我的目标是遍历一列,遍历每个不同的正则表达式,并分配一个标记为该类别的新列。 The problem I am running into is that .items() creates a list, which is an unhashable object.我遇到的问题是 .items() 创建了一个列表,它是一个不可散列的对象。

new_columns = ['counts']
category_columns = [dictionary]

for problem_count, problem in zip(new_columns, category_columns):
    for category, reg_expr in problem.items():
        found = notes.str.contains(reg_expr, na=False) # Errors out here 
        note_data[category] = np.where(found, True, False)
    note_data[problem_count] = note_data[problem_keys()].sum(axis=1)

What is the best work around for this?什么是最好的解决方法?

If values are lists, then I would Join them with |如果值是列表,那么我会加入它们 | for a regex:对于正则表达式:

found = notes.str.contains('|'.join(reg_expr), na=False) 

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

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