How to compare two lists with two conditions?

3018 views python
6

What I am specifically trying to do

symbol_list = ["....firstline.\\.||hi",
               "pythonisasnake..\....",
               "||||last.\\|line\\..."]

normal_list = ["ABCDfirstlineEFGHIJhi",
               "pythonisasnakeKLMNOPQ",
               "RSTUlastVWXYlineZABCD"]

In the symbol_list, I want to replace symbols with the letter in the corresponding position in normal_list and I want to replace the non-symbols in the list with a *

  • The symbols will always be . \ |

The expected output:

new_list = ["ABCD*********EFGHIJ**",
            "**************KLMNOPQ",
            "RSTU****VWXY****ZABCD"]

What is the solution to doing this?

What I tried to do

For both lists, I somehow needed to access every item in each row which seemed really complicated to do. I tried to start this with for loops:

for symbol_row in symbol_list:
    for symbol_item in symbol_row:
        for normal_row in normal_list:
            for normal_item in normal_row:

This was confusing and it seems as if it is recommended to do at most two

I then tried to check one of the conditions:

if symbol_item in (".","\\","|"): # because \ is an escape character
    symbol_row = symbol_row.replace(symbol_item, normal_item)

Then I appended this new row to new_list but my list ended up 10 times longer than it was before after I printed it out

new_list.append(symbol_row)

answered question

3 Answers

9

It can be done with a line comprehension and ternary comparison:

symbol_list = ["....firstline.\\.||hi",
               "pythonisasnake..\....",
               "||||last.\\|line\\..."]

normal_list = ["ABCDfirstlineEFGHIJhi",
               "pythonisasnakeKLMNOPQ",
               "RSTUlastVWXYlineZABCD"]

print([''.join([n if not s.isalpha() else '*' for n,s in zip(nor, sym)])
                    for nor, sym in zip(normal_list, symbol_list)])

posted this
9

We can write this like:

my_symbols = {'\\', '|', '.'}

[
  ''.join(('*', cb)[ca in my_symbols] for ca, cb in zip(linea, lineb))
  for linea, lineb in zip(symbol_list, normal_list)
]

here we thus first define a set of symbols (to make it easy to extend this). Next we concurrently iterate over the lines of the symbol_list and the normal_list. For each line, we then iterate over the two lines, and use ca (the character of the symbol_list line) as a selector to decide if we fill in '*' or cb for that specific character.

The output for your sample input is:

>>> [
...   ''.join(('*', cb)[ca in my_symbols] for ca, cb in zip(linea, lineb))
...   for linea, lineb in zip(symbol_list, normal_list)
... ]
['ABCD*********EFGHI**', '**************KLMNOPQ', 'RSTU****VWX****eZAB']

In your sample input there is a bit of a "misalignment" on the last line since it contains:

||||last.\|line\...
RSTUlastVWXYlineZABCD

so your last line contains:

||||last.\|line\...
RSTUlastVWXYlineZABCD
---------------------
RSTU****VWXY***eZAB

posted this
8

This code should do what you need.

symbol_list = ["....firstline.\\.||hi",
               "pythonisasnake..\....",
               "||||last.\\|line\\..."]

normal_list = ["ABCDfirstlineEFGHIJhi",
               "pythonisasnakeKLMNOPQ",
               "RSTUlastVWXYlineZABCD"]

letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"

new_list = []

for i in range(len(symbol_list)):
    new_list.append("")
    for index in range(len(symbol_list[i])):
        if letters.find(symbol_list[i][index]) != -1:
            new_list[i] += "*"
        else:
            new_list[i] += normal_list[i][index]

print(new_list)
input()

It finds all the instances of letters in the symbol_list and puts an asterisk in the new list

posted this

Have an answer?

JD

Please login first before posting an answer.