我有一个项目清单,例如
item_list = ['char_model_..._main', 'char_model_..._main_default', 'char_rig_..._main', 'char_rig_..._main_default', 'char_acc__..._main']
虽然我能够从列表中得到我想要的某些东西,并且我将其编码如下(不是以一种非常庄重的方式,但它会给我反馈):
item_wanted=[]
for item in item_list:
if item.startswith("char_model") and (item.endswith("main") or item.endswith("main_default")):
item_wanted.append(item)
因此,虽然我能够得到我想要的项目,使得现在的my item_wanted
列表包含'char_model_..._main', 'char_model_..._main_default'
,但我应该如何编码它,以便如果'main'
存在,就使用它,否则就使用'main_default'
?你知道吗
嗯,我不完全明白你的意图,但也许你可以试着把它分开
希望这有帮助
我的方法是简单地使用两个变量并对输入列表进行一次迭代。它能够处理输入中的多个“good”和“best”匹配。为了简化,我使用了数字列表,对于
main
和main_default
部分,我习惯于使用1而不是2。你知道吗代码:
输出:
看到这个运行在https://ideone.com/bBj5Op
将有效项分为包含默认值的项和不包含的项。然后,迭代那些do以default结尾的键,并尝试在另一个列表中找到相同的键(当然要删除结尾)。如果它不存在,就意味着我们必须使用默认值,如果它存在,我们就保持原样。你知道吗
相关问题 更多 >
编程相关推荐