-2

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

akumar
  • 49
  • 1
  • 9
  • Is there any reason why you have a single-element list? Can we please simplify the question to *"I have this string"*? – Aran-Fey May 03 '18 at 09:13
  • Have you see this https://stackoverflow.com/questions/9671224/split-a-python-list-into-other-sublists-i-e-smaller-lists – Sylvrec May 03 '18 at 09:14
  • I'm not sure what you are after. Are you trying to produce strings formatted a certain way, or the actual list of numbers `[0,0,0,16]` as you have written? – khelwood May 03 '18 at 09:14
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check [How to Ask](https://stackoverflow.com/questions/how-to-ask). –  May 03 '18 at 09:18
  • *I want to strip off the "[" and "]"* Ok so what is the Python string function to strip specific characters. e.g. whitespace and then brackets? Or substitute ' [' with ''? You need to show a little effort, SO is not a code-writing service. Did you notice after you strip ' [' that your string of numbers has regular chunks of length 11? – smci May 03 '18 at 09:23

2 Answers2

1

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]
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
1

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]
Sruthi
  • 2,908
  • 1
  • 11
  • 25