【发布时间】:2020-09-14 11:59:34
【问题描述】:
我正在尝试做一个程序,试图找出是否可以使用给定的输入形成二叉树。
def main():
children={}
parents={}
possible=True
tup=input("enter the tuples seperated by commas")
tup=tup.replace("(","")
tup=tup.replace(")","")
data=tup.split(" ")
print(len(data))
for x in range(0,len(data)):
child,parent=data[x].split(",")
if parent in parents :
parents[parent].append()
else:
parents[parent]=child
if len(parents[parent])>2:
possible=False
break
if child in children:
possible=False
break
else:
children[child]=parent
if possible==True:
print("BINARY TREE POSSIBLE")
else:
print("BINARY TREE NOT POSIBLE")
if __name__=="__main__":
main()
问题出在这一行:
parents[parent].append()
在第 37 行它说:
AttributeError: 'str' object has no attribute 'append' 如何添加列表 作为python字典中键的值
【问题讨论】:
标签: python list dictionary