[英]JSON, lists and recursion in python
我是Python的新手,遇到了我無法解決的問題。
我已經將以下解析樹從JSON解碼到以下列表。
>>> tree
['S', ['NP', ['DET', 'There']], ['S', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', 'asbestos']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]]
使用遞歸函數,我已經能夠獲得包含終止詞的列表。
def explorer(tree):
for sub in tree[1:]:
if(type(sub) == str):
allwords.append(sub)
else:
explorer(sub)
>>> allwords
['There', 'is', 'no', 'asbestos', 'in', 'our', 'products', 'no'.]
現在,我需要替換原始樹中符合某些條件的單詞,以便獲得如下內容:
['S', ['NP', ['DET', 'There']], ['S', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', '_REPLACED_']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]]
我嘗試了以下功能,但是無法向上傳播替換項,因此我總是得到相同的舊原始樹。
def replacer(tree):
string=[]
for sub in tree[1:]:
if(type(sub) == str):
if #'condition is true':
sub="_REPLACE_"
return sub
else: return sub
else:
string.extend(replacer(sub))
print(string)
我希望能獲得一些提示,以實現結果。 先感謝您。
您的問題是在某些情況下您要重新調整字符串,而在其他情況下要打印列表。 確保您的替換器始終返回字符串列表,並且應該沒問題。
因此,這是我如何使用列表推導來執行此類操作的示例。 如果您不知道,列表推導就是something = [explorer(x) for x in something]
。 這也是遞歸發生的地方。 您得到的是完全相同的結構的列表,但是您已經“接觸”到每個端點,並且可以檢查和替換事物。 我進行了兩次任意替換。
>>> tree = ['S', ['NP', ['DET', 'There']], ['S', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', 'asbestos']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]]
>>> def explorer(something):
if type(something) == list:
something = [explorer(x) for x in something]
else: # You may want to check other conditions here, like if it's a string
if something == 'asbestos':
something = 'Oh my'
if something == 'S':
something = 'Z'
return something
>>> explorer(tree)
['Z', ['NP', ['DET', 'There']], ['Z', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', 'Oh my']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]]
>>>
仔細閱讀您的話后,我才發現一些東西。 您之所以無法“向上傳播替換”,是因為您的循環結構如下所示:
for x in aList:
if x = somethingSpecial:
x = somethingElse
那在Python中不起作用,但是可以:
for i,x in enumerate(aList):
if x = somethingSpecial:
aList[i] = somethingElse
現在, aList
已被修改為所需的方式。 如果您不知道enumerate()
作用,只需復制/粘貼以下內容:
aList = ['a','b','c']
for i,x in enumerate(aList):
print(i,x)
如果我正確理解了您的問題,那么解決您的問題的一種方法是這樣的:
>>> tree = ['S', ['NP', ['DET', 'There']], ['S', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', 'asbestos']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]]
>>> def replacer(tree):
for i, sub in enumerate(tree[1:]):
if type(sub) == str and sub == 'asbestos':
tree[i+1] = '__REPLACE__'
else:
replacer(sub)
如果對tree [1:]進行了更改,則實際上不是在更改列表,而是在更改接頭。 因此, 枚舉功能可解決此問題。 您的sub="_REPLACE_"
實際上不會更改列表。 它只是為name sub
分配一個新值。
結果:
>>> replacer(tree)
>>> tree
['S', ['NP', ['DET', 'There']], ['S', ['VP', ['VERB', 'is'], ['VP', ['NP', ['DET', 'no'], ['NOUN', '__REPLACE__']], ['VP', ['PP', ['ADP', 'in'], ['NP', ['PRON', 'our'], ['NOUN', 'products']]], ['ADVP', ['ADV', 'now']]]]], ['.', '.']]]
要獲得第一個函數創建的新列表,您只需將第一個函數應用於新的tree
列表:
>>> explorer(tree)
['There', 'is', 'no', '__REPLACE__', 'in', 'our', 'products', 'now', '.']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.