Given a dictionary how can I check if two of its keys are the same?

anon

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'
wjandrea

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 key b string values are the same and add a new key to the dictionary equals 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]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I return common values of dictionary values for specific keys?

分類Dev

How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary?

分類Dev

How can I check whether a given file is FASTA?

分類Dev

How can i check if input is a number ? If its a string alert an error

分類Dev

Make dictionary keys to be the same for common values of two different dictionaries

分類Dev

How to get dictionary keys and values if both keys are in two separated dictionaries?

分類Dev

How to convert a dictionary to a list of keys, with repeat counts given by the values?

分類Dev

how can i check two columns while inserting values?

分類Dev

How to combine two arrays with same keys?

分類Dev

How can i convert a python dictionary with tuple keys to a pandas multi Index data frame?

分類Dev

How can I display two Radio Buttons in the same row?

分類Dev

How can I do two foreach loops in php at the same time

分類Dev

How can I do two foreach loops in php at the same time

分類Dev

How can I check if a @patched-out python method is called, without changing its behavior?

分類Dev

How can I check for word similarity from a list in JavaScript (without looping through an entire dictionary)?

分類Dev

How to check if two NSDates are from the same day

分類Dev

How to check if two NSDates are from the same day

分類Dev

How to check if contents of two lists is same in python?

分類Dev

How can I call a php file that is on the same folder of a js file without explicitly declaring its url?

分類Dev

How can I merge two files while printing a given value on resulting empty fields using AWK?

分類Dev

How can I create an array from two models that have different keys in a Rails 3.2 app?

分類Dev

Given a dictionary and a sequence return its total in python

分類Dev

How to update keys in dictionary?

分類Dev

How can I declare two classes so they can be passed to the same function?

分類Dev

If I have multiple OpenPGP keys in GnuPG with the same user ID/e-mail address, how can I distinguish between them?

分類Dev

How to merge two hashes that have same keys in ruby

分類Dev

How to combine (append values) two nested dictionaries with the same keys in python?

分類Dev

How to combine (append values) two nested dictionaries with the same keys in python?

分類Dev

How can I make these two spans stay on the same line, no matter how narrow the browser window gets?

Related 関連記事

  1. 1

    How can I return common values of dictionary values for specific keys?

  2. 2

    How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary?

  3. 3

    How can I check whether a given file is FASTA?

  4. 4

    How can i check if input is a number ? If its a string alert an error

  5. 5

    Make dictionary keys to be the same for common values of two different dictionaries

  6. 6

    How to get dictionary keys and values if both keys are in two separated dictionaries?

  7. 7

    How to convert a dictionary to a list of keys, with repeat counts given by the values?

  8. 8

    how can i check two columns while inserting values?

  9. 9

    How to combine two arrays with same keys?

  10. 10

    How can i convert a python dictionary with tuple keys to a pandas multi Index data frame?

  11. 11

    How can I display two Radio Buttons in the same row?

  12. 12

    How can I do two foreach loops in php at the same time

  13. 13

    How can I do two foreach loops in php at the same time

  14. 14

    How can I check if a @patched-out python method is called, without changing its behavior?

  15. 15

    How can I check for word similarity from a list in JavaScript (without looping through an entire dictionary)?

  16. 16

    How to check if two NSDates are from the same day

  17. 17

    How to check if two NSDates are from the same day

  18. 18

    How to check if contents of two lists is same in python?

  19. 19

    How can I call a php file that is on the same folder of a js file without explicitly declaring its url?

  20. 20

    How can I merge two files while printing a given value on resulting empty fields using AWK?

  21. 21

    How can I create an array from two models that have different keys in a Rails 3.2 app?

  22. 22

    Given a dictionary and a sequence return its total in python

  23. 23

    How to update keys in dictionary?

  24. 24

    How can I declare two classes so they can be passed to the same function?

  25. 25

    If I have multiple OpenPGP keys in GnuPG with the same user ID/e-mail address, how can I distinguish between them?

  26. 26

    How to merge two hashes that have same keys in ruby

  27. 27

    How to combine (append values) two nested dictionaries with the same keys in python?

  28. 28

    How to combine (append values) two nested dictionaries with the same keys in python?

  29. 29

    How can I make these two spans stay on the same line, no matter how narrow the browser window gets?

ホットタグ

アーカイブ