Home > Software engineering >  I want to remove randomly appearing 'A' 'B' 'C' strings from a list
I want to remove randomly appearing 'A' 'B' 'C' strings from a list

Time:04-25

I possess the following listing results. This is the result obtained from the AB test of the article.

texts = [
    'A text',
    '89',
    '71%',
    '10%',

    'B',
    'B text',
    '110',
    '50%',
    '9%',

    'C',
    'C text',
    '30%',
    '4%'
    ]

texts2 = [
    'A'
    'A text',
    '89',
    '71%',
    '10%',

    'B',
    'B text',
    '110',
    '50%',
    '9%',

    'C text',
    '30%',
    '4%'
    ]

Only the best result in this list does not contain any of the letters 'A', 'B', or 'C'. In this list, the A result does not contain 'A'. But I am wondering what to do with the possibility of a list that appears without 'B' and 'C' as strings.

I was trying the following code now, but it does not work.

Is there a good solution?

while ('A' or 'B' or 'C') in texts:
    try:
        texts.remove('A')
        texts.remove('B')
        texts.remove('C')
    except Exception as ex:
        print(ex)

CodePudding user response:

Try this:

texts = [
    'A text',
    '89',
    '71%',
    '10%',

    'B',
    'B text',
    '110',
    '50%',
    '9%',

    'C',
    'C text',
    '30%',
    '4%'
    ]

exclude = ['A','B','C']
t = [x for x in texts if (x not in exclude)]
print(t)

Output: ['A text', '89', '71%', '10%', 'B text', '110', '50%', '9%', 'C text', '30%', '4%']

  • Related