如何比较具有两个条件的两个列表?

时间:2018-11-04 10:34:42

标签: python list for-loop

我正在尝试做的事情

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

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

symbol_list中,我想用normal_list中相应位置的字母替换符号,并用*替换列表中的非符号

  • 符号将始终为. \ |

预期输出:

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

解决方案是什么?

我想做什么

对于这两个列表,我都需要以某种方式访问​​似乎非常复杂的每一行中的每个项目。我试图从for循环开始:

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

这令人困惑,似乎建议最多执行两次

然后我尝试检查其中一种情况:

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

然后我将这一新行附加到new_list,但列表的打印时间是打印出来后的10倍。

new_list.append(symbol_row)

4 个答案:

答案 0 :(得分:5)

可以通过行理解和三元比较来完成:

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)])

答案 1 :(得分:1)

此代码应满足您的需求。

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()

它在symbol_list中找到字母的所有实例,并在新列表中添加一个星号

答案 2 :(得分:0)

我们可以这样写:

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

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

在这里,我们首先定义一组符号(以使其易于扩展)。接下来,我们同时遍历symbol_listnormal_list的行。然后,对于每一行,我们在这两行上进行迭代,并使用casymbol_list行的字符)作为选择器来决定我们是否填写'*'或{{1} }。

样本输入的输出为:

cb

在示例输入中,由于最后一行包含以下内容,因此最后一行有些“未对齐”:

>>> [
...   ''.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']

所以您的最后一行包含:

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

答案 3 :(得分:0)

我宁愿可读性胜于长度,

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

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



final_list = list()
for nor, sym in zip(normal_list, symbol_list):
    temp_string = ""
    for n, s in zip(nor, sym):
        if s.isalpha():
            temp_string += '*'
        else:
            temp_string += n
    final_list.append(temp_string)

print(final_list)

# output,
['ABCD*********EFGHI**', '**************KLMNOPQ', 'RSTU****VWX****eZAB']