仍然是编程/脚本的新手,这个一直困扰着我。我有一个函数可以搜索名称列表,将其与名称模板列表进行比较,当它找到匹配项时,它会以正确的顺序将其放入我的最终列表中。为了使以后的某些功能正常工作,我需要能够将其中一些名称附加为数组/列表。我遇到的问题是,每次我需要将列表添加到最终列表时,只要我更改变量,最终列表就会随之更新。我该如何解决?
light = ['template of names in here in correct order']
listUser = ['names gathered from user input']
for userChan in listUser:
for channelName in light:
#check if channelName is a list or string
if isinstance(channelName, basestring):
#search for matches in userchan
print channelName, 'is a string'
if channelName in userChan.lower():
matchFound = True
listLight.append(userChan)
else:
print channelName, 'is a list'
for piece in channelName:
print 'searching %s in %s' %(piece, userChan.lower())
if piece in userChan.lower():
print "found %s in %s" %(piece, userChan.lower())
lightMultList.append(piece)
matchFound = True
if len(lightMultList) == 2:
listLight.append(lightMultList)
del lightMultList[:]
所以我的问题是lightMultList。它总是被限制在 2 个元素中,但它会发生变化。希望这不是措辞太可怕..