使用for循环添加到defaultdict键

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

我尝试过在其他线程上找到的多个不同的建议,但似乎没有任何工作(是的,我是编程的新手)。

我有以下代码:

SysLength = defaultdict(list) #empty dictionary 
for x in SystemDict: 
    length = len(SystemDict[x]) #obtains length of each system
    SysLength[x].append(length) #adds system and length to the dictionary
SysMin = sorted(SysLength, key=SysLength.get) #sorts systems from smallest to largest 

emailDict = defaultdict(list) #empty dictionary

for x in SysMin: #cycles through lowest system first 
    for email in SystemDicto3[x]: #cycles through email in the lowest system
        emailDict[email] = 0 #adds email to dictionary and sets it equal to 0 

for x in SysMin: 
    for email in SystemDicto3[x]: 
        if emailDict[email] < 3: #if count is below 3, pass email through
            emailDict[email] += 1 #adds 1 to each email passed through
            SystemDictu4[x].append(email)

我遇到这个问题:

SystemDictu4[x].append(email)

我要做的是将'email'附加到键值'x'。 SystemDictu4是一个defaultdict,每个键下有1000个条目,我需要将email变量添加到这些键中。

我收到以下错误:

TypeError: cannot concatenate a non-NDFrame object
python pandas dictionary append key
1个回答
0
投票

显示的代码是正确的,但正如@jpp建议的那样,我回顾了SystemDictu4字典是如何创建的。我创建字典的原始方法是使用以下代码片段:

SystemDictu4 = defaultdict(list) #initializing dictionary
for item in SystemsList:
    SystemDictu4[item]=dfu4[dfu4[item]== 1.0]["Email"]

经过一些测试后,我了解到将.values.tolist()添加到最后一行的末尾(在['Email']之后),它可以工作,不会更改为系列。谢谢大家的帮助!

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