• Nov 19, 2022 •CodeCatch
1 like • 3 views
def hex_to_rgb(hex): return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)) hex_to_rgb('FFA501') # (255, 165, 1)
0 likes • 0 views
from collections import defaultdict def combine_values(*dicts): res = defaultdict(list) for d in dicts: for key in d: res[key].append(d[key]) return dict(res) d1 = {'a': 1, 'b': 'foo', 'c': 400} d2 = {'a': 3, 'b': 200, 'd': 400} combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
• Jun 1, 2023 •CodeCatch
0 likes • 1 view
filename = "data.txt" data = "Hello, World!" with open(filename, "a") as file: file.write(data)
0 likes • 6 views
def when(predicate, when_true): return lambda x: when_true(x) if predicate(x) else x double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2) print(double_even_numbers(2)) # 4 print(double_even_numbers(1)) # 1
• Mar 26, 2023 •AustinLeath
import os # Get the current directory current_dir = os.getcwd() # Loop through each file in the current directory for filename in os.listdir(current_dir): # Check if the file name starts with a number followed by a period and a space if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ': # Remove the number, period, and space from the file name new_filename = filename[3:] # Rename the file os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))
• May 31, 2023 •CodeCatch
0 likes • 5 views
my_list = [1, 2, 3, 4, 5] removed_element = my_list.pop(2) # Remove and return element at index 2 print(removed_element) # 3 print(my_list) # [1, 2, 4, 5] last_element = my_list.pop() # Remove and return the last element print(last_element) # 5 print(my_list) # [1, 2, 4]