• Nov 18, 2022 •AustinLeath
0 likes • 5 views
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0} # How to sort a dict by value Python 3> sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))} print(sort) # How to sort a dict by key Python 3> sort = {key:mydict[key] for key in sorted(mydict.keys())} print(sort)
• Aug 12, 2024 •AustinLeath
magnitude = lambda bits: 1_000_000_000_000_000_000 % (2 ** bits) sign = lambda bits: -1 ** (1_000_000_000_000_000_000 // (2 ** bits)) print("64 bit sum:", magnitude(64) * sign(64)) print("32 bit sum:", magnitude(32) * sign(32)) print("16 bit sum:", magnitude(16) * sign(16))
• Oct 10, 2025 •AustinLeath
0 likes • 2 views
#Original def output_json_log_data_to_file(filename, record_dictionary_list): with open(filename, 'w') as outputFile: for record in record_dictionary_list: json.dump(record, outputFile) outputFile.write('\n') #Atomic def output_json_log_data_to_file(filename, record_dictionary_list): # Use atomic file operations to prevent race conditions with readers # Write to temporary file first, then atomically rename to target file tmp_filename = filename + '.tmp' with open(tmp_filename, 'w') as outputFile: for record in record_dictionary_list: json.dump(record, outputFile) outputFile.write('\n') # Atomic rename - this prevents readers from seeing partial writes shutil.move(tmp_filename, filename)
• Nov 19, 2022 •CodeCatch
# Deleting all even numbers from a list a = [1,2,3,4,5] del a[1::2] print(a)
0 likes • 6 views
# Python Program to calculate the square root num = float(input('Enter a number: ')) num_sqrt = num ** 0.5 print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
0 likes • 1 view
# Python code to find the URL from an input string # Using the regular expression import re def Find(string): # findall() has been used # with valid conditions for urls in string regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))" url = re.findall(regex,string) return [x[0] for x in url] # Driver Code string = 'My Profile: https://codecatch.net' print("Urls: ", Find(string))