a = {"hello" : "world", "cat":"bat"}
//Trying to achieve this
//Form a new dictionary only with keys with "hello" and their values
b = {"hello" : "world"}
//This didn't work
b = dict((key, value) if key == "hello" for (key, value) in a.items())
Vorschläge, wie ein bedingten Ausdruck im Wörterbuch Verständnis schließen Schlüssel zu entscheiden, ob, Werttupel sollteBedingte Ausdrücke in Python Wörterbuch Comprehensions
Ist die Bedingung immer in der Form 'key ==" ... "' oder suchen Sie eine allgemeinere Lösung? – icktoofay
'if Schlüssel ==" Hallo "' ist kein [Bedingungsausdruck] (http://docs.python.org/2/reference/expressions.html#conditional-expressions) und 'dict (...)' nicht ein Diktatverständnis. –
'//' ist kein gültiger Python-Kommentar –