0

I have a list is a = ['R','R','R','B','R','B','B','S','S']. my goal is to delete repeat 'R's and 'S's and then delete the 'B's (if there is only one R or S, just keep it). Therefore, I want the output to be ['R','R','S'], but mine is ['R', 'S']. Can anyone help me take look my code? Thank you

This is my code

a = ['R','R','R','B','R','B','B','S','S']  # create a list to store R S B

a = [x for x in a if x != 'B']  # Delete all the B's

new_list = []  # create another list to store R and S without repeat
last = None
for x in a:
    if last == x and (len(new_list) == 0 or new_list[-1] != x):
        new_list.append(last)
    last = x
print(new_list)

My output is this

['R', 'S']

but I want this

['R','R','S']
Wenxuan Li
  • 37
  • 6
  • 1
    `delete every repeat R's and S's between B.`, then no `R's` should be deleted right, since there is no repeated `R` between B and S? – Devesh Kumar Singh Jun 27 '19 at 17:53
  • 1
    After re-reading this I deleted my answer below because your question doesn't actually make sense. You have no repeated R's or S's between B's in the list you start with. By this description your results should be `['R','R','R','B','R','B','B','S','S']` so either your description is wrong or your results are wrong. Please clarify. – Mark Jun 27 '19 at 18:07
  • I think my description is wrong. if there is no repeat just keep it. The output is what I want. No matter how many R B S. I just want to keep R and S just for once after delete the B. Does this make sense? – Wenxuan Li Jun 27 '19 at 18:11
  • 1
    @WenxuanLi, but you also seem to be deleting repeated R and S values that are **not** between B. I think my answer below is what you are looking for, but aren't explaining well. Maybe you should say you want to delete repetitions of R and S that are grouped together. – Mark Jun 27 '19 at 18:13
  • yes, maybe my description is confuse, but I want no repeat R and S in the end before or after B. – Wenxuan Li Jun 27 '19 at 18:16
  • It is unwise to delete the 'B's first and then try to to anything to letters that used to be between the 'B's. And as Mark Meyer stated, the description of your problem and your desired output do not align. I think you want to try deleting all repeats first, and once you have that working, then try removing all the 'B's.' –  Jun 27 '19 at 18:17

3 Answers3

9

You could use itertools.groupby to group the elements first, then delete the B values:

from itertools import groupby

a = ['R','R','R','B','R','B','S','S']  # create a list to store R S B
[k for k, v in groupby(a) if k != 'B']

Result:

['R', 'R', 'S']
Mark
  • 90,562
  • 7
  • 108
  • 148
0

You could try this. This creates a new list without anything that is a repeat, and no 'B's.

a = ['R','R','R','B','R','B','B','S','S']

new_list = []  # create another list to store R and S without repeat
last = None

for x in a:
    if last != x and x!='B':
        new_list.append(x)
    last = x
print(new_list)

Another option is to use a list comprehension:

a = ['R','R','R','B','R','B','B','S','S']
new_list = [ x for i,x in enumerate(a) if (a[i-1] != x and x!='B') or (i==0) ]
print(new_list)

Output from either example is the same:

['R', 'R', 'S']

Neither of these options require an import. However, I think the groupby code given by Mark Meyer is what I'd use in most cases.

-1

You can use fromkeys in this case.

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist) # ['a', 'b', 'c']
Aman Raheja
  • 615
  • 7
  • 16