I have list as below
list = [' [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10]']
I want tto strip off the "[" and "]" from the list and then divide the list into 4 elements. i.e.
output_list = [0x00000000 , 0x00000000 , 0x00000000 , 0x00000010]
Any help is appreciated.
Cheers
Step 1
l = mylist[0].strip(' []').split(' ')
Output:
['00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '10']
Step 2
parts = [l[x:x+4] for x in range(0, len(l), 4)]
Output:
[['00', '00', '00', '00'],
['00', '00', '00', '00'],
['00', '00', '00', '00'],
['00', '00', '00', '10']]
Step 3
output_list = ['0x%s' % ''.join(p) for p in parts]
Output:
['0x00000000', '0x00000000', '0x00000000', '0x00000010']
To get integers, just do it like this:
[int(op, 16) for op in output_list]
Output:
[0, 0, 0, 16]
Try this
list = [' [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10]']
#removing [ and ] and extra spaces
string=list[0]
string=string.replace("[","")
string=string.replace("]","")
string=string.replace(" ","")
output_list=["0x"+string[i:i+8] for i in range(0,len(string),8)]
print(output_list)
#['0x00000000', '0x00000000', '0x00000000', '0x00000010']
This is a simpler approach. Just take the string from your list and first remove the square brackets. Then it's just a matter of iterating through this string and adding 0x
to the beginning of each part.
If the output should be a list of ints and not strings :
output_list=[int("0x"+string[i:i+8],16) for i in range(0,len(string),8)]
#[0, 0, 0, 16]
User contributions licensed under CC BY-SA 3.0