Present ndarray of interger elements as ndarray of arrays - python

I'm having problems with vectorized function application to ndarrays.
What is a good and working way to do this?
Input:
y_train
array([0, 0, 2, 1, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2,
1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 1, 2, 2, 2, 0, 2, 1, 0,
0, 0, 1, 2, 0, 2, 2, 1, 2, 2, 1, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 0,
0, 1, 1, 1, 0, 2, 0, 1, 1, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 1,
2, 0, 1, 0, 0, 1, 2, 2, 2, 0, 1, 1, 2, 0, 1, 0, 0, 2, 2, 0, 2, 0,
2, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0])
Desired Output:
array([[0,0],
[0,0],
[0,1],
[1,0],
...
..])
I have:
def func(x):
return np.array([int(x) for x in list(np.binary_repr(x,width=2,))])
func(y_train)
TypeError Traceback (most recent call last)
<ipython-input-178-ca45ba935147> in <module>
TypeError: only integer scalar arrays can be converted to a scalar index

Based on the additional chat conversation, it looks like you want to convert the (130,1) shaped array to a (130,2) shaped array where you want to replace 0 with [0,0], 1 with [1,0], and 2 with [0,1].
To do this, create a dictionary, then lookup the dictionary, and replace each element in y_train with the dictionary value.
The code is as follows:
y_train = [0, 0, 2, 1, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2,
1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 1, 2, 2, 2, 0, 2, 1, 0,
0, 0, 1, 2, 0, 2, 2, 1, 2, 2, 1, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 0,
0, 1, 1, 1, 0, 2, 0, 1, 1, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 1,
2, 0, 1, 0, 0, 1, 2, 2, 2, 0, 1, 1, 2, 0, 1, 0, 0, 2, 2, 0, 2, 0,
2, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0]
d = {0:[0,0],1:[1,0],2:[0,1]}
arr = [d[i] for i in y_train]
print (arr)
The output of this will be:
[[0, 0], [0, 0], [0, 1], [1, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 0], [0, 0], [1, 0], [0, 1], [1, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [0, 0], [1, 0], [0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [1, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 1], [0, 1], [0, 1], [0, 0], [0, 1], [1, 0], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [0, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [1, 0], [1, 0], [0, 1], [0, 0], [0, 0], [0, 0], [1, 0], [1, 0], [1, 0], [0, 0], [0, 1], [0, 0], [1, 0], [1, 0], [0, 1], [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 0], [0, 1], [0, 1], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [0, 0], [1, 0], [0, 0], [0, 0], [1, 0], [0, 1], [0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [0, 1], [0, 0], [1, 0], [0, 0], [0, 0], [0, 1], [0, 1], [0, 0], [0, 1], [0, 0], [0, 1], [1, 0], [1, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [1, 0], [1, 0], [1, 0], [1, 0], [0, 0], [1, 0], [1, 0], [0, 0], [0, 0], [0, 0]]
You can also achieve this using list(map(d.get, y_train)) where d is the dictionary with the lookup values.
Looks like you want this to be a two 2 column array.
import numpy as np
y_train = [0, 0, 2, 1, 2, 0, 2, 1, 0, 0, 1, 2, 1, 0, 0, 0, 2, 0, 2, 1, 2, 2,
1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 1, 2, 2, 2, 0, 2, 1, 0,
0, 0, 1, 2, 0, 2, 2, 1, 2, 2, 1, 2, 2, 2, 0, 1, 1, 1, 1, 2, 0, 0,
0, 1, 1, 1, 0, 2, 0, 1, 1, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 1,
2, 0, 1, 0, 0, 1, 2, 2, 2, 0, 1, 1, 2, 0, 1, 0, 0, 2, 2, 0, 2, 0,
2, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0]
ln = len(y_train) #find the length of the list
arr = np.array(y_train) #convert y_train to numpy array
arr1 = arr.reshape(ln//2,2) #convert it to length/2 for rows, 2 for columns
print (arr1)
The output of this will be:
[[0 0]
[2 1]
[2 0]
[2 1]
[0 0]
[1 2]
......
[2 0]
[0 0]
[1 1]
[1 1]
[0 1]
[1 0]
[0 0]]

Related

Concatenate all 2 dimensional values in a dictionary. (Output is Torch tensor)

I want to concatenate all 2 dimensional values in a dictionary.
The number of rows of these values is always the same.
D = {'a': [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
'b': [[1, 1],
[1, 1],
[1, 1]],
'c': [[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]]
}
And the output must be form of a torch tensor.
tensor([[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2]])
Any help would be appreciated!!
import torch
print(torch.cat(tuple([torch.tensor(D[name]) for name in D.keys()]), dim=1))
Output:
tensor([[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2]])
from itertools import chain
l = []
for i in range(len(D)):
t = [ D[k][i] for k in D ]
l.append( list(chain.from_iterable(t)) )
Output:
[[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2]]

Python pandas cumsum with reset by value in another column

I've got success/failure data on several simulations. Each simulation consists of several trials and I want a cumulative sum of the successes per simulation. Here's an example of my data:
data = pd.DataFrame([[0, 0, 0],
[0, 1, 0],
[0, 2, 1],
[0, 3, 0],
[1, 0, 1],
[1, 1, 0],
[1, 2, 0],
[1, 3, 1],
[2, 0, 0],
[2, 1, 1],
[2, 2, 1],
[2, 3, 1],
[0, 0, 0],
[0, 1, 1],
[0, 2, 1],
[0, 3, 0]],
columns=['simulation', 'trial', 'success'])
Using this answer, I came up with the following code but it isn't quite working and I can't figure out why.
cumsum = data['success'].cumsum()
reset = -cumsum[data['trial'] == 0].diff().fillna(cumsum)
data['cumsum'] = data['success'].where(data['trial'] != 0, reset).cumsum()
The resulting column is [0, 0, 1, 1, -1, -1, -1, 0, -1, 0, 1, 2, -1, 0, 1, 1] but I expect [0, 0, 1, 1, 1, 1, 1, 2, 0, 1, 2, 3, 0, 1, 2, 2]
You can do groupby 'simulation' & then cumsum the 'success'.
data.groupby(data.simulation.ne(data.simulation.shift()).cumsum())['success'].cumsum()
or
data.groupby((data.simulation!=data.simulation.shift()).cumsum())['success'].cumsum()

How to set 0-1 matrix using a vector of indices using numpy?

Matrix A:
A = np.array([[3, 0, 0, 8, 3],
[9, 3, 2, 2, 6],
[5, 5, 4, 2, 8],
[3, 8, 7, 1, 2],
[3, 9, 1, 5, 5]])
Matrix B: values in each row means the index of each row in matrix A.
B = np.array([[1, 2],
[3, 4],
[1, 3],
[0, 1],
[2, 3]])
We will set values in A whose index are in B to 1, others to 0.
Then the result will be:
A = np.array([[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[0, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 1, 0]])
I don't want to use for loop, how can I do it with numpy?
We can index using arrays. For axis0, we just make a range for 0-len(B) to cover each row. Then for axis1, we transpose B to represent all the column indices we want to access.
>>> C = np.zeros_like(A)
>>> C[np.arange(len(B)), B.T] = 1
>>> C
array([[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[0, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 1, 0]])
>>> B = np.array([[1, 2],
... [3, 4],
... [1, 3],
... [0, 1],
... [2, 3]])
Convenient but a bit wasteful
>>> np.identity(5,int)[B].sum(1)
array([[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[0, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 1, 0]])
More economical but also more typing
>>> out = np.zeros((5,5),int)
>>> out[np.c_[:5],B] = 1
>>> out
array([[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[0, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 1, 0]])

Python - Shift/Delete Values in a 2-Dimensional Array

I need help with shifting and deleting elements in a 2-dimensional array.
If the value in a list is negative and their is a list above it with positive values in the same location. It should shift everything down, causing the negative values to disappear.
If there isn't any list above it or the corresponding values in the list above are just 0. It will replace the negative values with 0.
Scenario 1, 3, and 4 are working! But Scenario 2 doesn't work. (I hope I covered all possible scenarios in my examples)
Note: The positive values should never disappear, they can only move down when needed. Only the negative values (below -100) disappear.
These examples should explain it better:
Scenario 1: # This Works
DATA
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 3, 1, 0, 0],
[-102, -102, -102, 0, 0],
[ 3, 1, 3, 0, 0]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[3, 1, 3, 0, 0]]
Scenario 2: # Doesn't Work
DATA
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0],
[ 2, 1, 2, 0, 0],
[-103, -103, -103, 0, 0]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0]]
The Current Output (Incorrect):
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0],
[0, 0, 0, 0, 0]]
Scenario 3: # This Works
DATA
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, -101, -101, -101],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
Scenario 4: # This Works
DATA
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[-101, 2, 2, 3, 4],
[-101, 1, 2, 3, 2],
[-101, 3, 3, 2, 3]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 3, 4],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
Here is my code - It's definitely overcomplicated for what I am trying to achieve but I have been stuck on this and can't figure out any alternative. (I can't import any outside function...so no numpy)
def move(data):
# PART 1
'''
Creates a list that contains a tuple (row, coll) of the location where a negative value (> -100) appears on the list.
'''
rows = len(data)
c_count = 4
row_list = []
while c_count >= 0:
for x in range(rows):
if data[x][c_count] < -100:
row_list.append((x, c_count))
c_count -=1
# PART 2
'''
Iterates through the list of values that contain negative value (> -100) and performs the actions listed below.
'''
for x in row_list:
row = x[0]
col = x[1]
try: # If there isn't anything above the negative value (except for 0), make all negative value (>-100) == 0. EXAMPLE (DATA 3)
if data[row-1][col] == 0:
data[row][col] = 0
except(IndexError):
pass
try: # If a row of negative values is between a row on top and bottom that contains possitive values, then merge the values above with the negative values below it. EXAMPLE (DATA 1)
if data[row-1][col] > 0 and data[row+1][col] > 0:
c_count = 4
while c_count >= 0:
count = len(data) - 1
prev = count - 1
while count > 0 and prev >= 0:
if data[count][c_count] < -100:
while prev >= 0 and data[prev][c_count] == 0:
prev -= 1
data[count][c_count] = data[prev][c_count]
data[prev][c_count]= 0
count -= 1
prev -= 1
c_count -= 1
return data
except(IndexError):
pass
try: # If a row of negative values has nothing underneath it (at the bottom) of the list. Then push everything on the top down replacing the negative value. This isn't working!
**SCENARIO 2 Should Have Worked Here**
if data[row-1][col] > 0:
data[row][col] = 0
rows = len(data)
c_count = 4
while c_count >= 0:
for x in range(rows):
if data[x][c_count] > 0:
rows = x
break
last = rows-1
if data[last][c_count] == 0:
while last > 0:
data[last][c_count] = data[last-1][c_count]
last -= 1
data[0][c_count] = 0
c_count -= 1
except(IndexError):
pass
return data
print('Data 1') # This Works
data1 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[-102, -102, -102, 0, 0],
[3, 1, 3, 0, 0]]
print(data1, 'org')
x = move(data1)
expect1 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[3, 1, 3, 0, 0]]
print(x, 'sol')
print(expect1, 'expect')
print(data1 == expect1)
print()
print('Data 2') # Doesn't Work
data2 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0],
[-103, -103, -103, 0, 0]]
print(data2, 'org')
y = move(data2)
expect2 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0]]
print(y,'sol')
print(expect2, 'expect')
print(data2 == expect2)
print()
print('Data 3') # This Works
data3 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, -101, -101, -101],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
print(data3, 'org')
z = move(data3)
expect3 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
print(z,'sol')
print(expect3, 'expect')
print(data3 == expect3)
print()
print('Data 4') # This Works
data4 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[-101, 2, 2, 3, 4],
[-101, 1, 2, 3, 2],
[-101, 3, 3, 2, 3]]
print(data4, 'org')
a = move(data4)
expect4 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 3, 4],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
print(a,'sol')
print(expect4, 'expect')
print(data4 == expect4)
I use code from previous question
Python - Shift/Delete Elements in a 2-Dimensional Array
and it gives correct results:
I work with columns separately, not with full rows.
search in column from bottom to top
find negative value
find positive value (bigger then zero) above
if not found then put zero in place of negative
if found then move down all value above
(move above to row, above-1 to row-1, above-2 to row-2, etc.)
.
def move(data):
# work in column, not with full rows
for col in range(len(data)):
# move from bottom to top
for row in range(len(data[0])-1, -1, -1):
# check if negative value
if data[row][col] < 0:
print('debug: negative:', data[row][col])
# find positive value above
above = row-1
while above > -1 and data[above][col] <= 0:
above -= 1
# check if found positive value
if above == -1:
# put zero if not found value above
print('debug: put zero')
data[row][col] = 0
else:
# move down all values above
print('debug: move down', above+1, 'element(s)')
while above > -1:
data[row][col] = data[above][col]
data[above][col] = 0
row -= 1
above -= 1
return data
# --- function to run one scenario, display data and check result ---
def run(data, expect):
print('data:')
print('\n'.join(str(row) for row in data))
print()
result = move(data)
print()
print('result:')
print(result)
print('expect:')
print(expect)
print('expect == result:', expect == result)
print('---')
# --- scenarios ---
def scenario_B1():
DATA = [
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 3, 1, 0, 0],
[-102, -102, -102, 0, 0],
[ 3, 1, 3, 0, 0]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[3, 1, 3, 0, 0]
]
run(DATA, EXPECT)
def scenario_B2():
DATA = [
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0],
[ 2, 1, 2, 0, 0],
[-103, -103, -103, 0, 0]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0]
]
run(DATA, EXPECT)
def scenario_B3():
DATA = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, -101, -101, -101],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]
]
run(DATA, EXPECT)
def scenario_B4():
DATA = [
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[-101, 2, 2, 3, 4],
[-101, 1, 2, 3, 2],
[-101, 3, 3, 2, 3]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 3, 4],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]
]
run(DATA, EXPECT)
# --- start scenarios ---
scenario_B1()
scenario_B2()
scenario_B3()
scenario_B4()
Results:
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 3, 1, 0, 0]
[-102, -102, -102, 0, 0]
[3, 1, 3, 0, 0]
debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 2, 1, 0, 0]
[2, 1, 2, 0, 0]
[-103, -103, -103, 0, 0]
debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 2, -101, -101, -101]
[0, 1, 2, 3, 2]
[0, 3, 3, 2, 3]
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[-101, 2, 2, 3, 4]
[-101, 1, 2, 3, 2]
[-101, 3, 3, 2, 3]
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 2, 3, 4], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 2, 3, 4], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---

how to convert Node and Arc sets representation to an adjacency matrix in python

What am I doing wrong?
N=[1, 2, 3, 4, 5, 6]
A=[[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]
for i in range (len(N)):
for j in range (len(N)):
my_list1 = [i[0] for i in A]
my_list2 = [i[1] for i in A]
print my_list1
print my_list2
I am not getting this output instead im getting [1, 1, 1, 2, 2, 3, 3, 4, 5]
repeated multiply times
ADJ=[[0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0], \
[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0]]
The simplest way to approach this requires building an empty adjacency matrix first, then populating it with a single pass through A. Here's a simple example that ignores the contents of N.
#!/usr/bin/env python
def show(matrix):
for row in matrix:
print row
print
N = [1, 2, 3, 4, 5, 6]
A = [[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]
adj_target = [
[0, 1, 1, 0, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0]
]
show(adj_target)
size = len(N)
adj = [[0]*size for _ in range(size)]
#show(adj)
for u,v in A:
adj[u-1][v-1] += 1
show(adj)
print adj == adj_target
output
[0, 1, 1, 0, 1, 0]
[0, 0, 1, 1, 0, 0]
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0]
[0, 1, 1, 0, 1, 0]
[0, 0, 1, 1, 0, 0]
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0]
True

Categories