Ace school

with brainly

  • Get help from millions of students
  • Learn from experts with step-by-step explanations
  • Level-up by helping others

A community for students.

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

MIT 6.00 Intro Computer Science (OCW)
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer

SEE EXPERT ANSWER

To see the expert answer you'll need to create a free account at Brainly

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.
ok sounds good I'll try that, is there anyway to order the integers by value?
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.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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.
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
and c is the final answer
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
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
If you want, you can also add: b.sort() after the b.append(i)
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)))
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.
Just ignore that last post, this webpage put it on the wrong thread.
Nevermind, I was seeing a post here that I made in a different thread. Argh.

Not the answer you are looking for?

Search for more explanations.

Ask your own question