根据单独的字典有条件地创建字典

问题描述 投票:0回答:1

我发现了类似的问题,但大多数是针对Python 2和更早版本的。

我有一列有5000多个值,我正在用它创建一个字典。我想用某些单词列出一些行,如果该值不包含它,它将被保存到“其他”。

我已经完成以下工作:

my_groups = {
    'Group 1' : r'utilities|cleaning',
    'Group 2' : r'cooking|kitchen',
    'Group 3' : r'decorations|planning',
    'Group 4' : r'conceirge|guest|information|attendants',
    #...there are 300 groups in the dataset
}

但是由于数据非常大,所以我需要将这些组分为2类:frontlinebackdoor。我可以做:

group_cat = {
    'Frontline' : r'conceirge|guest|information|attendants|waiter|MC',
    'Backdoor' : r'utilities|cleaning|cooking|kitchen|chef|event|decorations|planning',
    #...there are 300 groups in the dataset
}

但是列表会很长,因为大约有300个具有不同说明的组。有没有一种方法我可以只指定第一个,而在另一个中自动分配其他?

#something like this
group_cat = {
    'Frontline' : r'conceirge|guest|information|attendants|waiter|MC',
    'Backdoor' : r'OTHER_KEYWORDS_HERE',
}

我发现了类似的线程,但大多数用于Python 2和更早版本。我有一列有5000多个值,我正在用它创建一个字典。我想用某些单词列出某些行,如果...

python dictionary jupyter-notebook
1个回答
0
投票

如果我正确理解了这个问题,那么下面的代码应该可以工作:

© www.soinside.com 2019 - 2024. All rights reserved.