+ 1

Tuple & int comparisons in python3

How to syntax-express a tuple & int comparisons in python3? Please help

10th Sep 2019, 3:19 PM
Nontakeinvain .com
Nontakeinvain .com - avatar
16 odpowiedzi
+ 2
Well try: items = [   ("product1", 10),   ("product2", 9),   ("product3", 12), ] filterers = [] for filterer in items:   if filterer[1] >= 10:     filterers.append(filterer) print(filterers)
11th Sep 2019, 6:59 AM
Seb TheS
Seb TheS - avatar
+ 3
As in, what is different between tuples and ints?
10th Sep 2019, 3:24 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
What do you want to know
10th Sep 2019, 3:27 PM
Finn
Finn - avatar
+ 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. 🍀)
10th Sep 2019, 4:38 PM
Seb TheS
Seb TheS - avatar
+ 1
Oh wow, Seb TheS, big thanks to you, it works now
11th Sep 2019, 7:12 AM
Nontakeinvain .com
Nontakeinvain .com - avatar
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
10th Sep 2019, 3:58 PM
Nontakeinvain .com
Nontakeinvain .com - avatar
0
Seb TheS, Thanks, but I need it to be iterable as well Or how to get tuple's index and iterate through it?
10th Sep 2019, 4:57 PM
Nontakeinvain .com
Nontakeinvain .com - avatar
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
10th Sep 2019, 5:23 PM
Seb TheS
Seb TheS - avatar
0
You can perform each task I mentioned similarly for lists aswell.
10th Sep 2019, 5:26 PM
Seb TheS
Seb TheS - avatar
0
Seb TheS, Thanks, but I need the loop to contain a comparison of a tuple to int If t == el:
11th Sep 2019, 2:18 AM
Nontakeinvain .com
Nontakeinvain .com - avatar
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
11th Sep 2019, 2:29 AM
Finn
Finn - avatar
0
Thanks Finn Styles, But it doesn't work...
11th Sep 2019, 2:56 AM
Nontakeinvain .com
Nontakeinvain .com - avatar
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)
11th Sep 2019, 2:58 AM
Nontakeinvain .com
Nontakeinvain .com - avatar
0
list(len(items)) -> list(3) -> error What you wanted to do with variable z? What did you mean with list(len(items))
11th Sep 2019, 4:35 AM
Seb TheS
Seb TheS - avatar
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)
11th Sep 2019, 5:46 AM
Nontakeinvain .com
Nontakeinvain .com - avatar