Python 字典 get() 方法:操作指南

B站影视 2025-02-10 00:57 3

摘要:Python 中的字典 'get' 方法可帮助安全地检索值,而无需担心 KeyError 异常。但它不仅仅是方括号表示法的更安全的替代方案,它还是一种编写更简洁、更易于维护的代码的工具。让我们看看如何有效地使用它。

Python 中的字典 'get' 方法可帮助安全地检索值,而无需担心 KeyError 异常。但它不仅仅是方括号表示法的更安全的替代方案,它还是一种编写更简洁、更易于维护的代码的工具。让我们看看如何有效地使用它。

下面是基本模式:

value = dictionary.get(key, default_value)

比较这些方法:

# Using square brackets - can raise KeyErroruser = {"name": "John", "age": 30}try: email = user["email"]except KeyError: email = None# Using get - cleaner and more directuser = {"name": "John", "age": 30}email = user.get("email", None) # Returns None if key doesn't exist# Dictionary of user preferencespreferences = { "theme": "dark", "notifications": True}# Get font size with a sensible defaultfont_size = preferences.get("font_size", 12)# Get language with system defaultimport localesystem_language = locale.getdefaultlocale[0]language = preferences.get("language", system_language)# Get refresh rate with calculated defaultdef calculate_default_refresh: # Complex logic to determine optimal refresh rate return 60refresh_rate = preferences.get("refresh_rate", calculate_default_refresh)class AppConfig: def __init__(self, config_dict): self.debug = config_dict.get("debug", False) self.host = config_dict.get("host", "localhost") self.port = config_dict.get("port", 8080) self.timeout = config_dict.get("timeout", 30) self.retries = config_dict.get("retries", 3) def as_dict(self): return { "debug": self.debug, "host": self.host, "port": self.port, "timeout": self.timeout, "retries": self.retries }# Usageconfig = { "host": "example.com", "debug": True}app_config = AppConfig(config)print(f"Server will run on {app_config.host}:{app_config.port}")def process_user_data(users): processed_data = for user in users: processed_user = { "name": user.get("name", "Anonymous"), "age": user.get("age", 0), "status": user.get("status", "unknown").lower, "last_active": user.get("last_login", "never"), "engagement_score": calculate_engagement(user) } processed_data.append(processed_user) return processed_datadef calculate_engagement(user): points = 0 points += 10 if user.get("profile_complete", False) else 0 points += min(user.get("posts_count", 0), 50) points += min(user.get("comments_count", 0) * 0.5, 25) return points# Usageusers = [ {"name": "John", "posts_count": 20}, {"name": "Jane", "profile_complete": True, "comments_count": 30},]processed = process_user_data(users)def safe_get_nested(dictionary, *keys, default=None): """Safely navigate nested dictionaries.""" current = dictionary for key in keys: if isinstance(current, dict): current = current.get(key, default) else: return default return current# Example usage with deeply nested datadata = { "user": { "profile": { "address": { "city": "New York", "country": "USA" } } }}# Safe navigationcity = safe_get_nested(data, "user", "profile", "address", "city")# Returns "New York"# Non-existent pathpostal = safe_get_nested(data, "user", "profile", "address", "postal_code", default="N/A")# Returns "N/A"from time import timeclass SimpleCache: def __init__(self, default_timeout=300): # 5 minutes default self._cache = {} self.default_timeout = default_timeout def get(self, key, default=None): cache_item = self._cache.get(key, {}) if not cache_item: return default expiry = cache_item.get("expiry") if expiry and time > expiry: del self._cache[key] return default return cache_item.get("value", default) def set(self, key, value, timeout=None): timeout = timeout or self.default_timeout self._cache[key] = { "value": value, "expiry": time + timeout }# Usagecache = SimpleCachecache.set("user_123", {"name": "John", "age": 30})user = cache.get("user_123", default={"name": "Unknown"})# Original data with missing valuesdata = [ {"id": 1, "name": "John"}, {"id": 2}, {"id": 3, "name": "Jane"}]# Create normalized dictionarynormalized = { item["id"]: item.get("name", f"User_{item['id']}") for item in data}# Result: {1: "John", 2: "User_2", 3: "Jane"}def process_text_data(data_dict): """Process text data with various default transformations.""" processed = { "title": data_dict.get("title", "").title, "description": data_dict.get("description", "").strip, "tags": [ tag.lower for tag in data_dict.get("tags", ) ], "category": data_dict.get("category", "uncategorized").lower, "word_count": len(data_dict.get("content", "").split) } return processed# Usagearticle = { "title": "Python tips", "description": " Helpful Python tips ", "tags": ["Python", "Programming", "Tips"],}processed_article = process_text_data(article)# Problematic: List as default valuedef get_tags(user_dict): return user_dict.get("tags", ).append("default") # Returns None!# Fixed versiondef get_tags(user_dict): tags = user_dict.get("tags", ) tags.append("default") return tags# Inefficient: Calculating default value every timedef get_config(key): return config_dict.get(key, expensive_calculation)# Better: Calculate default only when neededdef get_config(key): value = config_dict.get(key) if value is None: value = expensive_calculation return valuedef get_int_value(dictionary, key, default=0): """Safely get an integer value from a dictionary.""" value = dictionary.get(key, default) try: return int(value) except (TypeError, ValueError): return default# Usagedata = {"count": "123", "invalid": "abc"}valid_count = get_int_value(data, "count") # Returns 123invalid_count = get_int_value(data, "invalid") # Returns 0missing_count = get_int_value(data, "missing") # Returns 0

'get' 方法不仅仅是一个丢失键的安全网——它是一个编写更简洁、更易于维护的代码的工具。使用它来正常处理缺失值,提供合理的默认值,并使代码在应对意外输入时更加健壮。

来源:自由坦荡的湖泊AI

相关推荐