Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
Kevink
Group Title
How can I consolidate a Tuple e.g. make (1,5,4,6,5,2,5,1) just (1,2,4,5,6)
 3 years ago
 3 years ago
Kevink Group Title
How can I consolidate a Tuple e.g. make (1,5,4,6,5,2,5,1) just (1,2,4,5,6)
 3 years ago
 3 years ago

This Question is Closed

Keen Group TitleBest ResponseYou've already chosen the best response.0
The 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.
 3 years ago

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

Keen Group TitleBest ResponseYou've already chosen the best response.0
Umm, 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.
 3 years ago

Aslander Group TitleBest ResponseYou've already chosen the best response.0
tuples 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.
 3 years ago

Kevink Group TitleBest ResponseYou've already chosen the best response.0
ok 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
 3 years ago

Kevink Group TitleBest ResponseYou've already chosen the best response.0
and c is the final answer
 3 years ago

Aslander Group TitleBest ResponseYou've already chosen the best response.0
Here'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
 3 years ago

Aslander Group TitleBest ResponseYou've already chosen the best response.0
oh  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
 3 years ago

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

polpak Group TitleBest ResponseYou've already chosen the best response.0
The 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)))
 3 years ago

Aslander Group TitleBest ResponseYou've already chosen the best response.0
Yes  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.
 3 years ago

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

Keen Group TitleBest ResponseYou've already chosen the best response.0
Nevermind, I was seeing a post here that I made in a different thread. Argh.
 3 years ago
See more questions >>>
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.