removing locations of characters that repeat

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amankillz2346
    New Member
    • Oct 2012
    • 5

    #1

    removing locations of characters that repeat

    Hello, my program is supposed to print the length of the string inputed, print put characters into a list only if they dont repeat, then print a list of the location of those characters. I am sure there are many ways to do this however, this is the only way i could think of, just setting another variable and adding that variable to a list, however after adding it i need to find out a way to delete the locations of the characters that are duplicate. Thanks.
    Code:
    string = raw_input("Enter string->")
    length = len(string)
    indx = 0
    z = 0
    print "The length of the string is", length
    chars_stored = []
    frequencysite = []
    while length > indx:
        if string[indx] == chars_stored:
            indx = indx + 1
        elif string[indx] != chars_stored:
            chars_stored = chars_stored + [(string[indx])]
            frequencysite = frequencysite + [z]
            z = z + 1        
            indx = indx + 1
        
        
    print chars_stored
    print frequencysite
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If you're trying to see if an element exists in an array, you use the 'in' operator, not the == operator. As far as deleting the repeats, you don't need to, just don't append to the new list you're building.

    Comment

    • amankillz2346
      New Member
      • Oct 2012
      • 5

      #3
      how do i do that? (append)

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You're already doing it. On line 12, you're appending to your new list.

        Comment

        • amankillz2346
          New Member
          • Oct 2012
          • 5

          #5
          when i run it though it gives me the position for all the characters including the ones that repeat.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            That's because you haven't fixed the comparison issue I mentioned in post #2.

            Comment

            • amankillz2346
              New Member
              • Oct 2012
              • 5

              #7
              when i did change it, it gave me the location of the characters in the new list instead of the original

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                That's because you're appending the a variable instead of the indx variable.

                Comment

                • amankillz2346
                  New Member
                  • Oct 2012
                  • 5

                  #9
                  thank you!
                  such a silly mistake but thanks, its greatly appreaciated

                  Comment

                  Working...