Reputation: 3629
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
What this code is doing is... It finds value lists from the 'Table' dictionary by keys called 'SearchValue'
I check if the key exists by the following code..
targetValue in LineText
What I want to do to get the value at the same time of the key value existing check. Because I think that performs better.
Upvotes: 0
Views: 292
Reputation: 50985
I've reformatted your code according to the python style guide and added some optimizations:
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'''
search_value = u'''A
B
C
D
Bang
F
Bang
G
H'''
line_text = dict(line.split(",") for line in table.splitlines())
for target_value in search_value.splitlines():
print line_text.get(target_value, "Error: {0}".format(target_value))
Upvotes: 1
Reputation: 80761
you should take a look to the dict.get method :
SearchValue = SearchValue.splitlines()
for targetValue in SearchValue:
print LineText.get(targetValue, 'Error:' + targetValue)
Upvotes: 2