匹配词典的值

时间:2016-08-10 06:25:34

标签: python dictionary

如果有多个词典,我会收集一些记录:

Sub QuickDeleteRows()
Dim Sheet_Data As Worksheet, NewSheet_Data As Worksheet, Data As Range
Dim Sheet_Name As String, Text As String, Water As Long, Beer As Long, Vodka As Long

On Error GoTo Error_Handler
SpeedUp True

Set Sheet_Data = Sheets("SOVI")
Sheet_Name = Sheet_Data.Name
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

ReDim Output(1 To LastRow - 1, 1 To 1) As Long

For i = 1 To LastRow - 1
    Text = Cells(i + 1, 13)
    Water = InStr(Text, "water")
    Beer = InStr(Text, "beer")
    Vodka = InStr(Text, "vodka")
    If Water > 0 Or Beer > 0 Or Vodka > 0 Then Output(i, 1) = 1
Next
[S2].Resize(LastRow - 1, 1) = Output

LastColumn = Cells(2, Columns.Count).End(xlToLeft).Column

Set Data = Sheet_Data.Range(Cells(1, 1), Cells(LastRow, LastColumn))

Set NewSheet_Data = Sheets.Add(After:=Sheet_Data)

Data.AutoFilter Field:=19, Criteria1:="=1"
Data.Copy

With NewSheet_Data.Cells
    .PasteSpecial xlPasteColumnWidths
    .PasteSpecial xlPasteAll
    .Cells(1, 1).Select
    .Cells(1, 1).Copy
End With

Sheet_Data.Delete
NewSheet_Data.Name = Sheet_Name
NewSheet_Data.Columns(19).Clear

Safe_Exit:
    SpeedUp False
    Exit Sub
Error_Handler:
    Resume Safe_Exit
End Sub

Sub SpeedUp(SpeedUpOn As Boolean)
With Application
    If SpeedUpOn Then
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
        .DisplayStatusBar = False
        .DisplayAlerts = False
    Else
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .DisplayStatusBar = True
        .DisplayAlerts = True
    End If
End With
End Sub

我想搜索与第一个字典的ID匹配的所有记录,并将它们保存在一个包含所有字段的新记录中,' value_1',' value_2'和' value_3':

d1 = {'id':['223','444'],'value_1':['v1','x1']}
d2 = {'id': ['223','666'],'value_2':['v2','x2']}
d3 = {'id':['223','444'], 'value_3':['v3','x3']}

如果所有词典中都没有其中一条记录,我会添加' ----'在那个领域。因此,在这种情况下,输出将是:

d_4 = {'id':[],'value_1':[],'value_2':[],'value_3':[]}

我写这段代码是为了这样做:

d_4

{'id': ['223', '444'],
 'value_1': ['v1', 'x1'],
 'value_2': ['v2', '----'],
 'value_3': ['x3', 'x3']} 

但似乎不是一个好方法。

1 个答案:

答案 0 :(得分:1)

也许这会对你有所帮助:

代码:

import sys
print(sys.version)

# you should use an iterable for your data
in_list = [{'id':['223','444'],'value_1':['v1','x1']},
    {'id': ['223','666'],'value_2':['v2','x2']},
    {'id':['223','444'], 'value_3':['v3','x3']}
    ]

print "Input:\n", in_list

# note that dictionaries are not ordered
out_dict = {}
out_dict["id"] = in_list[0]["id"]
out_dict["value_1"] = in_list[0]["value_1"]

for i,d in enumerate(in_list[1:]):  
    values = [v[1] for v in d.items() if "value" in v[0]][0]
    #print(i, d, values)
    if in_list[i+1]["id"] != in_list[0]["id"]:
        values[1] = "---"
    out_dict["value_{}".format(i+2)] = values

print "Output:\n", out_dict

输出:

2.7.2 (default, Aug 31 2011, 14:05:14) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build)]
Input:
[{'value_1': ['v1', 'x1'], 'id': ['223', '444']}, {'id': ['223', '666'], 'value_2': ['v2', 'x2']}, {'id': ['223', '444'], 'value_3': ['v3', 'x3']}]
Output:
{'value_1': ['v1', 'x1'], 'id': ['223', '444'], 'value_3': ['v3', 'x3'], 'value_2': ['v2', '---']}

<小时/> 更新:修复错误以获得所需的输出 Update2:i + 1偏移量