I have the following code:
matrix = [[0, 0, 1, 0], [1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 1]]
I am able to print every line as follows using this:
for i in matrix:
print(*i)
outputting:
0 0 1 0
1 1 0 0
0 0 0 1
1 0 0 1
I want to create custom boundaries for each line and I am able to do so with by manually adding the boundaries to the list of list as shown below:
for k in range(0,columns):
matrix[k].insert(0,'[')
matrix[k].insert(columns+1,']')
giving me the output as desired:
[ 0 0 1 0 ]
[ 1 1 0 0 ]
[ 0 0 0 1 ]
[ 1 0 0 1 ]
Is there a better way to do this, particularly without having to add the boundaries into my list?
Yes you can do it with two for loop like that
for i in matrix:
s = "["
for j in i:
s = s + str(j) + " "
s = s + "]"
print(s)
Or you can still do it with 1 for loop like that
for i in matrix:
print("[", *i, "]")
for row in matrhx:
print( '[ ' + ' '.join(str(j)) + ' ]' )
for row in matrix:
print(row)
almost does what you want, but it has commas. Replace those commas by nothing:
for row in matrix:
print(str(row).replace(',',''))
[0 0 1 0]
[1 1 0 0]
[0 0 0 1]
[1 0 0 1]
Even this isn't quite what your target is, but in mathematical type-setting it is not customary to pad the boundaries of a matrix with white space.
Another way with simple list to str casting and replacing all the commas with nothing like below-
matrix = [[0, 0, 1, 0], [1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 1]]
for i in matrix:
print(str(i).replace(',',''))
DEMO: https://rextester.com/QESAJC13339
Related
I know that you can move an array with NumPy so if you use np.roll you can shift array to right or to the left. I was wondering how to move a specific set of values with in the array to either left right up or down.
for example
if I wanted to move what is circled in red to the left how would i be able to move that and nothing else?
numpy can use slice to get subarray and later assing it in different place
import numpy as np
x = [
[0, 1, 2, 1, 0, 0, 1, 2, 1, 0 ],
[0, 1, 2, 1, 0, 0, 1, 2, 1, 0 ]
]
arr = np.array(x)
print(arr)
subarr = arr[0:2,1:4] # get values
print(subarr)
arr[0:2,0:3] = subarr # put in new place
print(arr)
Result:
[[0 1 2 1 0 0 1 2 1 0]
[0 1 2 1 0 0 1 2 1 0]]
[[1 2 1]
[1 2 1]]
[[1 2 1 1 0 0 1 2 1 0]
[1 2 1 1 0 0 1 2 1 0]]
It keeps original values in [0][1], [1][1]. If you want remove them then you could copy subarray, set zero in original place, and put copy in new place
import numpy as np
x = [
[0, 1, 2, 1, 0, 0, 1, 2, 1, 0 ],
[0, 1, 2, 1, 0, 0, 1, 2, 1, 0 ]
]
arr = np.array(x)
print(arr)
subarr = arr[0:2,1:4].copy() # duplicate values
print(subarr)
arr[0:2,1:4] = 0 # remove original values
arr[0:2,0:3] = subarr # put in new place
print(arr)
Result
[[0 1 2 1 0 0 1 2 1 0]
[0 1 2 1 0 0 1 2 1 0]]
[[1 2 1]
[1 2 1]]
[[1 2 1 0 0 0 1 2 1 0]
[1 2 1 0 0 0 1 2 1 0]]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
I have a piece of code that should be updating a single item in a list but instead updates the entire list.
Here is the code
a = [
[(0, 0), (3, 4)]
]
board = [[0] * 5] * 5
for solution in a:
for _x, _y in solution:
board[_x][_y] = 1
print(board)
Expected output:
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
Actual output:
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
Your problem resides in this line:
board = [[0] * 5] * 5
[[0] * 5] * 5 replicates the resulting list of [0, 0, 0, 0, 0] five times so that the reference remains the same across the board:
print([id(i) for i in board])
[59200896, 59200896, 59200896, 59200896, 59200896]
This makes it impossible to make the change individually.
Instead, do this:
board = [[0] * 5 for _ in range(5)]
print([id(i) for i in board])
[45118008, 45045120, 43900680, 43899760, 45119088]
The list comprehension will create a distinct object reference for each inner list, and the objects can now be updated individually:
[
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0]
]
try the below code
a = [
[(0, 0), (3, 4)]
]
board = [x[:] for x in [[0] * 5] * 5]
for solution in a:
for _x, _y in solution:
board[_x][_y] = 1
print(board)
The following code fills a 2d array (grid) with random numbers(0 or 1):
def create_initial_grid(rows, cols):
grid = []
for row in range(rows):
grid_rows = []
for col in range(cols):
if random.randint(0, 7) == 0:
grid_rows += [1]
else:
grid_rows += [0]
grid += [grid_rows]
return grid
I want to fill the grid from a text file that looks like this:
7
0,0,0,0,0,0,0
0,0,1,0,1,0,0
0,0,1,1,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,0,0,0,0,0,0
Other option is to use numpy.loadtxt to read .txt (since you are use the array and matrix format):
data = np.loadtxt("text.txt", delimiter=",",dtype=int , skiprows=1)
print(data)
Out:
[[0 0 0 0 0 0 0]
[0 0 1 0 1 0 0]
[0 0 1 1 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 0 0 0 0 0 0]]
Note:
skiprows=1 # parameter for skipping the first line when reading from
the file.
dtype=int parameter for reading in the int format (default is
float)
You can read the file, with:
with open('myfile.txt') as f:
next(f) # skip the first line
data = [list(map(int, line.strip().split(','))) for line in f]
Here next(..) will move the cursor to the next line, since the first here contains a 7.
If there is data after the lines, we might want to prevent reading that, and use:
from itertools import islice
with open('myfile.txt') as f:
n = int(next(f)) # skip the first line
data = [list(map(int, line.strip().split(','))) for line in islice(f, n)]
For both file fragments here, the result is:
>>> data
[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 1, 1, 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, 0, 0, 0, 0, 0, 0]]
filesname = "t.txt"
with open(filesname) as f:
lines = f.read().split()
n = lines[0]
data_lines = lines[1:]
data = [map(int, row.split(",")) for row in data_lines]
print(data)
Hope this helps!
For example the binary table for 3 bit:
0 0 0
0 0 1
0 1 0
1 1 1
1 0 0
1 0 1
And I want to store this into an n*n*2 array so it would be:
0 0 0
0 0 1
0 1 0
1 1 1
1 0 0
1 0 1
For generating the combinations automatically, you can use itertools.product standard library, which generates all possible combinations of the different sequences which are supplied, i. e. the cartesian product across the input iterables. The repeat argument comes in handy as all of our sequences here are identical ranges.
from itertools import product
x = [i for i in product(range(2), repeat=3)]
Now if we want an array instead a list of tuples from that, we can just pass this to numpy.array.
import numpy as np
x = np.array(x)
# [[0 0 0]
# [0 0 1]
# [0 1 0]
# [0 1 1]
# [1 0 0]
# [1 0 1]
# [1 1 0]
# [1 1 1]]
If you want all elements in a single list, so you could index them with a single index, you could chain the iterable:
from itertools import chain, product
x = list(chain.from_iterable(product(range(2), repeat=3)))
result: [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]
Most people would expect 2^n x n as in
np.c_[tuple(i.ravel() for i in np.mgrid[:2,:2,:2])]
# array([[0, 0, 0],
# [0, 0, 1],
# [0, 1, 0],
# [0, 1, 1],
# [1, 0, 0],
# [1, 0, 1],
# [1, 1, 0],
# [1, 1, 1]])
Explanation: np.mgrid as used here creates the coordinates of the corners of a unit cube which happen to be all combinations of 0 and 1. The individual coordinates are then ravelled and joined as columns by np.c_
Here's a recursive, native python (no libraries) version of it:
def allBinaryPossiblities(maxLength, s=""):
if len(s) == maxLength:
return s
else:
temp = allBinaryPossiblities(maxLength, s + "0") + "\n"
temp += allBinaryPossiblities(maxLength, s + "1")
return temp
print (allBinaryPossiblities(3))
It prints all possible:
000
001
010
011
100
101
110
111
I need to write a code to read a .txt file, which is a matrix displayed as below, and turn it into an new integer list matrix. However, I want to skip first line of this .txt file without manually deleting the file. I do not know how to do that.
I have written some code. It is able to display the matrix, but I am unable to get rid of the first line:
def display_matrix(a_matrix):
for row in a_matrix:
print(row)
return a_matrix
def numerical_form_of(a_list):
return [int(a_list[i]) for i in range(len(a_list))]
def get_scoring_matrix():
scoring_file = open("Scoring Matrix")
row_num = 0
while row_num <= NUMBER_OF_FRAGMENTS:
content_of_line = scoring_file.readline()
content_list = content_of_line.split(' ')
numerical_form = numerical_form_of(content_list[1:])
scoring_matrix = []
scoring_matrix.append(numerical_form)
row_num += 1
#print(scoring_matrix)
display_matrix(scoring_matrix)
# (Complement): row_num = NUMBER_OF_FRAGMENTS
return scoring_matrix
get_scoring_matrix()
Scoring Matrix is a .txt file:
1 2 3 4 5 6 7
1 0 1 1 1 1 1 1
2 0 0 1 1 1 1 1
3 0 0 0 1 1 1 1
4 0 0 0 0 1 1 1
5 0 0 0 0 0 1 1
6 0 0 0 0 0 0 1
7 0 0 0 0 0 0 0
The result of my code:
[1, 2, 3, 4, 5, 6, 7]
[0, 1, 1, 1, 1, 1, 1]
[0, 0, 1, 1, 1, 1, 1]
[0, 0, 0, 1, 1, 1, 1]
[0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0]
just put a scoring_file.readline() before the while loop.
I suggest using an automated tool:
import pandas
df = pandas.read_table("Scoring Matrix", delim_whitespace = True)
If you insist doing it yourself, change the while loop;
while row_num <= NUMBER_OF_FRAGMENTS:
content_of_line = scoring_file.readline()
if row_num == 0:
content_of_line = scoring_file.readline()