A community for students.
Here's the question you clicked on:
 0 viewing
Kevink
 4 years ago
How can I consolidate a Tuple e.g. make (1,5,4,6,5,2,5,1) just (1,2,4,5,6)
Kevink
 4 years ago
How can I consolidate a Tuple e.g. make (1,5,4,6,5,2,5,1) just (1,2,4,5,6)

This Question is Closed

Keen
 4 years ago
Best ResponseYou've already chosen the best response.0The simple way to do it is to make a new empty tuple variable. Then have 2 nested for loops that iterate over the unconsolidated tuple, and the new tuple. If the current item in the unconsolidated tuple exists in the consolidated tuple, change the value of a state variable. Once the iterating over the new tuple is done, check the value of the state variable and add the value to the new tuple accordingly.

Kevink
 4 years ago
Best ResponseYou've already chosen the best response.0ok sounds good I'll try that, is there anyway to order the integers by value?

Keen
 4 years ago
Best ResponseYou've already chosen the best response.0Umm, I think it would be easiest to use some sorting algorithm on the tuple once you've got it filtered to unique values. Wikipedia has a bunch of sorting algorithm articles you can reference. Although, thinking about it more, it might be easier to first sort the values, then you can just iterate through it, checking to see if the value in the adjacent index is the same.

Aslander
 4 years ago
Best ResponseYou've already chosen the best response.0tuples are immutable, so you can't just sort them. You would have to create a new tuple, with the items already sorted...or so the documentation indicates.

Kevink
 4 years ago
Best ResponseYou've already chosen the best response.0ok finally figured this out a=(5,2,2,1,4,4,1,6,34,34,34,34,34,34,65,5,32,1,12,34,63,34,34,7,5,6) b=() c=() e=() d=0 f=0 for n in range(0,len(a)): value = a[n] d=0 f=0 for m in range(0,len(a)): for j in range(0,len(a)): for k in range(0,len(b)): if b[k]==value: f+=1 if f==1: break if a[j] >= value: d+=1 if d==len(a)m: b+=(value,) d=0 f=0 t=(len(b)) while t>0: for m in range(0,len(b)): value = b[m] for y in range(0,len(b)): if b[y] >= value: d+=1 if d==len(b): c+=(value,) f+=1 if f<1: e+=(value,) d=0 f=0 b=e e=() t=1 works if you had the same question, although it probably could be a lot simpler

Kevink
 4 years ago
Best ResponseYou've already chosen the best response.0and c is the final answer

Aslander
 4 years ago
Best ResponseYou've already chosen the best response.0Here's something a little shorter...same idea I think. :) def duplicates(): a = (1,1,1,2,2,2,3,3,3,4,4,4) b = [] c = () for i in a: if i not in b: b.append(i) for i in b: if i not in c: c = c + (i,) a = c print a

Aslander
 4 years ago
Best ResponseYou've already chosen the best response.0oh  ps. You don't need the "if i not in c:" The list b is already sorted with the unique items, and you nly need to make a tuple from the list. So it should read: def duplicates(): a = (1,1,1,2,2,2,3,3,3,4,4,4) print "Original Tuple:", a b = [] c = () for i in a: if i not in b: b.append(i) for i in b: c = c + (i,) a = c print "And the survey says!", a

Aslander
 4 years ago
Best ResponseYou've already chosen the best response.0If you want, you can also add: b.sort() after the b.append(i)

polpak
 4 years ago
Best ResponseYou've already chosen the best response.0The best solution is to use the builtin set datatype. It's an unsorted collection of unique elements. a = (1,1,1,2,3,4,2,3,1,4,5) a = tuple(sorted(set(a)))

Aslander
 4 years ago
Best ResponseYou've already chosen the best response.0Yes  SORTED will work nicely. I wasn't at that point yet, but nopw I intend to read and study a little further...there is so much to learn.

Keen
 4 years ago
Best ResponseYou've already chosen the best response.0Just ignore that last post, this webpage put it on the wrong thread.

Keen
 4 years ago
Best ResponseYou've already chosen the best response.0Nevermind, I was seeing a post here that I made in a different thread. Argh.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.