简体   繁体   中英

Python - Check if String is in Dictionary Value when Value is a list

I am trying to resolve the following: I created a list of the books in the Bible. I also created a Dictionary which has a Key and a Value which is a reference to the list created.

I would like to see if a string value is present in the dictionary, if present, return the key of the value.

Here is my code:

# Dict List for bible
BIBLE_BOOKS_LIST_DICT = [
("Genesis"), ("Exodus"), ("Leviticus"),
("Numbers"), ("Deuteronomy"), ("Joshua"),
("Judges"), ("1 Samuel"), ("2 Samuel"),
("1 Kings"), ("2 Kings"), ("1 Chronicles"),
("2 Chronicles"), ("Ezra"), ("Nehemiah"),
("Esther"), ("Job"), ("Psalms"), ("Proverbs"),
("Ecclesiastes"), ("Song of Solomon"),
("Isaiah"), ("Jeremiah"), ("Lamentations"),
("Ezekiel"), ("Daniel"), ("Hosea"), ("Joel"),
("Amos"), ("Obadiah"), ("Jonah"), ("Micah"),
("Nahum"), ("Habakkuk"), ("Zephaniah"),
("Haggai"), ("Zechariah"), ("Malachi"),
("Matthew"), ("Mark"), ("Luke"), ("John"),
("Acts"), ("Romans"), ("1 Corinthians"),
("2 Corinthians"), ("Galatians"), ("Ephesians"),
("Philippians"), ("Colossians"), ("1 Thessalonians"),
("2 Thessalonians"), ("1 Timothy"), ("2 Timothy"),
("Titus"), ("Philemon"), ("Hebrews"), ("James"),
("1 Peter"), ("2 Peter"), ("1 John"), ("2 John"),
("3 John"), ("Jude"), ("Revelation")
]

# Dict for bible categories
BIBLE_BOOKS_DICT = {
'The Law':BIBLE_BOOKS_LIST_DICT[:5],
'OT History':BIBLE_BOOKS_LIST_DICT[5:16],
'Poetry':BIBLE_BOOKS_LIST_DICT[16:21],
'Major Prophets':BIBLE_BOOKS_LIST_DICT[21:26],
'Minor Prophets':BIBLE_BOOKS_LIST_DICT[26:38],
'Gospels':BIBLE_BOOKS_LIST_DICT[38:42],
'NT History':BIBLE_BOOKS_LIST_DICT[42:43],
'Pauline Epistles':BIBLE_BOOKS_LIST_DICT[43:52],
'Pastoral Letters':BIBLE_BOOKS_LIST_DICT[52:55],
'General Epistles':BIBLE_BOOKS_LIST_DICT[55:64],
'Prophecy':BIBLE_BOOKS_LIST_DICT[64:65]
}

I've been working on this for hours but have not found any solution! My logic is,

if "Matthew" in BIBLE_BOOKS_DICT.values():
    print *the key related to that value*

Thank you!

How about this, using the dict.items() method:

for key, value in BIBLE_BOOKS_DICT.items():
    if "Matthew" in value:
        print(key)

If you want to be able to look up by book, and return a category, you need to be storing the books as the keys in the dictionary:

BIBLE_BOOKS_DICT = {
"Matthew": 'Gosphels'
"Revelation": 'Prophecy'

# etc...

"1 John": 'Gosphels'

Dictionaries are able to look up a value given a key in very quick runtime (eg constant). But to look up a key given a value, you would essentially have to iterate over all of the values, and then back-map the found value to its key. With the above dictionary your lookup logic would be:

# Look up the key, and assign its value to a variable
result = BIBLE_BOOKS_DICT.get("Matthew")

# Searched keys that don't exist in the dictionary will return None.
if result is not None:
    print result

Let me know if any of this doesn't make sense, and I'm happy to elaborate further!

Someone was faster.

input = 'Matthew'
for k, v in BIBLE_BOOKS_DICT.items():
    if input in v:
        print(k)

But I have a function.

def get_cat(book):
    for k, v in BIBLE_BOOKS_DICT.items():
        if book in v:
            return k

print(get_cat('Matthew'))

Output

Gospels
Gospels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM