+ 2

how to find the repeated item of a tuple? tup=(2,4,5,6,2,3,4,4,7,8,4,3,2) expecting output:4

17th Apr 2017, 1:20 PM
Chilllittle
Chilllittle - avatar
13 Respostas
+ 16
for i in tup: if tup.count(i) > 1: print('REPEATED') I think this is the shortest solution
17th Apr 2017, 6:29 PM
Gami
Gami - avatar
+ 11
print(tup.count(4)) # outputs 4 because there are 4 fours
17th Apr 2017, 3:55 PM
Gami
Gami - avatar
+ 8
This is perfect : https://code.sololearn.com/cQMR30h0jaYy/?ref=app #Don't forget to like the code. 😉😁😉
17th Apr 2017, 4:32 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 8
@Tobi Yes! I have just started learning python. Will learn every module completely. Oh! And they does not teach lambda or Linq in C# here. You will need to learn it on the MSDN. Very few programmers use these techniques. So less tutorials out there for advance programming. 😔
18th Apr 2017, 12:52 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 7
@Tobi Wow! There is also lambda expression in python too!! Lambda makes codes much smaller and efficient. I always use lambda expression in C#. 😉
18th Apr 2017, 7:37 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 6
Perhaps use a loop that would find out how many repeats a number has (maybe with an array). Then you can just find the number with the most repeats. Or use 2 nested loops, one to check number of repeats, one to go through the other loop again to see if there's a larger repeat. (easier method but O(n^2) complexity.) Edit: @mary sorry I've never done python so i wouldn't be able to help with writing the actual code.
17th Apr 2017, 2:35 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
@Cyrus: Yeah, I love all these concepts from functional programming too. Python has quite a few cool concepts like these, check out the functional programming module in the course, if you like. SoloLearn really did a great job introducing them. I will definitely look into C#, when the time has come.
18th Apr 2017, 8:52 AM
Tob
Tob - avatar
+ 2
This gives you one of the list elements, which are repeated the most. Essentially one line and not to inelegant: https://code.sololearn.com/c0p3TH5wQ3w9/?ref=app
17th Apr 2017, 6:36 PM
Tob
Tob - avatar
+ 1
Why 4 and not 2? 2 is also repeated.
17th Apr 2017, 2:02 PM
Tob
Tob - avatar
+ 1
@Tobi maybe the question wants the most repeated.
17th Apr 2017, 2:53 PM
Chilllittle
Chilllittle - avatar
0
@Rrestoring faith it's too complicated for me……
17th Apr 2017, 2:54 PM
Chilllittle
Chilllittle - avatar
0
okay,thank you all
17th Apr 2017, 10:16 PM
Chilllittle
Chilllittle - avatar
0
#This finds the repeated items in a tuple.... tup= (1,2,3,4,4,5,5,12,12,12,12,1,3,2,4, "my", "my", "hi", "hi", "hello") rep=[] for i in tup: if tup.count(i)>1: rep.append(i) else: continue for x in rep: while rep.count(x)>1: rep.remove(x) print("Repeated items:",tuple(rep))
28th Mar 2019, 5:44 PM
bunny