使用for循环添加到defaultdict键

2024-09-30 14:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在其他线程上尝试了多种不同的建议,但似乎没有任何效果(是的,我对lol编程是新手)。你知道吗

我有以下代码:

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)

我要做的是在键值“x”后面加上“email”。SystemDictu4是一个defaultdict,每个键下有1000个条目,我需要将email变量添加到这些键中。你知道吗

我得到以下错误:

TypeError: cannot concatenate a non-NDFrame object

Tags: toinfordictionaryemailsystemlengthadds
1条回答
网友
1楼 · 发布于 2024-09-30 14:29:28

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

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

经过一些测试,我了解到在最后一行的末尾添加.values.tolist()(在['Email']之后),它可以工作,不会变成一个序列。谢谢大家的帮助!你知道吗

相关问题 更多 >