3

I am trying to generate an array out of a file with numpy.genfromtxt.

File is like:

16.37.235.200|59009|514|16.37.235.153|
17.37.235.200|59009|514|18.37.235.153|

And I get an array like:

['16.37.235.200' '17.37.235.200']

But I want the array to be like that:

[16.37.235.200,17.37.235.200]
Georgy
  • 12,464
  • 7
  • 65
  • 73
QWERASDFYXCV
  • 245
  • 1
  • 6
  • 15
  • IPs are 4 numbers separated by dots. They can only be stored as strings, They aren't integers or floats, – hpaulj Oct 05 '18 at 15:17

1 Answers1

5

Here is your original array:

x = np.array(['16.37.235.200', '17.37.235.200'])

which is displayed like this when printed:

print(x)
>>> ['16.37.235.200' '17.37.235.200']

In order to display it with comma as a delimiter and without quotes around strings we can use np.array2string:

print(np.array2string(x, separator=',', formatter={'str_kind': lambda x: x}))
>>> [16.37.235.200,17.37.235.200]

I don't like that lambda x: x formatter but couldn't come up with something better to remove the quotes.


You can find more here: How to pretty-printing a numpy.array without scientific notation and with given precision?

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • Okay thank you, so i can use that to delete the quotes around the strings. Do u have a clue, how i map those IP adresses to a unique integer (injective function)? – QWERASDFYXCV Oct 05 '18 at 14:39
  • @QWERASDFYXCV See here: [Map a NumPy array of strings to integers](https://stackoverflow.com/questions/36676576/map-a-numpy-array-of-strings-to-integers) – Georgy Oct 05 '18 at 14:41
  • Ok thanks, but I have 2 arrays of strings filled with IP Adress (Source,Destination), and i want to map IP Adress A in both arrays to the same integer. Does np.unique(dataSet, return_index=True) work there also? I don't think it works, because they get the value dependent on their index. – QWERASDFYXCV Oct 05 '18 at 15:09
  • @QWERASDFYXCV You should ask a separate question about this. – Georgy Oct 05 '18 at 15:20
  • I did, at the moment im trying to get it work with np.unique(dataSet, return_inverse=True) but it gives me two arrays as return. But i just only need the array of the return_inverse. Do you know how i can get only that one array as return? – QWERASDFYXCV Oct 05 '18 at 16:06