是否有一种python方式初始化字典?
animals = ["dog","cat","cow"]
for x in animals:
primers_pos[x]={}
是否有像
这样的东西(primers_pos[x]={} for x in animals)
答案 0 :(得分:2)
您可以使用字典理解(在Python 2.7 +中支持):
>>> animals = ["dog", "cat", "cow"]
>>> {x: {} for x in animals}
{'dog': {}, 'cow': {}, 'cat': {}}
答案 1 :(得分:1)
您也可以使用 collections.defaultdict
Sub StatementReformat()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
'First Page
.Text = "REPORT*EXTRACT FILE : [A-Z0-9]{1,}^13^12"
.Replacement.Text = ""
.Execute Replace:=wdReplaceOne
'Column Headers (except New First Page)
.Text = "^12*VEND^13*[-]{1,}^13"
.Execute Replace:=wdReplaceAll
'Header Underline on First Page
.Text = "[-]{1,}^13"
.Execute Replace:=wdReplaceAll
'Last Page
.Text = " TOTAL #*^12REPORT*{1,255}PAGE*{1,255}FUND TOTALS*[=]{1,}*^13*^13"
.Execute Replace:=wdReplaceAll
'Unwanted Header Lines - First Page
.Text = "REPORT*CHECK^13"
.Execute Replace:=wdReplaceAll
'Empty Paragraphs
.Text = "[ ]{1,}^13"
.Execute Replace:=wdReplaceAll
'Header Line Wrap - First Page
.Text = "(STATUS)^13"
.Replacement.Text = "\1"
'Record Line Wraps
.Execute Replace:=wdReplaceAll
.Text = "(OUTSTANDING)^13"
.Execute Replace:=wdReplaceAll
.Text = "(CLEARED)^13"
.Replaceme`enter code here`nt.Text = "\1 "
.Execute Replace:=wdReplaceAll
.Text = "(VOIDED)^13"
.Replacement.Text = "\1 "
.Execute Replace:=wdReplaceAll
'Pad Records with Multiple Rows
'Pad Records with Multiple Rows
.Text = "(^13)([ ]{20})([ ]{1,8}[0-9]{1,8}.[0-9]{2})"
.Replacement.Text = "\1\2\2\2\2\2\2\2 \3"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
然后,每当您使用新密钥引用 primer_pos 时,将自动创建字典
primer_pos = defaultdict(dict)
将链创建 {'Fluffy':'Nice Kitty'} 作为字典 primer_pos
中键'cat'的值