Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18819

How to keep the format of data in a list to be unchanged after converting to array

$
0
0

I have a list called coo. the data in it is like the following:

coo: [(0.0, 1.0), (1.0, 0.0)]coo: [(0.0, 1.0), (1.0, 0.0)]coo: [(0.0, 1.0), (1.0, 0.0)]coo: [(0.0, 1.0), (1.0, 0.0)]coo: [(0.0, 1.0), (0.0, 3.0), (0.0, 5001.0), (1.0, 0.0), (1.0, 3.0), (1.0, 5001.0), (3.0, 0.0), (3.0, 1.0), (3.0, 5001.0), (5001.0, 0.0), (5001.0, 1.0), (5001.0, 3.0)]coo: [(0.0, 1.0), (0.0, 5001.0), (1.0, 0.0), (1.0, 5001.0), (3.0, 5001.0), (5001.0, 0.0), (5001.0, 1.0), (5001.0, 3.0)]coo: [(0.0, 1.0), (0.0, 5001.0), (1.0, 0.0), (1.0, 5001.0), (2.0, 3.0), (3.0, 2.0), (3.0, 5001.0), (5001.0, 0.0), (5001.0, 1.0), (5001.0, 3.0)]

After I convert it to be an array I see the data becomes like the following:

coo: [[0. 1.] [1. 0.]]coo: [[0. 1.] [1. 0.]]coo: [[0. 1.] [1. 0.]]coo: [[0. 1.] [1. 0.]]coo: [[0.000e+00 1.000e+00] [0.000e+00 3.000e+00] [0.000e+00 5.001e+03] [1.000e+00 0.000e+00] [1.000e+00 3.000e+00] [1.000e+00 5.001e+03] [3.000e+00 0.000e+00] [3.000e+00 1.000e+00] [3.000e+00 5.001e+03] [5.001e+03 0.000e+00] [5.001e+03 1.000e+00] [5.001e+03 3.000e+00]]

Here is my code:

          coo=[]          for r, c in zip(row, col):            # Get the node name corresponding to the index r            node_r = next((node for node, index in node_to_index.items() if index == r), None)            # Get the node name corresponding to the index c            node_c = next((node for node, index in node_to_index.items() if index == c), None)              coo.append((node_r, node_c))          coo_array = np.array(coo)           print('coo:',coo_array)

How can I keep the data in coo_array to be the same as in coo for example if there is 1.0 in coo, the same should exist in coo_array.

coo_array = np.array(coo,dtype=float)


Viewing all articles
Browse latest Browse all 18819

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>