python Hex string to array - python

I have a string
s='0xbb06e6cf,0xbb6fceb1,0xbabb39c3'
and first I want to convert it to array like
arr = [0xbb06e6cf,0xbb6fceb1,0xbabb39c3]
and then change the arr to float64 arr, which is the fastest way to convert the hex arr to float64?

You can do it using below code:
s='0xbb06e6cf,0xbb6fceb1,0xbabb39c3'
x=s.split(",")
print x
Output is:
['0xbb06e6cf', '0xbb6fceb1', '0xbabb39c3']

First you want to split the string on "," characters. Python implements a split() function that does this. Then you would want to convert each string returned from the split function into a number, you can use the int() function for this by specifying the base the number is in. Using list comprehension, the following code will do what you want:
s='0xbb06e6cf,0xbb6fceb1,0xbabb39c3'
arr = [int(n, base=16) for n in s.split(',')]

Related

creating list from a string and than change that new list

Hy guys this is my first time here.I am a beginner and i wantend to check how can i from a given string
(which is: string="5,93,14,2,33" ) make a list, after that to get the square of each number from the list and than to return that list (with a squared values) in to string?
input should to be:
string = "5,93,14,2,33"
output:
string = "25,8649,196,4,1089"
i tried to make new list with .split() and than to do square of each element, but i understand that i didnt convert the string with int().I just cant put all that together so i hope that you guys can help.Thanks and sorry if this question was stupid, i just started learning
Yes you started correctly.
# First, let's split into a list:
list_of_str = your_list.split(',') # '2,3,3,4,5' -> ['2','3','4','5']
# Then, with list comprehension, we transform each string into integer
# (assuming there will only be integers)
list_of_numbers = [int(number) for number in list_of_str]
Now you have your list of integers!
You can do it using the following approach:
Split the string of numbers into an array of integers
Square them
Convert them back to a string
Join them to a string
Print the output
# Input
input = "5,93,14,2,33"
# Get numbers as integers
numbers = [int(x) for x in input.split(",")]
# Stringify while squaring and join them into a string
output = ",".join([str(x ** 2) for x in numbers])
# Print the output
print(output)

I have s str variable containing list of float values, all i want to convert into numpy array with type float64 in python

s = "[-3.34 -0.34]"
s type is str and its size is 1
what is want is to convert this str into numpy float64 array into like this
s = [-3.34 -0.34]
np.array(list(map(float, s[1:-1].split())))
s[1:-1] will print out everything between the brackets as a string.
.split() will split the string at the spaces into a list of 2 strings.
map() will convert the strings into floats.
list() will convert the map object into a list.
np.array() will convert the list into a numpy array.
print(s[1:-1].split())
# "-3.34 -0.34"
print(map(float, s[1:-1].split()))
# <map object at 0x108146128>
print(list(map(float, s[1:-1].split())))
# [-3.34, -0.34]
print(np.array(list(map(float, s[1:-1].split()))))
# [-3.34 -0.34]
Not sure what you're looking for because there is no comma is your expression but, assuming that you just want to create an array, you can do this :
expr = "[-3.34, -0.34]"
s = eval(expr)

Python: Need help converting a string of hex values into numpy array

I am very much a Python beginner using Thonny and Python 3.7.7. I have strings of values that I want to convert to integers and put in a numpy array. A typical string:
print(temp)
05:01:00016043:00002F4F:00002F53:00004231:000050AA:00003ACE:00005C44:00003D3B:000064BC
temp = temp.split(":")
print(temp)
['05', '01', '00016043', '00002F4F', '00002F53', '00004231', '000050AA', '00003ACE', '00005C44', '00003D3B', '000064BC']
I want to efficiently turn this list of strings describing hexadecimal numbers into integers and put them into an numpy array (with the emphasis on efficiently!).
a = np.array([11], dtype=int)
Any suggestions? Thanks
How about a nice and tidy one-line list comprehension? For a string s:
np.array([int(hexa, base=16) for hexa in s.split(sep=":")])
This may look complicated, but the output of s.split(sep=":") is a list of string hexadecimals. Passing each one of them (each hexa) into int with base=16 converts them, as you'd like.
Apply function which converts x to int(x, 16) to every element in L
import pandas as pd
import numpy as np
L = ['05', '01', '00016043', '00002F4F', '00002F53', '00004231', '000050AA', '00003ACE', '00005C44', '00003D3B', '000064BC']
output = pd.Series(L).apply(lambda x:int(x, 16)).values
print(output)
output is
[ 5 1 90179 12111 12115 16945 20650 15054 23620 15675 25788]

Numpy: How to cast string array to float while some of the elements contain extra quoted string

I am trying to convert string to float type by the following
X = arr[:,:-1].astype(np.float32)
However, error as below is rising
ValueError: could not convert string to float: '"53"'
I know this means I have some elements with extra quote in the array.
My problem is how should I solve this. How can I convert element '"53"' into 53 inside the array?
UPDATE 1:
Here is an example to reproduce
import numpy as np
a = np.array([['12','13'],['"53"','44']])
a = a.astype(np.float32)
Try stripping the double quotes from the array, then casting to float.
Like so:
arr = np.char.strip(arr, '"')
X = arr[:,:-1].astype(np.float32)
You could also use numpy.char.replace() to perform element-wise string replace on an array of strings.
Signature: np.char.replace(a, old, new, count=None)
Docstring: For
each element in a, return a copy of the string with all occurrences
of substring old replaced by new. Calls str.replace element-wise.
import numpy as np
a = np.array([["12","13"],['"53"',"44"]])
b = np.char.replace(a, '"', '')
c = b.astype(np.float32)

Python:Can anybody explain me this one line code?

print(''.join(map(str,range(1,n+1))))
Like what is str doing here ? and how is this outputting in the single line ?i know what map and join does but still i'm not clear with this whole code
numbers_one_to_n = range(1,n+1)
numbers_as_strings = map(str, numbers_one_to_n)
numbers_joined_to_single_string = ''.join(numbers_as_strings)
print(numbers_joined_to_single_string)
print(''.join(map(str,range(1,n+1))))
You say you know what map does? The documentation says:
map(function, iterable, ...)
Return an iterator that applies function to every item of iterable, yielding the result.
So str is the function. The iterable is the range of integers (in Python 3 this is a range object`)
str returns a string object generated from its argument.
So, str is called for each integer in the range.
An alternative to map is a list comprehension, which some prefer:
print(''.join([str(i) for i in range(1,n+1)]))
'' is a separator, that used between the concatenating elements (strings in a string sequence or characters in a string). For instance:
>>> '-'.join(('foo', 'bar'))
'foo-bar'
>>> '-'.join('bar')
'b-a-r'
str is a type and the function to convert to this type.
So, map(str, list_of_integers) converts this list to a list of strings. Because map applies the function to each element of input list to get output list.
So, we have the range from 1 to (n + 1), that have been converted to the list of the strings, and then this list have been concatenated with the empty slitter ''.

Categories

Resources