创建一个字典,将列表中元素的长度作为键

时间:2019-06-24 06:19:34

标签: python list dictionary

我有一个列表,其中有多个作为元组存储在列表中的元素,我想创建一个字典,其中键的长度为1,2等,而各个长度的元素。 列表的示例是

combination = [('A',), ('B',), ('C',), ('D',), ('A', 'B'), ('A','C'),
 ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D'), ('A', 'B', 'C'), 
('A', 'B', 'D'), ('A', 'C', 'D'), ('B', 'C', 'D'),('A', 'B', 'C', 'D')]

我尝试过 temp_dict = {len(i): i for i in combinations}

所需的输出是

{1: [('A',), ('B',), ('C',), ('D',)], 2: [('A', 'B'), ('A','C'),
 ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')], 3: [('A', 'B', 'C'), 
('A', 'B', 'D'), ('A', 'C', 'D'), ('B', 'C', 'D')], 4: [('A', 'B', 'C', 'D')]}```

4 个答案:

答案 0 :(得分:6)

您可以尝试其中包含列表理解的字典理解

temp_dict = {len(i): [x for x in combination if len(x) == len(i)] for i in combination}
print(temp_dict)

或者您可以使用setdefault

temp_dict = {}
for i in combination:
    temp_dict.setdefault(len(i), []).append(i)
print(temp_dict)

两个输出:

{1: [('A',), ('B',), ('C',), ('D',)], 2: [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')], 3: [('A', 'B', 'C'), ('A', 'B', 'D'), ('A', 'C', 'D'), ('B', 'C', 'D')], 4: [('A', 'B', 'C', 'D')]}

答案 1 :(得分:2)

您当前的方法{len(i): i for i in combination}实际上是选择特定长度的最后一个元组并将其添加到字典中,这不是您想要的,而是您想要所有这些元组的列表。

您可以使用collections.defaultdict创建列表的字典作为值。然后,您可以遍历列表并将相同长度的元组附加到键上

from collections import defaultdict

res = defaultdict(list)

#Iterate over the list
for item in combination:
    #Append tuples with same length to same value list
    res[len(item)].append(item)

print(dict(res))

输出将为

{1: [('A',), ('B',), ('C',), ('D',)], 
2: [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')], 
3: [('A', 'B', 'C'), ('A', 'B', 'D'), ('A', 'C', 'D'), ('B', 'C', 'D')], 
4: [('A', 'B', 'C', 'D')]}

答案 2 :(得分:1)

另一种方法是使用itertools.groupby。请注意,groupby假设您的数据是已排序

from itertools import groupby

{k: list(g) for k, g in groupby(combination, key=len)}

输出:

{1: [('A',), ('B',), ('C',), ('D',)],
 2: [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')],
 3: [('A', 'B', 'C'), ('A', 'B', 'D'), ('A', 'C', 'D'), ('B', 'C', 'D')],
 4: [('A', 'B', 'C', 'D')]}

答案 3 :(得分:1)

另一张照片是通过 $mail = new PHPMailer; $froms=$respu['froms']; $mail->Timeout = 3600; $mail->SMTPDebug = 2; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = $respu['server']; // Specify main and backup SMTP servers $mail->SMTPAuth = $respu['authentication']; // Enable SMTP authentication $mail->Username = $respu['username']; // SMTP username $mail->Password = $respu['password']; // SMTP password $mail->SMTPSecure = $respu['security']; // Enable TLS encryption, `ssl` also accepted $mail->Port = $respu['port']; // TCP port to connect to $mail->SetFrom($respu['username'],$froms); $mail->AddAddress($to); $mail->Subject = $subject; $mail->MsgHTML($message); if(!$mail->Send()) { //$errors=$mail->getSMTPInstance()->getError(); $date=date('Y-m-d h:i'); echo $msg= "Message Not Sent: to $to " . $mail->ErrorInfo; $date=date('Y-m-d h:i'); $sql="insert into log (log_text,user_email,log_time,status)values ('$msg','$to','$date',0) "; $this->query_return($sql); exit(); } else {$date=date('Y-m-d h:i'); $sql="insert into log (log_text,user_email,log_time,status)values ( 'Message Sent Successfully ','$to','$date',1) "; $this->query_return($sql); }

itertools.groupby