Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need help on converting the varbinary data which I acquired from sql, into int by taking 4 bits at a time.
From the code above, I acquired the results as ('0x640761075D075A0.....'). My plan is to take every 4 bits & swap it (ex: 0764 0761 07D5 and so on) and then turn every 4 bits into integers. what should I do?
thank you very much!
Try it:
from struct import pack, unpack
def convert_hex_to_int(n:int, interval:int):
splitted = [hex(n)[2:][i:i+interval] for i in range(0, len(hex(n)[2:]), interval)]
return [unpack('<H', pack('>H', int(i, 16)))[0] for i in splitted]
print(convert_hex_to_int(0x640761075D07, 4))
It will return list of int: [1892, 1889, 1885]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
How can I multiply two elements in a list?
I am reading a text file, and printing the following:
for i in range(len(listeisotoper)):
print("Isotop type:"+listeisotoper[i][0])
print("Isotopisk masse u: "+listeisotoper[i][1])
print("Naturlig forekomst: "+listeisotoper[i][2])
print("xxx"+"g/mol")
print("\n")
However i cannot fathom how i can multiply the listeisotoper[i][1] * listeisotoper[i][2]
and then have it print the number with decimal points.
Any suggestions?
It isn't clear question for me, but may be this code will help you
for i in range(len(listeisotoper)):
print("Isotop type:"+listeisotoper[i][0])
print("Isotopisk masse u: "+listeisotoper[i][1])
print("Naturlig forekomst: "+listeisotoper[i][2])
mult_res = float(listeisotoper[i][1]) * float(listeisotoper[i][2])
print(f"Mult = {mult_res:.1f}")
print("xxx"+"g/mol")
print("\n")
I am assuming your problem is multiplying strings. If so, try this:
for i in range(len(listeisotoper)):
print("Isotop type:"+listeisotoper[i][0])
print("Isotopisk masse u: "+listeisotoper[i][1])
print("Naturlig forekomst: "+listeisotoper[i][2])
print(str(float(listeisotoper[i][1])*float(listeisotoper[i][2]))+"g/mol")
print("\n")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have sequences generated by python code like this:
TTTTTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
I would like to iterate through them, and change every character with certain % to N, like 0.2%, otherwise leave it to the original character. How can i do it?
You can use random int for this, like this:
import random
for i in range(len(your_list)):
if random.randint(0,1000)<2: #0.2%chance
your_list[i] = 'N'
Use random.random() in a generator expression and join back to a string.
import random
s = "TTTTTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
p = 0.002
s2 = "".join("N" if random.random() <= p else c for c in s)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'd like to know how I can use a string like order = '8927391' (with 8 being largest and 1 being smallest) and print out an answer according to the order, for example:
Let's say if_no_is_higher(no1, no2) is a function that I have defined already to print yes if the first number is higher than the second, and no if the first number is smaller.
if_no_is_higher(8, 9)
returns
yes
because according to the order 8 is higher than 9
Create a custom order and use the index of that collection to check for your condition:
a = '8927391'
custom_order = [int(item) for item in a]
min(8, 9, key=custom_order.index)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list in python of the following form:
myList = ['r0x94', 'r0x21', 'r0x51']
I want to sort it based on the last number in each string entry of the list such that:
sorted_myList = ['r0x21', 'r0x51', 'r0x94']
The last number is not hex, rather it is decimal. How to do it?
>>> my_list = ['r0x94', 'r0x21', 'r0x51']
>>> sorted(my_list, key=lambda x: int(x.rpartition('x')[-1]))
['r0x21', 'r0x51', 'r0x94']
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I convert def stringParametre(x) x to a list so that if x is hello then it will become a list like ["H","E","l","l","O"] . Capitals don't matter .
Note that you do not have to convert to a list if all you want to do is to iterate over the characters of the string:
for c in "hello":
# do something with c
works
Building on #idjaw's comment:
def stringParametre(x):
return list(x)
Of course this will have an error if x is not a string (or other sequence type).
list(x)
OR
mylist = []
for c in x:
mylist.append(c)