+ 1
Tuple & int comparisons in python3
How to syntax-express a tuple & int comparisons in python3? Please help
16 Answers
+ 2
Well try:
items = [
("product1", 10),
("product2", 9),
("product3", 12),
]
filterers = []
for filterer in items:
if filterer[1] >= 10:
filterers.append(filterer)
print(filterers)
+ 3
As in, what is different between tuples and ints?
+ 2
What do you want to know
+ 2
You can't always compare integers to tuples because program does not know how do you want to compare them.
For example: (24, 8, 2, 11) > 17
It could mean:
~Length of (24, 8, 2, 11) is greater than 17.
~All the items in (24, 8, 2, 11) are greater than 17.
~Atleast one item in (24, 8, 2, 11) is greater than 17.
~Sum of (24, 8, 2, 11) is greater than 17.
Program does not simply know.
Thus programmer needs to find a way to change (24, 8, 2, 11) into integer.
Solutions for previous examples:
~len((24, 8, 2, 11)) > 17
~min((24, 8, 2, 11)) > 17
~max((24, 8, 2, 11)) > 17
~sum((24, 8, 2, 11)) > 17
(Just luck they all got solved with just builtin functions. 🍀)
+ 1
Oh wow, Seb TheS, big thanks to you, it works now
0
Well...
Prometheus,Cbr,Finn Styles:
Thanks all, integers are numbers and tuples are collections, yes I know it, but still how to pull an index spot from a tuple so I could compare it with ints... Like this:
Items = ("Price 7", 25)
Pricings = [ ]
For pricing in items:
If pricing >= 25:
Pricings.append(pricing [1])
Print(Pricings)
The above code cannot execute due to comparison issues, but let me say that I need that near to exact code to work, so please avoid maps and lists comprehensions... Help please
0
Seb TheS,
Thanks, but I need it to be iterable as well
Or how to get tuple's index and iterate through it?
0
Nontakeinvain .com Your question was little unclear, but here might be 2 wanted solutions:
t = (24, 8, 2, 11)
#When item indices aren't required:
for el in t:
print(el)
Output:
24
8
2
11
#When item indices are required:
for i in range(len(t)):
print(i, t[i])
Output:
0 24
1 8
2 2
3 11
0
You can perform each task I mentioned similarly for lists aswell.
0
Seb TheS,
Thanks, but I need the loop to contain a comparison of a tuple to int
If t == el:
0
You have to use index value through for loop while checking for the no. in the tuple is an integer or not,for this
For i in range(len(tuple)):
If tuplename[i]==integer:
Or..
For i in tuple:
if i==integer :
Hope this helps
0
Thanks Finn Styles,
But it doesn't work...
0
This is my original code (isn't working), which I want to preserve as is:
items = [
("product1", 10),
("product2", 9),
("product3", 12),
]
z = list(len(items))
filterers = []
for filterer in z:
if filterer >= 10:
filterers.append(filterer)
print(filterers)
0
list(len(items)) -> list(3) -> error
What you wanted to do with variable z?
What did you mean with list(len(items))
0
Seb TheS, z helps the code to be more readable.
I hoped list() would make the non-itetables iterable.
items = [
("product1", 10),
("product2", 9),
("product3", 12),
]
z = len(items)
filterers = []
for filterer in z:
if filterer >= 10:
filterers.append(filterer)
print(filterers)