How can i Convert 110_181 to 11.0.181 - python

What is the easier way to convert 110_181 to 11.0.181?
I was able to convert 110_181 to 11.0.181 using following str.replace function.
.replace("_",".")
How can I convert 110.181 to 11.0.181?

If string size and format will be same always, you can use slice operation and str.join:
>>> s = '110_181'
>>> '.'.join((s[:2], s[2], s[4:]))
'11.0.181'

Related

Python Converting A String to binary then an INT

I am working on an IPV4 breakdown where I have the necessary values in a string variable to represent the binary
(example: 00000000.00000000.00001111.11111111) This is a string
I need a way to turn this string into binary to then properly convert it to it's proper integer value
(in this case 0.0.15.255)
I've seen posts asking about something similar but attempting to apply it to what I'm working on has been unsuccessful
Apologies if this made no sense this is my first time posing a question here
You can achieve this using int() with base argument.
You can know more about int(x,base) - here
Split the string at '.' and store it in a list lst
For every item in lst, convert the item (binary string) to decimal using int(item, base=2) and then convert it into string type.
Join the contents of lst using .
s = '00000000.00000000.00001111.11111111'
lst = s.split('.')
lst = [str(int(i,2)) for i in lst]
print('.'.join(lst))
# Output
0.0.15.255
First split the string on . then convert each to integer equivalent of the binary representation using int builtin passing base=2, then convert to string, finally join them all on .
>>> text = '00000000.00000000.00001111.11111111'
>>> '.'.join(str(int(i, base=2)) for i in text.split('.'))
# output
'0.0.15.255'
You should split the data, convert and combine.
data = "00000000.00000000.00001111.11111111"
data_int = ".".join([str(int(i, 2)) for i in data.split(".")])
print(data_int) # 0.0.15.255
Welcome! Once you have a string like this
s = '00000000.00000000.00101111.11111111'
you may get your integers in one single line:
int_list = list(map(lambda n_str: int(n_str, 2), s.split('.')))

How can I convert bytes to string In Tensorflow

I have a list of bytes and I want to convert it to a list of strings, in python I use this decode function:
x=[b'\xd8\xa8\xd9\x85\xd8\xb3\xd8\xa3\xd9\x84\xd8\xa9',
b'\xd8\xa5\xd9\x86\xd8\xb4\xd8\xa7\xd8\xa1',
b'\xd9\x82\xd8\xb6\xd8\xa7\xd8\xa1',
b'\xd8\xac\xd9\x86\xd8\xa7\xd8\xa6\xd9\x8a',
b'\xd8\xaf\xd9\x88\xd9\x84\xd9\x8a']
y=[ a.decode("utf-8") for a in x]
How can I get the same result in Tensorflow?
thank you
You can use tf.compat.as_str_any().
for i in x:
print(tf.compat.as_str_any(i))
will print list items as python strings. No need to use session.

Python convert tuple values from unicode to str

What is the best way to convert the values in tuples from unicode to string, when the tuples are in a list, can it be done without looping?
unicodedata.normalize('NKFD', x) can only take unicode, not a tuple. The dataset also includes float values.
EXAMPLE
unicode_tuple_list = [(u'text in unicode', u'more unicode'), (u'more text in unicode', u'even more unicode')]
print type(unicode_tuple_list) # list - keep as list
print type(unicode_tuple_list[0]) # tuple - keep as tuple
print type(unicode_tuple_list[0][0]) # unicode
How can all these values be made a str?
I'm not sure there is a way to convert this without using a loop/list comprehension.
I would use the map function to accomplish this, see:
unicode_tuple_list = [(u'text in unicode', u'more unicode'), (u'more text in unicode', u'even more unicode')]
string_tuple_list = [tuple(map(str,eachTuple)) for eachTuple in unicode_tuple_list]
print string_tuple_list
Unpack the tuples, convert to a string and repack.
tuple(map(str, unicode_tuple_list))

ValueError: could not convert string to float: '[231.49377550490459]'

I am trying to cast a python list into a float.
This is the problem narrowed down:
loss = ['[228.55112815111235]', '[249.41649450361379]']
print(float(loss[0]))
And results in the error:
ValueError: could not convert string to float: '[231.49377550490459]'
Can anyone help me?
Strip the brackets.
float(loss[0].replace('[', '').replace(']', ''))
You can use string slicing if there is always just one element in your string list.
loss = ['[228.55112815111235]', '[249.41649450361379]']
print(float(loss[0][1:-1]))
That is because your float value is encapsulated within brackets. And you'll get ValueError because that is not valid float value. In order to convert it, you have to firstly remove them. For example:
>>> my_val = '[228.55112815111235]'
>>> print float(my_val[1:-1])
228.551128151
In your case, you have to write:
>>> loss = ['[228.55112815111235]', '[249.41649450361379]']
>>> float(loss[0][1:-1])
228.55112815111235
In case you want to convert entire list to list of float, you may use map() function as:
>>> map(lambda x: float(x[1:-1]), loss)
[228.55112815111235, 249.4164945036138]
If you want to convert the list values to floats you can use list comprehension:
loss = [float(loss[i][1:-1]) for i in range(len(loss))]
Then your loss list will look like this:
[228.55112815111235, 249.4164945036138]

How to use map for converting a list of decimals to hex in python

I have list like this: a = ['12', ''34]. Now I want to convert to a list of hexadecimals by using map, how to do that?
I mean, if I tried to convert it to a list of decimals, then I would do map(int, a). Now what is its equivalent code to convert it in hex?
map(hex,map(int, a))
or perhaps
map(lambda x:int(x,16),a)
depending on what you are trying to do ...
or a list comprehension aside from #Joran's answer:
In [99]: [hex(int(i)) for i in a]
Out[99]: ['0xc', '0x22']

Categories