Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

Consider the below values in set:

set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])

set(['4.918859000', '0.060758000', '4.917336999', '0.003949999', '0.013945000', '10.281522000', '0.025082999'])  

I need to sort the qualities(Values) in each set in expanding order. I would prefer not to sort between the sets, yet the qualities in each set. 

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
The way to sort the values within each set in ascending order is by using the sorted() function along with a lambda function as the key parameter. Here's an alternative example:

set1 = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])

set2 = set(['4.918859000', '0.060758000', '4.917336999', '0.003949999', '0.013945000', '10.281522000', '0.025082999'])

sorted_set1 = sorted(set1, key=lambda x: float(x))

sorted_set2 = sorted(set2, key=lambda x: float(x))

print(sorted_set1)

print(sorted_set2)

Output:

['0.000000000', '0.009518000', '0.018384000', '0.030810999', '4.918560000', '10.277200999']

['0.003949999', '0.013945000', '0.025082999', '0.060758000', '4.917336999', '4.918859000', '10.281522000']

In this approach, a lambda function is used as the key parameter in the sorted() function. The lambda function converts each value to a float before performing the sorting. This ensures that the values are sorted numerically rather than as strings. By applying this approach to each set individually, you can achieve the desired result of sorting the values within each set in ascending order.
0 votes
by (26.4k points)

Through a comment,

I want to sort each set

That is simple. For any set s (or whatever else iterable), sorted(s) returns a rundown of the components of s in arranged order:

>>> s = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])

>>> sorted(s)

['0.000000000', '0.009518000', '0.018384000', '0.030810999', '10.277200999', '4.918560000']

The best arrangement is well on the way to store the numbers as numbers instead of strings in any case. However, in the event that not, you simply need to utilize a key function:

>>> sorted(s, key=float)

['0.000000000', '0.009518000', '0.018384000', '0.030810999', '4.918560000', '10.277200999']

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

0 votes
by (25.7k points)
To sort the values within each set in ascending order, you can use the sorted() function. Here's an example of how you can achieve this:

set1 = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])

set2 = set(['4.918859000', '0.060758000', '4.917336999', '0.003949999', '0.013945000', '10.281522000', '0.025082999'])

sorted_set1 = sorted(set1, key=float)

sorted_set2 = sorted(set2, key=float)

print(sorted_set1)

print(sorted_set2)

Output:

['0.000000000', '0.009518000', '0.018384000', '0.030810999', '4.918560000', '10.277200999']

['0.003949999', '0.013945000', '0.025082999', '0.060758000', '4.917336999', '4.918859000', '10.281522000']

In the above code, the sorted() function is used with the key parameter set to float. This ensures that the values are sorted based on their numeric representation rather than as strings. By applying this approach to each set individually, you can achieve the desired result of sorting the values within each set in ascending order.
0 votes
by (19k points)
You can use the sorted() function with a lambda function as the key parameter to sort the values within each set in ascending order. By converting each value to a float using the lambda function, the sorting is performed numerically. This approach ensures that the values are sorted correctly.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 22, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
asked May 9, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...