“Python -Filterwörterbuch durch Schlüssel” Code-Antworten

Halten Sie nur wenige Schlüsselwert vom DICT ab

>>> dict_filter = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
>>> large_dict = {"a":1,"b":2,"c":3,"d":4}
>>> new_dict_keys = ("c","d")
>>> small_dict=dict_filter(large_dict, new_dict_keys)
>>> print(small_dict)
{'c': 3, 'd': 4}
>>> 
Sachin

Python -Filterwörterbuch durch Schlüssel

# Basic syntax:
{key: your_dict[key] for key in your_dict.keys() and {'key_1', 'key_2'}}
# Where this uses list comprehension for dictionaries to make a new dictionary
#	with the keys that are found in the set

# Example usage:
your_dict = {'key_1': 1, 'key_2': 2, 'key_3': 3}
{key: your_dict[key] for key in your_dict.keys() and {'key_1', 'key_2'}}
--> {'key_1': 1, 'key_2': 2}
Charles-Alexandre Roy

Filterdikt durch Liste der Schlüsselpython

dict_you_want = { your_key: old_dict[your_key] for your_key in your_keys }
Stupid Stork

Ähnliche Antworten wie “Python -Filterwörterbuch durch Schlüssel”

Fragen ähnlich wie “Python -Filterwörterbuch durch Schlüssel”

Weitere verwandte Antworten zu “Python -Filterwörterbuch durch Schlüssel” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen