Skip to content

Tag: python

Disabling sorting mechanism in pprint output

I have big dictionary which I`m printing for viewing with prettyprint, but how I can keep formatting but kill sorting mechanism in pprint? Answer Python 3.8 or newer: Use sort_dicts=False: Python 3.7 or older: You can monkey patch the pprint module. Since the 2nd output is essentiallly randomly sorted, your o…

make os.listdir() list complete paths

Consider the following piece of code: The objective is to sort the listed files based on the creation time. However since the the os.listdir gives only the filename and not the absolute path the key ie, the os.path.getctime throws an exception saying OSError: [Errno 2] No such file or directory: ‘very_i…

The Next Palindrome number

I am beginner in programming, So can you please tell me what’s wrong with my code? I want to print next palindrome number if the number entered by the user (n) is not palindrome Answer There are two problems with your code. 1) Your “for i in range” loop calculates the reverse of the temp var…

Plotting CDF of a pandas series in python

Is there a way to do this? I cannot seem an easy way to interface pandas series with plotting a CDF. Answer In case you are also interested in the values, not just the plot. This will always work (discrete and continuous distributions) Alternative example with a sample drawn from a continuous distribution or …

Python classes self.variables

I have started learning python classes some time ago, and there is something that I do not understand when it comes to usage of self.variables inside of a class. I googled, but couldn’t find the answer. I am not a programmer, just a python hobbyist. Here is an example of a simple class, with two ways of…