From Python's official documentation: "A profile is a set of statistics that describes how often and for how long various parts of the program executed." Python's standard library provides two implementations of the same profiling interface: cProfile and profile but I always go with cProfile for short scripts because you can invoke it at run time like … Continue reading Profiling: check how long it takes to run a Python script
Tag: built-in
Map a string in Python with enumerate
Problem: create a map of the letters and indices in a string. My first approach was to loop over the string using range(len(s)), and checking if the letter exists in the map before adding it: s = 'mozzarella' mapped_s = {} for i in range(len(s)): if s[i] in mapped_s: mapped_s[s[i]].append(i) else: mapped_s[s[i]] = [i] print(f' mapped: … Continue reading Map a string in Python with enumerate