Reading 2d array from file in Python - python

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!

Related

Boundaries when printing list of lists

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

Creating a 2D array from a one line txt file in Python

I'm attempting to read a one line text file into an array in python, but I am struggling with actually getting the file to transform into an 2D array. This is the text file:
6 4 0 0 1 0 0 0 2 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 3 0
The first number (6) represents the columns and the second number (4) represents the rows. Here is the code I have so far:
maze_1d_arr = open(sys.argv[1], 'r')
maze = []
maze_split = np.array([maze_1d_arr])
size_X = len(maze_split)
size_Y = len(maze_split[0])
maze_grid = [int(x) for x in maze_split[2:]]
maze = np.array(maze_grid).reshape(size_X, size_Y)
start = np.where(maze_split == 2)
end = np.where(maze_split == 3)
path = astar(maze, start, end)
print(path)
Sorry if this question has been asked before but I'm stumped at how to get it to work. Any help would be appreciated!
import numpy as np
x = np.array([6, 4, 0, 0, 1, 0, 0, 0, 2, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0])
print(x[2:].reshape(x[[1,0]]))
[[0 0 1 0 0 0]
[2 0 1 0 1 1]
[0 0 1 0 0 0]
[0 0 0 0 3 0]]

Loading .dat file into one list and count entities in it

I have a .dat file with the following:
0 0 0 0 1 1
1 1 0 1 0 0
0 0 1 1 0 0
0 1 1 1 1 1
0 1 0 0 0 1
1 1 0 1 0 1
I'm trying to get this all into one list and then count the number of 1's and 0s.
I've got the code so far:
with open('image.dat', 'r') as a:
for line in a:
b = [line.strip()]
print(b)
c = b.count(0)
This just gives me:
['0 0 0 0 1 1']
['1 1 0 1 0 0']
['0 0 1 1 0 0']
['0 1 1 1 1 1']
['0 1 0 0 0 1']
['1 1 0 1 0 1']
0
I'm new to coding and I've tried everything.
Thanks for helping.
You can just count the number of times the string '0' (or '1') accures within the file:
with open("image.dat", "r") as a:
print(a.read().count('0'))
To load the data into one list, you can use list.extend method, for example:
data = []
with open('image.dat', 'r') as a:
for line in a:
data.extend(map(int, line.split()))
print(data)
Prints:
[0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1]
Then:
print('Number of 0:', data.count(0))
print('Number of 1:', data.count(1))
Prints:
Number of 0: 18
Number of 1: 18
EDIT: To load the data as list of lists:
lines = []
with open('image.dat', 'r') as a:
for line in a:
line = line.strip()
if not line:
continue
lines.append(list(map(int, line.split())))
print(lines)
Prints:
[[0, 0, 0, 0, 1, 1], [1, 1, 0, 1, 0, 0], [0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 0, 0, 0, 1], [1, 1, 0, 1, 0, 1]]

In order to generate all combinations of 1's and 0's we use a simple binary table. How can I easily create this binary table in an array?

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

how to skip the first line of a file in python

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()

Categories