Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

How can I consolidate a Tuple e.g. make (1,5,4,6,5,2,5,1) just (1,2,4,5,6)

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

ok sounds good I'll try that, is there anyway to order the integers by value?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

and c is the final answer

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

If you want, you can also add: b.sort() after the b.append(i)

OpenStudy (anonymous):

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)))

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

Just ignore that last post, this webpage put it on the wrong thread.

OpenStudy (anonymous):

Nevermind, I was seeing a post here that I made in a different thread. Argh.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!