Python: How to Get the Numbers that are Greater/Less Than x in a Tuple | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

Python: How to Get the Numbers that are Greater/Less Than x in a Tuple

Does anyone know how to get all of the numbers in a tuple that are greater/less than a specific number? I found this https://www.sololearn.com/Discuss/1619358/?ref=app , however, that was only for a specific index. Example: x=4,5,6,7,8,9 How would I get the amount of numbers that are greater than 6? ^would be 3 How would I get the numbers from it that are greater than 6? ^would be 7,8,9

17th Aug 2021, 11:00 PM
Katz321Juno
Katz321Juno - avatar
8 Réponses
+ 3
You should also note whether or not <x> was sorted (and better when no duplicate items). I guess a different approach may be used for sorted/unsorted container.
18th Aug 2021, 5:20 AM
Ipang
+ 2
✩✮★✮✩ thank you! Could you explain the "*"?
18th Aug 2021, 1:32 AM
Katz321Juno
Katz321Juno - avatar
+ 2
Ipang by sorted do you mean “1,2,3,4,5…” as opposed to “2,5,1,4,3…”?
18th Aug 2021, 3:04 PM
Katz321Juno
Katz321Juno - avatar
+ 2
Yes Katz That's what I meant by sorted ...
18th Aug 2021, 3:06 PM
Ipang
+ 2
Ipang why should it be noted?
18th Aug 2021, 3:08 PM
Katz321Juno
Katz321Juno - avatar
+ 2
Ipang then greater_than_six would equal all of the values from the tuple that are greater than 6? Or would it be how many are greater than 6?
18th Aug 2021, 3:19 PM
Katz321Juno
Katz321Juno - avatar
+ 1
You see ... When the tuple contains e.g. 100 values (and sorted sequentially), we can do this for example greater_than_six = len( x ) - x.index( 6 ) - 1
18th Aug 2021, 3:14 PM
Ipang
+ 1
Katz, If I'm not miscalculating, it counts how many ... To get a new list containing only those greater than 6 we can slice <x> like so print( x[ x.index( 6 + 1 ) : ] )
18th Aug 2021, 3:26 PM
Ipang