Home > Enterprise >  Extract type of object inside quotation marks
Extract type of object inside quotation marks

Time:01-20

I have the following list:

l = ['AB', '27.0', '30.5', '28', '31', 'CD', '29'] 

I want to turn each numerical value into float and each string into np.nan.

My attempt so far fails because float('AB') obviously can't be computed:

result = [float(i) if isinstance(float(i), float) else np.nan for i in l]

The desired outcome is:

result = [np.nan, 27.0, 30.5, 28.0, 31.0, np.nan, 29.0]

What do you suggest that I should do?

CodePudding user response:

If the numbers are integers like in your example:

result = [ float(i) if i.isdigit() else np.nan for i in l ]

CodePudding user response:

Use the Python ideal of "It's easier to ask for forgiveness than permission" principle:

EAFP: Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

This avoids additional overheads of e.g. a regular expression search, and works with both floats and integers in your input:

def to_float(value: str):
    try:
        return float(value)
    except ValueError:
        return np.nan

result = [to_float(value) for value in l]

CodePudding user response:

Use re.search instead:

result = [float(i) if re.search(r'^\d (?:\.\d )?$', i) else np.nan for i in l]

CodePudding user response:

If I understood correctly you just need to find if a string is numeric or not. If its numeric we change to float or else we give it np.nan

so we can work the problem like this:

y = []
for x in l:
    if x.isnumeric():
       x = float(x)
       y.append(x)
    else:
        x=np.nan
        y.append(x)

EDIT:

We can also tackle the problem is a reverse manner that we check if the string is alphabet or not So the code can look like this:

z = []

for x in l:
    if x.isalpha():
        x = np.nan
        z.append(x)
    else:
        x = float(x)
        z.append(x)
print(z)

The result that I got was :

[nan, 27.0, 30.5, 28.0, 31.0, nan, 29.0]

Hope this helps

  •  Tags:  
  • Related