Choose single float from a list

sort list of float
list of floats python
python float precision
float in python
python float type
create list of random floats python
random float python
create array of random floats python

I have a list of floats being generated starting from 0 to 1 in an increasing order. I need to do something with selected floats, for instance one near 0.25, 0.5, 0.75. But the generated floats can have any number of decimal places, and repeats, like ..........0.50001, 0.51125, 0.57466459, 0.5925, 0.5925, 0.634, ..........

I need to select only ONE from near 0.5 (any one will do) and likewise at other quarters. A made up example,

list_of_floats = my_some_function()
 for i in list_of_floats:
     if i is near 0.5:
        do_something()

I tried,

list_of_floats = my_some_function()
done_once = False
 for i in list_of_floats:
     if 0.5 < i < 0.6 and done_once is False:
        do_something()
        done_once = True

This kind of works for 0.5, but I need to do for other checkpoints (0.25, 0.75 etc) as well. There must be a better way to do this. Please help.

I'd start with a list of the checkpoints and some threshold for "near" (either at the top level or paired with each checkpoint if "near" varies for each checkpoint). You can take advantage of the data and checkpoints being sorted in the same order by only ever considering the first checkpoint in the list and popping it off the list when you get a hit:

checkpoints = [.25, .5. .75]
for i in list_of_floats:
    if abs(i - checkpoints[0]) < .1:
        do_something()
        checkpoints.pop(0)
    if not checkpoints:
        break

How to convert all items in a list to floats in Python, for an individual operation (add, multiply, subtract, divide) should be 0.5 ULP. Printing list of 10 random float numbers [104.26, 212.3, 238.17, 120.05, 160.21, 269.71, 362.45, 360.05, 311.65, 329.72] Note: In the above example, there is a chance to get duplicate float numbers in a list. let’s see how to generate a list of unique random floats. Create a list of unique random float numbers

I'm not totally sure what you're going for, but it sounds like you're looking for np.isclose. For instance, if you wanted to find all floats in your array that are within 0.01 of 0.5, you can use:

list_of_floats = np.array([0.50001, 0.51125, 0.57466459, 0.5925, 0.5925, 0.634])

# note that atol is the tolerance within which you want to select your floats
>>> list_of_floats[np.isclose(0.5, list_of_floats, atol = 0.01)]
array([0.50001])

Or, since you only want one, and any one will do, select the first:

>>> list_of_floats[np.isclose(0.5, list_of_floats, atol = 0.01)][0]
0.50001

Python, I need to do something with selected floats, for instance one near 0.25, 0.5, 0.75. But the generated floats can have any number of decimal places, and repeats,  The float property specifies how an element should float. Note: Absolutely positioned elements ignore the float property! Note: Elements after a floating element will flow around it. To avoid this, use the clear property or the clearfix hack (see example at the bottom of this page).

if you want the value closest to 0.5, then something like this might get you there:

import numpy

floats = numpy.array([0.1, 0.3, 0.48, 0.51, 0.55, 0.72, 0.8])

higher = numpy.where(floats > 0.5)
rest = numpy.where(floats[higher] < 0.6)
possibilities = floats[higher][rest]
print(min(possibilities))
>>>0.51

Floating point error, Given a list of float values, write a Python program to sort the list. Examples: Input: list = ['1.2' Output = sorted ( Input , key = lambda x: float (x)). # Printing output. Also, there are other ways to randomly select an item from a list lets see those now. Randomly select multiple choices from the list. As you know, the random.choice function only returns a single item from a list. If you want to randomly select more than one item from a list or set, I’d recommend using random.sample() or random.choice s instead.

Choose single float from a list, choice() to choose a single number from the range of numbers. Generate a list of random floats between a range. In this section, we will see how  To select only the float columns, use wine_df.select_dtypes (include = ['float']) . The select_dtypes method takes in a list of datatypes in its include parameter. The list values can be a string or a Python object. You can also use the filter method to select columns based on the column names or index labels.

Python, Floating viewports create layouts, which enable you to treat your screen like a Choose View tab ➪ Navigate 2D panel ➪ Zoom drop-down list➪Window and (​expanded) ➪ Viewport Configurations List➪ Single to display the view shown in  Madhusudan> Is it possible to convert a very long list of strings to a Madhusudan> list of floats in a single statement ? Madhusudan> I have tried float(x) and float(x[:]) but neither work.

Python Get Random Float Numbers using random and Uniform , you use floating viewports to lay out your drawing for plotting. panel⇒​Zoomdrop-down list⇒Window and choose a window around the left portion of choose View tab⇒Viewports panel⇒Viewport Configurations drop-down list⇒​Single to  Let’s discusses all different ways to select random values from a list. Method #1 : Using random.choice() This method is designed for the specific purpose of getting random number from the container and hence is the most common method to achieve this task of getting a random number from a list.

Comments
  • checkpoints.pop(0); That should do it. How will it handle repeats though?
  • Repeats in the checkpoints list? Each will be handled once (e.g. if checkpoints is [.1, .1, .5] you'll look for the first two values near to .1). Repeats in the list of floats? It'll only trigger for the first (that's why we pop the checkpoint). Repeat passes through the code? You'll have to reconstruct the checkpoints list each time, otherwise it'll be empty on subsequent passes.
  • I think you may have mixed up atol and rtol.
  • For this I will need to know the list of floats apriori I guess. But they are being generated in the code in a loop. The one I coded above is a made-up example. np.isclose is a useful find though
  • It needs knowledge of list apriori, but floats are being generated in a loop