当我将每个列表分别传递给函数时,我的函数运行良好。当我尝试传递列表列表时,它不起作用。如何使用带有功能的嵌套列表
我尝试过分别传递列表,并且运行正常。我在尝试传递嵌套列表时遇到问题
# for refrence, this is what the nested list looks like at this point
# [['.topA1', 'top:83px;', 'left:115px;'], ['.topA1', 'top:83px;', 'left:115px;'], ['.topA1', 'top:83px;', 'left:115px;'], ['.topA1', 'top:83px;', 'left:115px;']]
# dictionaries of old and new values
old = { 'old': '.topA1', 'old': '.topAl', 'old': '.topAl', 'old': '.topAl' }
new = { 'new': '.topA1', 'new': '.topB1', 'new': '.topC1', 'new': '.topD1' }
# defines functions to find and replace values
def replaced( *name_list, old=None, new=None ):
# returns and replaces new value in nested list if old value matches
return [ new if x == old else x for xs in name_list for x in xs ]
# calls functions with *args, **kwargs values
new_words = (replaced( *name_list, **old, **new ))
# iterates through items in list
for item in new_words:
# prints each item in list
print( "%s" % item )
预期输出:topA1,topB1,topC1,topD1