I have a string of 1s and 0s that I need to insert into a [4] by [4] matrix, that I can then use for other things.
This is my attempt at it:
b = '0110000101101000'
m = [[], [], [], []]
for i in range(4):
for j in range(4):
m[i].append(b[i * j])
But where I expected to get
[['0', '1', '1', '0'], ['0', '0', '0', '1'], ['0', '1', '1', '0'], ['1', '0', '0', '0']
I got [['0', '0', '0', '0'], ['0', '1', '1', '0'], ['0', '1', '0', '0'], ['0', '0', '0', '1']].
Could someone point me in the right direction here?
Get paper and a pencil and write a table of what you have now vs what you want:
i j i*j desired
0 0 0 0
0 1 0 1
0 2 0 2
0 3 0 3
1 0 0 4
1 1 1 5
... up to i=3, j=3
Now you can see that i * j is not the correct index in b. Can you see what the desired index formula is?
I'd agree with #John Zwinck that you can easily figure it out but if you hate math simply do
counter = 0
for i in range(4):
for j in range(4):
m[i].append(b[counter])
counter += 1 # keep track of the overall iterations
otherwise you have to find the starting row you are in (i * columns) and add the current column index
m[i].append(b[i * 4 + j]) # i * 4 gives the overall index of the 0th element of the current row
Here is a hint: range(4) starts from 0 and ends at 3.
See the python documentation: https://docs.python.org/3.9/library/stdtypes.html#typesseq
First of all, the rule to convert coordinates to index is index = row * NRO_COLS + col. You should use i * 4 + j.
Second, you can use list comprehension:
m = [[b[i * 4 + j] for j in range(4)] for i in range(4)]
then, it can be rewritten as:
m = [[b[i + j] for j in range(4)] for i in range(0, len(b), 4)]
or
m = [list(b[i:i+4]) for i in range(0, len(b), 4)]
another alternative is to use numpy, which is a great library, specially to handle multidimensional arrays
import numpy as np
m = np.array(list(b)).reshape(4,4)
or:
print(np.array(list(b)).reshape(4, -1))
print(np.array(list(b)).reshape(-1, 4))
Related
So i want to take each value from a list in a 2d array into its own separate 2d array for use later.
I have this code:
for i in portalsAll:
for x in i:
tpinx.append(x.split(" ")[0])
tpiny.append(x.split(" ")[1])
tpoutx.append(x.split(" ")[2])
tpouty.append(x.split(" ")[3])
tpIn_x.append(tpinx)
tpIn_y.append(tpiny)
tpOut_x.append(tpoutx)
tpOut_y.append(tpouty)
and this is the 2d array i wish to take the values from:
[['0 0 1 2', '0 2 2 0', '2 2 1 0'], ['1 0 2 0', '8 0 3 0', '0 0 9 0']]
As you can see, there are spaces between the values, which i cannot delete as i took this data from a file, and is why i split it to remove the spaces.
However this code does not work and it replies for tpIn_x ,as an example,
[['0', '0', '2', '1', '8', '0'], ['0', '0', '2', '1', '8', '0']]
which is a 2d array consisting of 2 repeated lists.
My ideal output is
[['0', '0', '2'], ['1', '8', '0']]
where it would only put the data in each list from the corresponding list in the original 2d array, by the way the list sizes in the 2d array is not set to 3 so i cannot set a maximum list size.
How can i fix this? Any help is gratefully accepted.
for i in portalsAll:
tpIn_x.append([x.split()[0] for x in i])
tpIn_y.append([x.split()[1] for x in i])
tpOut_x.append([x.split()[2] for x in i])
tpOut_y.append([x.split()[3] for x in i])
I have a text file with multiple matrices like this:
4 5 1
4 1 5
1 2 3
[space]
4 8 9
7 5 6
7 4 5
[space]
2 1 3
5 8 9
4 5 6
I want to read this input file in python and store it in multiple matrices like:
matrixA = [...] # first matrix
matrixB = [...] # second matrix
so on. I know how to read external files in python but don't know how to divide this input file in multiple matrices, how can I do this?
Thank you
You can write a code like this:
all_matrices = [] # hold matrixA, matrixB, ...
matrix = [] # hold current matrix
with open('file.txt', 'r') as f:
values = line.split()
if values: # if line contains numbers
matrix.append(values)
else: # if line contains nothing then add matrix to all_matrices
all_matrices.append(matrix)
matrix = []
# do what every you want with all_matrices ...
I am sure the algorithm could be optimized somewhere, but the answer I found is quite simple:
file = open('matrix_list.txt').read() #Open the File
matrix_list = file.split("\n\n") #Split the file in a list of Matrices
for i, m in enumerate(matrix_list):
matrix_list[i]=m.split("\n") #Split the row of each matrix
for j, r in enumerate(matrix_list[i]):
matrix_list[i][j] = r.split() #Split the value of each row
This will result in the following format:
[[['4', '5', '1'], ['4', '1', '5'], ['1', '2', '3']], [['4', '8', '9'], ['7', '5', '6'], ['7', '4', '5']], [['2', '1', '3'], ['5', '8', '9'], ['4', '5', '6']]]
Example on how to use the list:
print(matrix_list) #prints all matrices
print(matrix_list[0]) #prints the first matrix
print(matrix_list[0][1]) #prints the second row of the first matrix
print(matrix_list[0][1][2]) #prints the value from the second row and third column of the first matrix
I have a pandas DF where each column represent a node and two columns an edge, as following:
import pandas as pd
df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'],
'node2': ['4', '13', '25', '38', '208', '300']})
All Nodes are Undirected, i.e. you can get from one to the other undirected_graph
I would like to group them into all connected groupes (Connectivity), as following:
df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'],
'node2': ['4', '13', '25', '38', '208', '300']
,'desired_group': ['1', '1', '2', '2', '3', '3']})
For example, the reason why the first two rows were grouped, is because its possible to get from node 2 to node 13 (through 4).
The closest question that i managed to find is this one:
pandas - reshape dataframe to edge list according to column values but to my understanding, its a different question.
Any help on this would be great, thanks in advance.
Using networkx connected_components
import networkx as nx
G=nx.from_pandas_edgelist(df, 'node1', 'node2')
l=list(nx.connected_components(G))
L=[dict.fromkeys(y,x) for x, y in enumerate(l)]
d={k: v for d in L for k, v in d.items()}
#df['New']=df.node1.map(d)
df.node1.map(d)
0 0
1 0
2 1
3 1
4 2
5 2
Name: node1, dtype: int64
If for some reason you could not use an external library, you could implement the algorithms:
import pandas as pd
def bfs(graph, start):
visited, queue = set(), [start]
while queue:
vertex = queue.pop(0)
if vertex not in visited:
visited.add(vertex)
queue.extend(graph[vertex] - visited)
return visited
def connected_components(G):
seen = set()
for v in G:
if v not in seen:
c = set(bfs(G, v))
yield c
seen.update(c)
def graph(edge_list):
result = {}
for source, target in edge_list:
result.setdefault(source, set()).add(target)
result.setdefault(target, set()).add(source)
return result
df = pd.DataFrame({'node1': ['2', '4', '17', '17', '205', '208'],
'node2': ['4', '13', '25', '38', '208', '300']})
G = graph(df[['node1', 'node2']].values)
components = connected_components(G)
lookup = {i: component for i, component in enumerate(components, 1)}
df['group'] = [label for node in df.node1 for label, component in lookup.items() if node in component]
print(df)
Output
node1 node2 group
0 2 4 1
1 4 13 1
2 17 25 3
3 17 38 3
4 205 208 2
5 208 300 2
I have the following matrix:
import pandas as pd
df_test = pd.DataFrame({'TFD' : ['AA', 'SL', 'BB', 'D0', 'Dk', 'FF'],
'Snack' : ['1', '0', '1', '1', '0', '0'],
'Trans' : ['1', '1', '1', '0', '0', '1'],
'Dop' : ['1', '0', '1', '0', '1', '1']}).set_index('TFD')
df_test = df_test.astype(int)
matrix = df_test.T.dot(df_test)
print matrix
=>>>
Dop Snack Trans
Dop 4 2 3
Snack 2 3 2
Trans 3 2 4
​What I would want to yield:
Dop-Snack 2
Snack-Trans 2
Trans-Dop 3
Thanks in advance!
Assuming there are no special requirements regarding the order of the pairs:
import itertools
for c, r in itertools.combinations(matrix.columns, 2):
print("{}-{}\t{}".format(c, r, matrix.loc[c, r]))
# Dop-Snack 2
# Dop-Trans 3
# Snack-Trans 2
https://docs.python.org/2/library/itertools.html#itertools.combinations
A B C D
2 4 5 6
4 5 3 7
3 6 7 8
I want to get A, B, C column values to array(3 x 3) and D column to another array(3 x 1).
simple brute-force method:
a33 = [[],[],[]]
a31 = []
with open('dat.txt') as f:
for ln in f:
a,b,c,d = ln.split()
a33[0] += a
a33[1] += b
a33[2] += c
a31 += d
print a33
print a31
[['2', '4', '3'], ['4', '5', '6'], ['5', '3', '7']]
['6', '7', '8']
import numpy as np
# Read the data from a file
with open('data.txt') as file:
lines = file.readlines()
# Chop of the columns
raw_data = lines[1:]
# Now fetch all the data
data_abc = []
data_d = []
for line in raw_data:
values = line.split()
data_abc.append(values[:3])
data_d.append(values[3])
# Convert to matrix
data_abc = np.asmatrix(data_abc)
data_d = np.asmatrix(data_d)
# Display the result
print('Data A B C:', data_abc)
print('Data D:', data_d)