根据其他两个列表在 Python 中创建列表?
Create list in Python based on two other lists?
我有一个如下所示的列表:
working_list=['one', 'two', 'three', 'four', 'five']
我想要一个新列表,如下所示:
output_list=['one or two','one or two','three','four or five','four or five']
为此,我创建了另外两个列表:
old_names=[['one','two'],['four','five']]
new_names=['one or two','four or five']
然后我尝试了:
output_list=[]
for item in working_list:
for a_list in old_names:
if item in a_list:
index=old_names.index(a_list)
new_name=new_names[index]
output_list.append(new_name)
else:
output_list.append(item)
print(output_list)
但这给了我一个输出:
['one or two', 'one', 'one or two', 'two', 'three', 'three', 'four', 'four or five', 'five', 'four or five']
关于如何解决这个问题有什么想法吗?
将不胜感激!
对于任何类型的 a->b 映射,您都应该使用字典。例如,将 old_names
和 new_names
列表替换为 old_to_new_names
字典
working_list = ['one', 'two', 'three', 'four', 'five']
old_to_new_names = {
"one": "one or two",
"two": "one or two",
"four": "four or five",
"five": "four or five",
}
output_list = [old_to_new_names.get(i, i) for i in working_list]
old_to_new_names.get
方法在字典中查找 i
,如果它不存在则 returns i
(第二个参数是默认值)
如果您仍想稍作修改就使用您的代码,但@tomjn 是可扩展的并且是最好的方法
working_list=['one', 'two', 'three', 'four', 'five']
new_names=['one or two','four or five']
output_list = []
for item in working_list:
found = False
for check in new_names:
print(item)
if check.find(item) >= 0:
found = True
output_list.append(check)
break
else:
continue
if found == False:
output_list.append(item)
print(output_list)
@tomjin 的回答是一个更好的主意,带有字典的代码清晰易读,但如果您想知道如何调整您的代码以使其工作:
您需要做的就是检查工作列表中的项目是否存在于新名称的字符串中,如果存在,则将新名称而不是它添加到输出列表中。
但是你还需要检查你是否已经在项目的位置添加了一些东西到输出列表,所以你用布尔跟踪它,当且仅当你检查了所有 new_names 和你没有添加任何内容,将其添加到输出列表中。
for item in working_list
added = False
for new in new_names:
if item in new
output_list.append(new)
added = True
if not added
output_list.append(item)
我有一个如下所示的列表:
working_list=['one', 'two', 'three', 'four', 'five']
我想要一个新列表,如下所示:
output_list=['one or two','one or two','three','four or five','four or five']
为此,我创建了另外两个列表:
old_names=[['one','two'],['four','five']]
new_names=['one or two','four or five']
然后我尝试了:
output_list=[]
for item in working_list:
for a_list in old_names:
if item in a_list:
index=old_names.index(a_list)
new_name=new_names[index]
output_list.append(new_name)
else:
output_list.append(item)
print(output_list)
但这给了我一个输出:
['one or two', 'one', 'one or two', 'two', 'three', 'three', 'four', 'four or five', 'five', 'four or five']
关于如何解决这个问题有什么想法吗?
将不胜感激!
对于任何类型的 a->b 映射,您都应该使用字典。例如,将 old_names
和 new_names
列表替换为 old_to_new_names
字典
working_list = ['one', 'two', 'three', 'four', 'five']
old_to_new_names = {
"one": "one or two",
"two": "one or two",
"four": "four or five",
"five": "four or five",
}
output_list = [old_to_new_names.get(i, i) for i in working_list]
old_to_new_names.get
方法在字典中查找 i
,如果它不存在则 returns i
(第二个参数是默认值)
如果您仍想稍作修改就使用您的代码,但@tomjn 是可扩展的并且是最好的方法
working_list=['one', 'two', 'three', 'four', 'five']
new_names=['one or two','four or five']
output_list = []
for item in working_list:
found = False
for check in new_names:
print(item)
if check.find(item) >= 0:
found = True
output_list.append(check)
break
else:
continue
if found == False:
output_list.append(item)
print(output_list)
@tomjin 的回答是一个更好的主意,带有字典的代码清晰易读,但如果您想知道如何调整您的代码以使其工作:
您需要做的就是检查工作列表中的项目是否存在于新名称的字符串中,如果存在,则将新名称而不是它添加到输出列表中。
但是你还需要检查你是否已经在项目的位置添加了一些东西到输出列表,所以你用布尔跟踪它,当且仅当你检查了所有 new_names 和你没有添加任何内容,将其添加到输出列表中。
for item in working_list
added = False
for new in new_names:
if item in new
output_list.append(new)
added = True
if not added
output_list.append(item)