1

I have multiple arrays in python in the following form

array1=['8', '-9.7521970601311E-5', '1237.16', '1265.50"', 'spec2', '=', '"2', '59', '0', '9810.07016902']

I want to select the data point that ends with E-some number. In the case of array1 that data point is -9.7521970601311E-5.

The arrays are not symmetric and the data points with the number ending in E are quite random. The length of the arrays are however the same. I wanted to create a loop where it would look through each array to find out the number ending with E and store them in a separate array. How would I go about doing that? I am quite new to python and I have no idea how to even approach this problem.

0

4 Answers 4

1

Using regex and assuming that you want all items written in scientific notation:

import re 
result = [item for item in array1 if re.search("E-\d+$", item) is not None]

The $ here symbolizes the end of the string, \d+ says find one or more digits after E-.

Notice that this is stricter than other solutions, and if you think any words containing "E" could appear in your array, then you should use this stricter result.

Finally, it looks like in Python that any value less than 0.0001 is written in scientific notation, so if you had access to float values, then it would simply be

result = [item for item in array1 if item < 0.0001] 

which would be faster than searching strings.

Sign up to request clarification or add additional context in comments.

Comments

0
#Original Array (This is actually a list in python)
array1=['8', '-9.7521970601311E-5', '1237.16', '1265.50"', 'spec2', '=', '"2', '59', '0', '9810.07016902'] 

#Create an empty list to store the values containing "E"
array2=[]

#Go through each item in the list and if it contains "E" the append(add) it to the array2 list
for i in array1:
    if "E" in i:
        array2.append(i)

#print out the array2 to see what you have
print(array2)

Comments

0

you need to look for the string 'E-' for every item in the list and if the condition satisfies then store it in a different list.

array1=['E8E','EEEEEEEE', '-9.7521970601311E-5', '1237.16', '1265.50"', 'spec2', '=', '"2', '59', '0', '9810.07016902'] 
array2=[]
for i in array1:
    if "E-" in i:
        array2.append(i)
print(array2)

Comments

0

if you want the number from E to the end do this:

array2 = [] 
for item in array1:
   if item.contains("E-"):
       x = item.index("E-")
       y = item[x+1:]
       array2.append(y)
       #x+1 to go the next digit to the end excluding E-
       #x to the end including E-

Check if it endswith E-

array2 = [] 
for item in array1:
   if item.endswith("E-"):
        array2.append(item)
       #do something
    else:
      #do something or do nothing

Or if you want if the item contains a E- do this:

array2 = [] 
for item in array1:
    if item.contains("E-" ):
         array2.append(item)
        #do something
     else:
        #do something or do nothing 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.