简体   繁体   English

根据其他两个列表在 Python 中创建列表?

[英]Create list in Python based on two other lists?

I have a list which looks like this:我有一个看起来像这样的列表:

working_list=['one', 'two', 'three', 'four', 'five']

And I want to have a new list which would look like this:我想要一个看起来像这样的新列表:

output_list=['one or two','one or two','three','four or five','four or five']

For that, I have created two other lists:为此,我创建了另外两个列表:

old_names=[['one','two'],['four','five']]
new_names=['one or two','four or five']

And then I tried:然后我尝试了:

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)

But that gives me an output:但这给了我一个 output:

['one or two', 'one', 'one or two', 'two', 'three', 'three', 'four', 'four or five', 'five', 'four or five']

Any ideas on how to solve this?关于如何解决这个问题的任何想法?

Would greatly appreciate it!将不胜感激!

For any kind of a->b mapping you should use a dictionary.对于任何类型的 a->b 映射,您都应该使用字典。 For example, replace your old_names and new_names lists with an old_to_new_names dictionary例如,将old_namesnew_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]

The old_to_new_names.get method looks for i in the dictionary and if it doesn't exist just returns i (the second argument is the default) old_to_new_names.get方法在字典中查找i ,如果不存在则返回i (第二个参数是默认值)

If you would still like to use your code with little modification but @tomjn is scalable and the best method如果您仍然想使用您的代码而无需修改,但 @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)

The answer by @tomjin is a better idea, the code with dicts is clear and easy to read, but if you want to know how your code could be tweaked to work: @tomjin 的答案是一个更好的主意,带有 dicts 的代码清晰易读,但是如果您想知道如何调整代码以使其工作:

All you need to do is check if an item from the working list exists in the string of the new names and if so, add the new name instead of it to the output list.您需要做的就是检查工作列表中的项目是否存在于新名称的字符串中,如果存在,请将新名称而不是新名称添加到 output 列表中。

But you need to also check if you have already added something to the output list in the item's place, so you keep track of it with a bool and if and only if you checked all the new_names and you added nothing, you add it to the output list.但是您还需要检查您是否已经在项目位置的 output 列表中添加了某些内容,因此您使用布尔值跟踪它,并且当且仅当您检查了所有 new_names 并且您没有添加任何内容时,您将其添加到output 列表。

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM