+ 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
8 odpowiedzi
+ 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.
+ 2
✩✮★✮✩ thank you! Could you explain the "*"?
+ 2
Ipang by sorted do you mean “1,2,3,4,5…” as opposed to “2,5,1,4,3…”?
+ 2
Yes Katz
That's what I meant by sorted ...
+ 2
Ipang why should it be noted?
+ 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?
+ 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
+ 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 ) : ] )