Table = u'''A,This is A
B,This is B
C,This is C
D,This is D
E,This is E
F,This is F
G,This is G
H,This is H
I,This is I
J,This is J'''
SearchValue = u'''A
B
C
D
Bang
F
Bang
G
H'''
Table = Table.splitlines()
LineText = {}
for targetLineText in Table:
LineText[targetLineText.split(',')[0]] = targetLineText.split(',')[1]
SearchValue = SearchValue.splitlines()
for targetValue in SearchValue:
print LineText[targetValue] if targetValue in LineText else 'Error:' + targetValue
这个代码的作用是。。。 它通过名为“SearchValue”的键从“Table”字典中查找值列表
我通过下面的代码检查密钥是否存在。。在
^{pr2}$我要做的是在键值存在检查的同时获取值。因为我觉得这样做更好。在
您应该看看dict.get方法:
我根据the python style guide重新格式化了代码,并添加了一些优化:
相关问题 更多 >
编程相关推荐