Appending to a list gives ‘int’ object has no attribute ‘append’

Traceback (most recent call last): File “/home/shinigami/prac5.py”, line 21, in a[i].append(p) AttributeError: ‘int’ object has no attribute ‘append’ Your variable a is a list of integers. When you write a[i].append(…) you’re trying to call an append method on an int type, which causes the error. Writing a.append(…) is fine because a is a list, but as soon as you index into the list, you’re dealing with with the numbers within the list, and they … Read more

Sorting and Grouping Nested Lists in Python

For the first question, the first thing you should do is sort the list by the second field using itemgetter from the operator module: Then you can use itertools’ groupby function: Now y is an iterator containing tuples of (element, item iterator). It’s more confusing to explain these tuples than it is to show code: Which prints: For the … Read more

Substitution Cipher Python

Original answer below: Please show us some sample input and output for an example. Based on your code, I can come up with the following – random.shuffle shuffles everything in place and returns None, change your makeKey to: EDIT 2: For an approach without using dicts in encryption/decryption, see below: Prints: EDIT: After some spacing issues and experimentation, I … Read more

How to run Pip commands from CMD

Little side note for anyone new to Python who didn’t figure it out by theirself: this should be automatic when installing Python, but just in case, note that to run Python using the python command in Windows’ CMD you must first add it to the PATH environment variable, as explained here. To execute Pip, first of all make sure you have it … Read more

Easy pretty printing of floats?

It’s an old question but I’d add something potentially useful: I know you wrote your example in raw Python lists, but if you decide to use numpy arrays instead (which would be perfectly legit in your example, because you seem to be dealing with arrays of numbers), there is (almost exactly) this command you said you made … Read more