[英]Python dictionary length mismatch
我寫了一個小的測試腳本:
#!/usr/bin/env python
import pexpect
import re
List_dictionary = {
'b -c \\"select name,type,is_supported from algorithm where name = \\\'RSA\\\' and key_size=1024 \\"':'app \"algo -e -n RSA -ks 1024\"',
'b -c \\"select name,type,is_supported from algorithm where name = \\\'RSA\\\' and key_size=1024 \\"':'app \"algo -d -n RSA -ks 1024\"'
}
test_dict={
'a':1,
'b':2
}
for a,b in List_dictionary.items():
print a
for a,b in test_dict.items():
print a
print len(List_dictionary)
print len(test_dict)
我得到的輸出是:
./test.py
b -c \"select name,type,is_supported from algorithm where name = \'RSA\' and key_size=1024 \"
a
b
1
2
為什么僅從List_dictionary中獲取一個元素,其長度為1。相反,據我所知,它應該為2。
這很容易。
字典的工作方式是,每個key
在key : value
對中都是唯一的。 意思是您詞典中的第二個鍵將替換第一個,因為它們是相同的。
'b -c \\"... \\"'
'b -c \\"... \\"'
嘗試在開始時更改一個字母以表示´c -c`。
另一種解決方案是為您的命令/字符串設置適當的鍵,然后(如下面的GWW所述)將兩個命令都放在列表中。 考慮以下:
List_dictionary = {
'first' : ['b -c \\"select name,type,is_supported from algorithm where name = \\\'RSA\\\' and key_size=1024 \\"', 'app \"algo -e -n RSA -ks 1024\"'],
'sendond' : ['b -c \\"select name,type,is_supported from algorithm where name = \\\'RSA\\\' and key_size=1024 \\"', 'app \"algo -d -n RSA -ks 1024\"']
}
這為您提供了唯一的鍵,並使整個詞典更加簡潔。
您甚至可以將密鑰從first/second/
重命名為algo -e
和algo -d
因為keys字段中允許使用空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.