Using numpy.reshape in python - python

I am trying to use numpy.reshape to get a matrix of 2x100. I have a list having 200 elements. Here is my code-
vec is my list containing 200 elements-
[1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 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]
Here is my code-
data=np.array(vec)
shape = ( 2, 100 )
data.reshape(shape)
print(data)
But I do not get a 2x100 matrix. This is what I get-
[1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0
1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 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]

reshape does not modify the array. It returns a new reshaped array. Use data = data.reshape(shape).

Related

Python Pandas: Json to Excel just one row exports issue

I have a issue with Python Pandas. My code is showing first element but not showing another. How can I solve this issue? I shared a screenshot. My code added just Admin parent, HAKAN MISIRLI child.
My Json:
{
"ADMİN" : {
"HAKAN MISIRLI" : [ 33333333333, 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, 0 ]
},
"AKPINAR GES" : {
"BAHRİ KAŞARCI" : [ 44444444444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
},
"Gölbaşı Ges" : {
"MOLLA AHMET ŞAHİN " : [ 555555555555, 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, 0 ]
}
}
My code:
url = "the url of my Json file "
with urllib.request.urlopen(url + ".json") as url:
data = json.loads(url.read().decode())
role = list(data.keys())[0]
name = list(data[role].keys())[0]
listson = data[role][name]
columns = ["Saha", "İsim", "TC Kimlik"] + list(range(1, len(listson)))
rows = ["Personel"]
dfson1 = pd.DataFrame([role, name] + listson).T
dfson1.columns = columns
dfson1.index = rows
dfson1.to_excel("export.xlsx")
Export from this code:
screenshot from my code export
Doesn't look like you looped over the json as it's not in a list. Here's an alternative to consider. You can go line by line to see what the code is doing.
Try:
data = '''{
"ADMİN" : {
"HAKAN MISIRLI" : [ 33333333333, 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, 0 ]
},
"AKPINAR GES" : {
"BAHRİ KAŞARCI" : [ 44444444444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
},
"Gölbaşı Ges" : {
"MOLLA AHMET ŞAHİN " : [ 555555555555, 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, 0 ]
}
}'''
dataj = json.loads(data)
df = pd.concat([pd.json_normalize(j) for j in [dataj]])
df1 = df.T.reset_index()
df1[["Saha", "İsim"]] = df1['index'].str.split('.', expand=True)
dft = pd.DataFrame(df1[0].tolist())
df_final = pd.concat([df1[["Saha", "İsim"]], dft], axis=1)
df_final.rename(columns={0:'TC Kimlik'}, inplace=True)
Ouput
Saha İsim TC Kimlik 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 ADMİN HAKAN MISIRLI 33333333333 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 0
1 AKPINAR GES BAHRİ KAŞARCI 44444444444 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 Gölbaşı Ges MOLLA AHMET ŞAHİN 555555555555 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 0

Numpy: Insert arbitrary number of zeros into matrix rows at various indices

Problem
I have a 2D array that contains a series of 0's and 1's which represent values that have been bit-packed. I need to insert an arbitrary number of 0's at arbitrary points in every row in order to pad the bit-packed values a multiple of 8 bits.
I have 3 vectors.
A vector containing indices that I want to insert zeros at
A vector containing the number of zeros that I want to insert at each point from vector 1.
A vector that contains the size of each bit-string I am padding. (Probably don't need this to solve but it could be fun!)
Example
I have a vector that contains indices to insert before: [0 6 14]
and a vector that contains the number of zeroes that I want to insert: [2 0 4]
and a vector that has the size of each bitstring I am padding: [6, 8, 4]
The aim is to insert the zeroes into each row of array as such:
[[0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1]
[0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1]
[0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0]
[0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 1]
[0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0]
[0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 1]
[0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 1 0]
[0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1]
[0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0]
[1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1]]
*Spaces added between columns to highlight insertion points.
Becomes:
| | | | | |
v v v v v v
[[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1]
[0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1]
[0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0]
[0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1]
[0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0]
[0 0 1 1 0 0 1 0 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1]]
*Arrows denote inserted 0's
I am trying the most performant way of doing this. All of the vectors/arrays are numpy arrays. I've looked into using numpy.insert but that doesn't seem do have the ability to insert multiple values at a given index. I've also thought about using numpy.hstack and then flattening, but was unable to yield the result I wanted.
Any help is greatly appreciated!
np.insert does support inserting multiple values at the same index, you just have to provide that index multiple times. So you can obtain your desired result as follows:
indices = np.array([0, 6, 14])
n_zeros = np.array([2, 0, 4])
result = np.insert(matrix,
np.repeat(indices, n_zeros),
0,
axis=1)
Formatted the matrix for you (although it might be easier to work with a contrived example):
matrix = nparray([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1]])
indices = np.array([0, 6, 14])
num_zeros = np.array([2, 0, 4])
pad = np.array([6, 8, 4])
You need to allocate a new array to do this operation. Creating zero-filled arrays in numpy is very cheap. So let's start with allocating a zero filled array with our desired output shape:
out_shape = np.array(matrix.shape)
out_shape[1] += num_zeros.sum()
zeros = np.zeros(out_shape, dtype=matrix.dtype)
Now, write matrix to continuous blocks of memory in zeros by using slices:
meta = np.stack([indices, num_zeros])
meta = meta[:, meta[1] != 0] # throw away 0 slices
slices = meta.T.ravel().cumsum()
slices = np.append(cs, zeros.shape[1]) # for convenience
prev = 0
for start, end in zip(slices[1::2], slices[2::2]):
zeros[:, slice(start,end)] = matrix[:, slice(prev, prev + end-start)]
prev = end-start
Output in zeros:
[[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1]
[0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1]
[0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0]
[0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1]
[0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0]
[0 0 1 1 0 0 1 0 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1]]
My approach would be to create a zero array up front and copy the columns into the correct locations. The indexing is a little hairy with respect to clarity, so there is probably room for improvement there.
data = np.array(
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1]])
insert_before = [0, 6, 14]
zero_pads = [0, 2, 4]
res = np.zeros((len(data), 8*len(zero_pads)), dtype=int)
for i in range(len(zero_pads)):
res[:, i*8+zero_pads[i]:(i+1)*8] = data[:, insert_before[i]:insert_before[i]+8-zero_pads[i]]
>>> res
array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1]])

