我看到过类似的文章,关于从defaultdict中删除/过滤特定键,但是所有这些选项实际上并没有删除空值键对,而只是创建了一个没有它们的新字典。这样做,我将无法访问清洁程序defaultdict的项目。
这是我的问题的详细信息:
我正在尝试从列表中读取多个分子,以便可以访问其单个文件中列出的各种属性。我从索引每个分子中的原子开始;作为键,然后将相应原子的原子序号附加为值。为此,我使用Python的defaultdict(list)来避免KeyError。
Option Explicit
'Global Variables
Dim foundCell
Private Sub btnSearch_Click()
Dim Str
Dim FirstAddr As String
Str = "B-32"
With Sheet1
Set foundCell = .Cells.Find(What:=Str, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then
MsgBox ("""Bingo"" found in row " & foundCell.Row)
UserForm1.location.Text = Cells(foundCell.Row, 3).Value
UserForm1.office.Value = Cells(foundCell.Row, 2).Value
UserForm1.floor.Value = Cells(foundCell.Row, 1).Value
UserForm1.status.Value = Cells(foundCell.Row, 4).Value
UserForm1.telephone.Value = Cells(foundCell.Row, 5).Value
UserForm1.mobile.Value = Cells(foundCell.Row, 6).Value
UserForm1.owner.Value = Cells(foundCell.Row, 7).Value
UserForm1.notes.Value = Cells(foundCell.Row, 8).Value
FirstAddr = foundCell.Address
Else
MsgBox ("Bingo not found")
End If
Dim i As Integer
Do Until foundCell Is Nothing
Set foundCell = Sheet1.Cells.FindNext(After:=foundCell)
i = i + 1
If foundCell.Address = FirstAddr Then Exit Do
Loop
MsgBox (i)
End Sub
由于该分子没有任何Cl,Br,I或O,因此其值为[]。我想删除所有此类情况。
P.S:Python的新手,这是我在StackOverflow上的第一篇文章! 谢谢
答案 0 :(得分:0)
只需要做:
for element in element_list:
if element in element_indices:
这将忽略每个分子中实际上不存在的所有键值对。