I'm trying to print a list as a table
The following code creates specified amount of rows and columns (in other words, it creates a list of a specified size).
finalgrid1 =[[0 for j in range(keyword_size)] #print rows (size of keyword)
for i in range(1 + new_grid_size)] #print column (size of ciphers)
print(finalgrid1)
The result printed is (when keyword_size = 3):
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Could someone show me how to print it in the following format.
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
I'm working on creating a Cipher encoder and decoder program as part of an assignment.
I've implemented the majority of it already - just stuck with printing it in the right format (as a table).
Btw, I am not asking you to apply any changes whatsoever, I only want to know how to print the list in the format of a table, that's all.
I appreciate your help
Try this, essentially prints each list out until there are none left to print
i = 0
while i < len(finalgrid1):
print finalgrid1[i]
i += 1
Stringify each list and join them together:
>>> grid = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>>
>>> print('\n'.join(str(x) for x in grid))
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
Related
What is the procedure to initialise an n by n boolean list in python ?
for eg: 5 by 5 boolean list
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
I am able to create it using following code , but type is showing as list not a boolean list in following code
result_list = [[0]*n for _ in range(n)]
print(type(result_list))
I want that:
num = 3
array = [0,0,0,0,0]
become this:
array = ([0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0])
I've tried this:
array = ([0,0,0,0,0],)*num
and this:
array = [[0,0,0,0,0]]*num
but when I set the values, it sets it everywhere.
num = 3
array = [[0,0,0,0,0]]*num
print(array)
array[0][0] = 1
array[1][1] = 2
print(array)
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[1, 2, 0, 0, 0], [1, 2, 0, 0, 0], [1, 2, 0, 0, 0]]
when it should be that:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[1, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 0, 0, 0]]
Use List Comprehensions to build the 2D list . The thing that you are doing is actually making 3(num) references to the same list !! To understand it much better, consider this example
>>> a=b=[5]
>>> a[0] = 6
>>> a
[6]
>>> b
[6]
So use list comprehensions, so that all the inner lists are "unique".
num = 3
array = [[0,0,0,0,0] for i in range(num)]
print(array)
array[0][0] = 1
array[1][1] = 2
print(array)
Output:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[1, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 0, 0, 0]]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
s is a list of lists of integers with all values initialized to zero. I would like to increment only the first element of the first row by one, but the following command increments the first element of every row by one. How may I achieve this?
In [6]: s = [[0]*4]*4
In [7]: s[0][0] += 1
In [8]: s
Out[8]:
[[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0]]
Okay! Thanks for the advice, the problem was in my construction of s.
If s is truly a list of lists (and not a list containing multiple references to the same list), what you did works, your issue must be elsewhere
>>> s = [[0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0]]
>>> s[0][0]
0
>>> s[0][0] = 1
>>> s
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
You made your lists "incorrectly" in the first place; each element of your list simply points to the same single list. When you update that list they all update.
Make the list of lists using code something like this instead:
s = [[0 for _ in range(5)] for _ in range(5)]
This is classical Python oversight since lists assignments are done by references not by deep copy.
For example if you constructed using this way that's where it would have gone wrong.
>>> zeros = [0,0,0,0]
>>> s = [zeros,zeros,zeros,zeros]
>>> s[0][0]+=1
>>> s
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
So while copying lists use as below
>>> s = [list(zeros), list(zeros), list(zeros), list(zeros)]
>>> s[0][0]+=1
>>> s
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
I have the following matrix generated:
matrix = [[0] * columns for i in range(rows)]
where the user defines the rows and columns in the main sequence.
Say the user entered the numbers such that rows = 5 and columns = 4. When I print the matrix, I will get the following:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Thats okay, but I would like to make it nicer, so that it would look like this:
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
I believe that you would need to use something like \n, but I'm having trouble as to how to implement it. Perhaps theres a built-in function alread that I don't know of? Any help would be appreciated.
def formattedPrint(matrix):
print "["
for i in matrix:
print(i)
print "]"
You can take a look at the pprint library built into Python. I use it in 2.7, but it is available in Python 3.
Initializing a matrix as so seems to link the rows so that when one row changes, they all change:
>>> grid = [[0]*5]*5
>>> grid
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
>>> grid[2][2] = 1
>>> grid
[[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]]
How can I avoid this?
grid = [[0]*5 for i in range(5)]
Note: [int]*5 copies the int 5 times (but when you copy an int you just copy the value). [list]*5 copies the reference to the same list 5 times. (when you copy a list you copy the reference that points to the list in memory).