By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,565 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,565 IT Pros & Developers. It's quick & easy.

removing locations of characters that repeat

P: 5
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.
Expand|Select|Wrap|Line Numbers
  1. string = raw_input("Enter string->")
  2. length = len(string)
  3. indx = 0
  4. z = 0
  5. print "The length of the string is", length
  6. chars_stored = []
  7. frequencysite = []
  8. while length > indx:
  9.     if string[indx] == chars_stored:
  10.         indx = indx + 1
  11.     elif string[indx] != chars_stored:
  12.         chars_stored = chars_stored + [(string[indx])]
  13.         frequencysite = frequencysite + [z]
  14.         z = z + 1        
  15.         indx = indx + 1
  16.  
  17.  
  18. print chars_stored
  19. print frequencysite
  20.  
Oct 21 '12 #1
Share this Question
Share on Google+
8 Replies

Rabbit
Expert Mod 10K+
P: 12,441
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.
Oct 21 '12 #2

P: 5
how do i do that? (append)
Oct 21 '12 #3

Rabbit
Expert Mod 10K+
P: 12,441
You're already doing it. On line 12, you're appending to your new list.
Oct 21 '12 #4

P: 5
when i run it though it gives me the position for all the characters including the ones that repeat.
Oct 21 '12 #5

Rabbit
Expert Mod 10K+
P: 12,441
That's because you haven't fixed the comparison issue I mentioned in post #2.
Oct 21 '12 #6

P: 5
when i did change it, it gave me the location of the characters in the new list instead of the original
Oct 21 '12 #7

Rabbit
Expert Mod 10K+
P: 12,441
That's because you're appending the a variable instead of the indx variable.
Oct 21 '12 #8

P: 5
thank you!
such a silly mistake but thanks, its greatly appreaciated
Oct 21 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.