2016-08-26 21 views
0

我正在学习python。我想在数组或列表中存储一个一个元素。 这是我的代码。想要使用python存储数组或列表中的元素

for u in durl: 
     p2 = requests.get(u) 
     tree = html.fromstring(p2.content) 
     dcode.append = tree.xpath(ecode) 
     print(dcode) 

在dcode变量中,元素覆盖不会附加。 我想逐个插入它。 请帮助我。

+1

[追加对延长](HTTP的可能重复:// stackoverflow.com/questions/252703/append-vs-extend) –

回答

0

tree.xpath(...)返回一个列表,所以如果你想用现有的列表中选择元素dcode合并,你可能会做

dcode.extend(tree.xpath(ecode)) 
+0

但它再次给出错误,像dcode没有定义 – virushree

2

追加的方法不是一个变量,所以如果你要追加tree.xpath(ecode)dcode那么你应该写dcode.append(tree.xpath(ecode))而不是dcode.append =这是一个不属于方法调用的赋值。

0

你能做到这样,追加新的值列表,它更有意义......排序数据结构的

def main(decode=[]): 
    for u in durl: 
     p2 = requests.get(u) 
     tree = html.fromstring(p2.content) 
     dcode.append(tree.xpath(ecode)) 
     print(dcode) 
相关问题