I have one dictionary:
dict_one={'a':'the quick brown fox.','b':'the quick brown fox.', 'c':'good night guys',...,'n':'hey there'}
how can I check if key a
and key b
string values are the same and add a new key to the dictionary equals
to the dictionary showing me if they are different or equal values (True/False)?
dict_one={'a':'the quick brown fox.','b':'the quick brown fox.', 'c':'good night guys',...,'n':'hey there', 'equals':True}
I tried to:
for i,j in dict_one.iteritems():
if (a==b):
return 'True'
else:
return 'False'
You ask two different questions:
Given a dictionary how can I check if two of its keys are the same?
A dict by definition can't have duplicate keys.
How can I check if key
a
and keyb
string values are the same and add a new key to the dictionaryequals
if they are different or equal values (True/False)?
This compares the values of keys a
and b
and assigns the result to key equals
:
dict_one['equals'] = dict_one['a'] == dict_one['b']
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加