Python:遍历 nested-list-of-lists 中的每个项目并替换特定项目
Python: Iterate over each item in nested-list-of-lists and replace specific items
我是 python 的初学者。我想知道是否有任何内置功能或其他方式,以便我可以在 python 2.7:
中实现以下
查找列表和子列表中的所有-字母并替换为['not',字母]
例如:在下面的列表中找到所有以 - 开头的项目,并将它们替换为 ['not',letter]
Input : ['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']]
Output : ['and', ['or', ['not','S'], 'Q'], ['or', ['not','S'], 'R'], ['or', ['or', ['not','Q'], ['not','R']], ['not','S']]]
任何人都可以在 python 中建议如何做。
谢谢
尝试一点递归:
def change(lol):
for index,item in enumerate(lol):
if isinstance(item, list):
change(item)
elif item.startswith('-'):
lol[index] = ['not',item.split('-')[1]]
return lol
进行中:
In [24]: change(['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']])
Out[24]:
['and',
['or', ['not', 'S'], 'Q'],
['or', ['not', 'S'], 'R'],
['or', ['or', ['not', 'Q'], ['not', 'R']], ['not', 'S']]]
您需要使用递归 function.The isinstance(item, str)
只是检查一个项目是否为字符串。
def dumb_replace(lst):
for ind, item in enumerate(lst):
if isinstance(item, str):
if item.startswith('-'):
lst[ind] = ['not', 'letter']
else:
dumb_replace(item)
并且:
dumb_replace(Input)
给出:
['and', ['or', ['not', 'letter'], 'Q'], ['or', ['not', 'letter'], 'R'], ['or', ['or', ['not', 'letter'], ['not', 'letter']], ['not', 'letter']]]
根据找到的食谱 here:
def nested_list_replacer(seq, val = '-S', sub = ['not', 'letter']):
def top_kill(s):
for i in s:
if isinstance(i, str):
if i == val:
i = sub
yield i
else:
yield type(i)(top_kill(i))
return type(seq)(top_kill(seq))
l = ['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']]
print(nested_list_replacer(l, '-S'))
print(nested_list_replacer(l, '-Q'))
给出:
['and', ['or', ['not', 'letter'], 'Q'], ['or', ['not', 'letter'], 'R'], ['or', ['or', '-Q', '-R'], ['not', 'letter']]]
['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', ['not', 'letter'], '-R'], '-S']]
我是 python 的初学者。我想知道是否有任何内置功能或其他方式,以便我可以在 python 2.7:
中实现以下查找列表和子列表中的所有-字母并替换为['not',字母]
例如:在下面的列表中找到所有以 - 开头的项目,并将它们替换为 ['not',letter]
Input : ['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']]
Output : ['and', ['or', ['not','S'], 'Q'], ['or', ['not','S'], 'R'], ['or', ['or', ['not','Q'], ['not','R']], ['not','S']]]
任何人都可以在 python 中建议如何做。 谢谢
尝试一点递归:
def change(lol):
for index,item in enumerate(lol):
if isinstance(item, list):
change(item)
elif item.startswith('-'):
lol[index] = ['not',item.split('-')[1]]
return lol
进行中:
In [24]: change(['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']])
Out[24]:
['and',
['or', ['not', 'S'], 'Q'],
['or', ['not', 'S'], 'R'],
['or', ['or', ['not', 'Q'], ['not', 'R']], ['not', 'S']]]
您需要使用递归 function.The isinstance(item, str)
只是检查一个项目是否为字符串。
def dumb_replace(lst):
for ind, item in enumerate(lst):
if isinstance(item, str):
if item.startswith('-'):
lst[ind] = ['not', 'letter']
else:
dumb_replace(item)
并且:
dumb_replace(Input)
给出:
['and', ['or', ['not', 'letter'], 'Q'], ['or', ['not', 'letter'], 'R'], ['or', ['or', ['not', 'letter'], ['not', 'letter']], ['not', 'letter']]]
根据找到的食谱 here:
def nested_list_replacer(seq, val = '-S', sub = ['not', 'letter']):
def top_kill(s):
for i in s:
if isinstance(i, str):
if i == val:
i = sub
yield i
else:
yield type(i)(top_kill(i))
return type(seq)(top_kill(seq))
l = ['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', '-Q', '-R'], '-S']]
print(nested_list_replacer(l, '-S'))
print(nested_list_replacer(l, '-Q'))
给出:
['and', ['or', ['not', 'letter'], 'Q'], ['or', ['not', 'letter'], 'R'], ['or', ['or', '-Q', '-R'], ['not', 'letter']]]
['and', ['or', '-S', 'Q'], ['or', '-S', 'R'], ['or', ['or', ['not', 'letter'], '-R'], '-S']]