how to convert PBM file to list/array?

I have the following PBM file (ASCII encoding) and I need to put each line (excluding P1,#feep.pbm,and24 7. Through a series of functions.
P1
# feep.pbm
24 7
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 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0
0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 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
I have seen several advice by changing them to list or numpy array for convinience. But the problem is how to do this in Python? I found about a similar solution here https://stackoverflow.com/questions/4270700/how-to-write-pil-image-filter-for-plain-pgm-format but it does not work on my case.
An easy way is just use list slice.Try code below:
from pprint import pprint
with open("feep.ascii.pbm", "r") as f:
lines = f.readlines()
arr = []
for line in lines[3:]:
t = list(map(int, line.strip().split()))
arr.append(t)
pprint(arr)
Or with an easy list comprehension:
from pprint import pprint
with open("feep.ascii.pbm", "r") as f:
arr = [list(map(int, line.strip().split())) for line in f.readlines()[3:]]
pprint(arr)
All of them print:
[[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, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 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]]

Mask a 2D Numpy Array by array of indexes like np.in1d for 2d arrays

np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
[0,0,15,0,9,0,0,0,0,0,0,0],
[0,0,0,0,0,18,0,0,0,0,0,0],
[0,0,0,0,27,0,20,0,0,0,0,0],
[0,0,0,0,0,20,0,10,0,0,0,0],
[0,0,0,0,0,0,0,0,8,0,0,0],
[0,0,0,0,0,0,0,14,0,14,0,0],
[0,0,0,0,0,0,0,0,12,0,25,0],
[0,0,0,0,0,0,0,0,0,0,0,11],
[0,0,0,0,0,0,0,0,0,0,15,0],
[0,0,0,0,0,0,0,0,0,0,0,7],
[0,0,0,0,0,0,0,0,0,0,0,0]])
I am trying to find how to take a numpy array like above and then in one performant operation mask it with indexes of elements I want zeroed
[0,1]
[1,4]
[4,7]
[7,8]
[8,11]
So what I am left with is
np.array(
[[0,0,0,2,0,0,0,0,0,0,0,0],
[0,0,15,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,18,0,0,0,0,0,0],
[0,0,0,0,27,0,20,0,0,0,0,0],
[0,0,0,0,0,20,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,8,0,0,0],
[0,0,0,0,0,0,0,14,0,14,0,0],
[0,0,0,0,0,0,0,0,0,0,25,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,15,0],
[0,0,0,0,0,0,0,0,0,0,0,7],
[0,0,0,0,0,0,0,0,0,0,0,0]])
Something like the functionality of np.in1d but for a 2d array? I can iterate over each element but the arrays can be truly massive so vector single operation mask would be best. Is it possible? If this is a stupid question I'm sure I'll be told!
You can directly access these indexes in the following way
indexes = [[0,1], [1,4], [4,7], [7,8], [8,11]]
indexes =zip(*indexes)
>>[(0, 1, 4, 7, 8), (1, 4, 7, 8, 11)]
a[indexes[0], indexes[1]]=0
>>
[[ 0 0 0 2 0 0 0 0 0 0 0 0]
[ 0 0 15 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 18 0 0 0 0 0 0]
[ 0 0 0 0 27 0 20 0 0 0 0 0]
[ 0 0 0 0 0 20 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 8 0 0 0]
[ 0 0 0 0 0 0 0 14 0 14 0 0]
[ 0 0 0 0 0 0 0 0 0 0 25 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 15 0]
[ 0 0 0 0 0 0 0 0 0 0 0 7]
[ 0 0 0 0 0 0 0 0 0 0 0 0]]
I think you look for this
a = np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
[0,0,15,0,9,0,0,0,0,0,0,0],
[0,0,0,0,0,18,0,0,0,0,0,0],
[0,0,0,0,27,0,20,0,0,0,0,0],
[0,0,0,0,0,20,0,10,0,0,0,0],
[0,0,0,0,0,0,0,0,8,0,0,0],
[0,0,0,0,0,0,0,14,0,14,0,0],
[0,0,0,0,0,0,0,0,12,0,25,0],
[0,0,0,0,0,0,0,0,0,0,0,11],
[0,0,0,0,0,0,0,0,0,0,15,0],
[0,0,0,0,0,0,0,0,0,0,0,7],
[0,0,0,0,0,0,0,0,0,0,0,0]])
b = np.array([[0,1],[1,4],[4,7],[7,8],[8,11]])
# get x coordinates in an array
c1 = b[:,0]
# get y coordinates in an array
c2 = b[:,1]
a[c1[:,None],c2] = 0
a
array([[ 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 27, 0, 20, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 14, 0, 14, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Displaying python 2d list without commas, brackets, etc. and newline after every row

I'm trying to display a python 2D list without the commas, brackets, etc., and I'd like to display a new line after every 'row' of the list is over.
This is my attempt at doing so:
ogm = repr(ogm).replace(',', ' ')
ogm = repr(ogm).replace('[', ' ')
ogm = repr(ogm).replace("'", ' ')
ogm = repr(ogm).replace('"', ' ')
print repr(ogm).replace(']', ' ')
This is the input:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 1, 0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0, 0, 0]]
This is the output:
"' 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 1 1 1 1 0 0 1 1 0 0 1 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1 1 0 0 0 0 '"
I'm encountering two problems:
There are stray " and ' which I can't get rid of
I have no idea how to do a newline
Simple way:
for row in list2D:
print " ".join(map(str,row))
Maybe join is appropriate for you:
print "\n".join(" ".join(str(el) for el in row) for row in ogm)
0 0 0 0 0 0 0 0 0 0
1 1 0 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 1 1
0 0 0 0 0 0 1 1 1 0
0 0 0 1 1 0 1 1 1 1
0 0 1 1 0 0 1 1 1 1
0 1 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 1 0 0
1 0 1 1 1 1 0 0 0 0
print "\n".join(" ".join(map(str, line)) for line in ogm)
If you want the rows and columns transposed
print "\n".join(" ".join(map(str, line)) for line in zip(*ogm))
for row in list2D:
print(*row)
To make the display even more readable you can use tabs or fill the cells with spaces to align the columns.
def printMatrix(matrix):
for lst in matrix:
for element in lst:
print(element, end="\t")
print("")
It will display
6 8 99
999 7 99
3 7 99
instead of
6 8 99
999 7 99
3 7 99
ogm = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 1, 0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0, 0, 0]]
s1 = str(ogm)
s2 = s1.replace('], [','\n')
s3 = s2.replace('[','')
s4 = s3.replace(']','')
s5= s4.replace(',','')
print s5
btw the " is actually two ' without any gap
i am learning python for a week. u guys have given some xcellent solutions. here is how i did it....this works too....... :)

Categories

Resources