From 7db86add20455b94c71c517506fd6ede09f1ab35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 28 Nov 2019 00:50:16 +0100 Subject: [PATCH] Dict --- README.md | 5 +++-- index.html | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 01a5d1e..5ffa0ef 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,6 @@ value = .setdefault(key, default=None) # Returns and writes default if ``` ```python -.update() = dict() # Creates a dict from coll. of key-value pairs. = dict(zip(keys, values)) # Creates a dict from two collections. = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys. @@ -88,7 +87,9 @@ value = .setdefault(key, default=None) # Returns and writes default if ```python value = .pop(key) # Removes item or raises KeyError. -{k: v for k, v in .items() if k in keys} # Filters dictionary by keys. +.update() # Adds items. Replaces ones with matching keys. +[k for k, v in .items() if v == value] # Returns list of keys that point to value. +{k: v for k, v in .items() if k in keys} # Returns dictionary filtered by keys. ``` ### Counter diff --git a/index.html b/index.html index d2f8db4..4eff99d 100644 --- a/index.html +++ b/index.html @@ -266,13 +266,14 @@ value = <dict>.setdefault(key, default=None# Creates a dict with default value of type. <dict> = collections.defaultdict(lambda: 1) # Creates a dict with default value 1. -
<dict>.update(<dict>)
-<dict> = dict(<collection>)                     # Creates a dict from coll. of key-value pairs.
+
<dict> = dict(<collection>)                     # Creates a dict from coll. of key-value pairs.
 <dict> = dict(zip(keys, values))                # Creates a dict from two collections.
 <dict> = dict.fromkeys(keys [, value])          # Creates a dict from collection of keys.
 
value = <dict>.pop(key)                         # Removes item or raises KeyError.
-{k: v for k, v in <dict>.items() if k in keys}  # Filters dictionary by keys.
+<dict>.update(<dict>)                           # Adds items. Replaces ones with matching keys.
+[k for k, v in <dict>.items() if v == value]    # Returns list of keys that point to value.
+{k: v for k, v in <dict>.items() if k in keys}  # Returns dictionary filtered by keys.
 

Counter

>>> from collections import Counter
 >>> colors = ['blue', 'red', 'blue', 'red', 'blue']