0

I have a lot of strings corresponding each one to the path of files. I would like to extract number in exponential format in each string.

For example, I have :

../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_7.27168772219203e-07/wm_up

and I would like to extrat the float number : 7.27168772219203e-07

I would like to avoid using the splitmethod (with _ separator).

So I tried with python regexp like but I can't find which method to use (findall, research or sub) ?

How can I achieve this in a simple or short way (independently from wm_up substring since it may be other substrings (like this wm_dw for example))?

Clarifications

I would like to extract number since I want to sort in ascending order all these long srings. I would like to use natsorted:

For example, I have initially :

../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.301510038746646e-06/wm_up
../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.301510038746646e-06/wm_dw
../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.437191487625705e-05/wm_up
../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.437191487625705e-05/wm_dw

This is the result of natsortedof array of paths : as you can see, the ascending order takes into account the first digits and not the value of float exponential number (the real value) that I would like to extract. I would like to select by the ascending order of this value.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Does this answer your question? [How to extract a floating number from a string](https://stackoverflow.com/questions/4703390/how-to-extract-a-floating-number-from-a-string) – Reznik Dec 14 '19 at 13:10
  • @Reanik I have updated my question, this was not well explained. Do you grasp the little trick i want to apply to sort the paths as a function of "real" value of exponential float number ? Regards –  Dec 14 '19 at 13:21
  • Does the only difference in those name is the float number and the `up` `dw`? – Reznik Dec 14 '19 at 13:23
  • there can be others substring like `up2`, `up3`, `dw2`, `dw3` ... etc and of course, the exponential float number varies. –  Dec 14 '19 at 13:25

1 Answers1

1

Here is the code:

l = [
'../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.301510038746646e-06/wm_up',
'../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.301510038746646e-06/wm_dw',
'../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.437191487625705e-05/wm_up',
'../../Analysis_Pk_vs_Step_BEFORE_NEW_LAUNCH_13_DECEMBRE_22h57/Archive_WP_Pk_der_3_pts_step_9.437191487625705e-05/wm_dw'
] # the input that we have
# regex from https://stackoverflow.com/a/4703508/7434857
numeric_const_pattern = '[-+]? (?: (?: \d* \. \d+ ) | (?: \d+ \.? ) )(?: [Ee] [+-]? \d+ ) ?'
rx = re.compile(numeric_const_pattern, re.VERBOSE) # compile the regex
l.sort(key=lambda x: (float(rx.findall(x)[-1]),x))
Reznik
  • 2,663
  • 1
  • 11
  • 31
  • Thanks a lot ! How can I select only the pathname and not the value ?. The tuple element `"(i, path[i])"` creates issue after in my code since I am doing further floats[i] like if `floats` was an array of floats, but it generates an error ... –  Dec 14 '19 at 14:29
  • 1
    @youpilat13 hi i update my answer :), consider mark it as currect answer and upvote if it help you – Reznik Dec 14 '19 at 14:30
  • 2
    Why do you create a second list for the sort keys? If you just pass `key=function` it works fine and only calls the function once per element. – Mark Ransom Dec 14 '19 at 14:32