如何在定义的函数 Python3 中用列表 2 中的所有元素替换列表 1 中的所有元素
How to replace all elements in list 1 with all elements from list 2 in defined function Python3
我有 2 个列表和现成的函数传递和打印。我可以分别用第二个列表的每个元素替换第一个列表的每个元素,但我不确定如何在一个函数中一起完成。我已经在 Whosebug 上搜索了几个小时的答案,但是这里关于这个主题的所有 python 内容都太旧了,与 python 3.6 不兼容。我希望你能在不使用任何导入方法(例如 if/elif 或其他方法)的情况下给我一个提示。这是我目前所拥有的:
def goodbadString(string):
for (a,b) in zip(strings, expectedResults):
string = string.replace(strings[0],expectedResults[0])
return string
strings = ['It has been a good and bad day', 'bad company',
'good is as good does!', 'Clovis is a big city.']
expectedResults = ['I am confused', 'goodbye', 'hello',
'hello and goodbye']
for string, expectedResult in zip(strings, expectedResults):
print('Sample string = ', string)
print('Expected result =', expectedResult)
print('Actual result =', goodbadString(string))
print()
这是预期结果(虽然不是全部结果)
您可以看到我的函数确实显示了第一个 "Sample string" 的正确结果,但现在我应该继续处理其余元素(第二个样本的实际结果 "goodbye" 等等).
我不确定你 goodbadString()
到底想做什么。这是一个尝试:
def goodbadString(string):
idx = strings.index(string)
return string.replace(strings[idx],expectedResults[idx])
Sample string = It has been a good and bad day
Expected result = I am confused
Actual result = I am confused
Sample string = bad company
Expected result = goodbye
Actual result = goodbye
Sample string = good is as good does!
Expected result = hello
Actual result = hello
Sample string = Clovis is a big city.
Expected result = hello and goodbye
Actual result = hello and goodbye
这实际上是愚蠢的...只是 return 预期的字符串而不用费心去替换任何东西:
def goodbadString(string):
return expectedResults[strings.index(string)]
我有 2 个列表和现成的函数传递和打印。我可以分别用第二个列表的每个元素替换第一个列表的每个元素,但我不确定如何在一个函数中一起完成。我已经在 Whosebug 上搜索了几个小时的答案,但是这里关于这个主题的所有 python 内容都太旧了,与 python 3.6 不兼容。我希望你能在不使用任何导入方法(例如 if/elif 或其他方法)的情况下给我一个提示。这是我目前所拥有的:
def goodbadString(string):
for (a,b) in zip(strings, expectedResults):
string = string.replace(strings[0],expectedResults[0])
return string
strings = ['It has been a good and bad day', 'bad company',
'good is as good does!', 'Clovis is a big city.']
expectedResults = ['I am confused', 'goodbye', 'hello',
'hello and goodbye']
for string, expectedResult in zip(strings, expectedResults):
print('Sample string = ', string)
print('Expected result =', expectedResult)
print('Actual result =', goodbadString(string))
print()
这是预期结果(虽然不是全部结果)
您可以看到我的函数确实显示了第一个 "Sample string" 的正确结果,但现在我应该继续处理其余元素(第二个样本的实际结果 "goodbye" 等等).
我不确定你 goodbadString()
到底想做什么。这是一个尝试:
def goodbadString(string):
idx = strings.index(string)
return string.replace(strings[idx],expectedResults[idx])
Sample string = It has been a good and bad day
Expected result = I am confused
Actual result = I am confused
Sample string = bad company
Expected result = goodbye
Actual result = goodbye
Sample string = good is as good does!
Expected result = hello
Actual result = hello
Sample string = Clovis is a big city.
Expected result = hello and goodbye
Actual result = hello and goodbye
这实际上是愚蠢的...只是 return 预期的字符串而不用费心去替换任何东西:
def goodbadString(string):
return expectedResults[strings.index(string)]