Home > other >  convert a string into a list of floats
convert a string into a list of floats

Time:11-24

The string to be converted is:

strng= "0.000000000000000E 000, 2*2.400000000000000E-002  , 97*0.000000000000000E 000  ,"

which I was able to split using strng.split(',')

['  0.000000000000000E 000', ' 2*2.400000000000000E-002  ', ' 97*0.000000000000000E 000  ', '\n'] 

however ' 2*2.400000000000000E-002 ' is not a string but actually two elemts of a list and ' 97*0.000000000000000E 000 is actually 97 elements of a list.

Here is an ugly attempt to accomplish this:

  a=strng.split("=").split(',')
  lst=[]
  for item in a:
      print("intem=", item)
      i= item.split("*")
      if len(i) >1:
          print(int(i[0])*i[1])
          lst.append(int(i[0])*i[1])
      else:
           lst.append(i[0])
  print("================")
  print(lst)

What is a more elegant way to accomplish this?

CodePudding user response:

Considering

string = "0.000000000000000E 000, 2*2.400000000000000E-002  , 97*0.000000000000000E 000  ,"

You can apply the following steps:

  1. Split and strip the original string:
# Split by commas
expressions = string.split(',')

# Remove leading and trailing white space of each element
expressions = [expression.strip() for expression in expressions] 

# Remove empty elements
expressions = [expression for expression in expressions if expression]

or, alternatively,

expressions = [expression.strip() for expression in string.split(',') if expression.strip()]

This will evaluate to

['0.000000000000000E 000', '2*2.400000000000000E-002', '97*0.000000000000000E 000']
  1. Create a new list and evaluate each expression:
result = []
for expression in expressions:
    # Split by the multiplication sign
    operands = expression.split('*')

    # A single element, just add to `result`
    if len(operands) == 1:
        result.append(float(operands[0]))
        
    # Two operands, add to `result` repeatdely
    elif len(operands) == 2:
        o1, o2 = operands
        o1 = int(o1)
        o2 = float(o2)
        for _ in range(o1):
            result.append(o2)

This will evaluate to

[0.0, 0.024, 0.024, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

CodePudding user response:

Here is another way to do this with string manipulations but this is only so that you for your understanding on how it works.

string = "0.000000000000000E 000, 2*2.400000000000000E-002  , 97*0.000000000000000E 000  ,"

out = [eval(i.strip().replace('*','*[') ']' if '*' in i else '[' i.strip() ']') for i in strng.split(',') if len(i)>0]

print(out)
[[0.0], [0.024, 0.024], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
  • Related