简体   繁体   中英

want to store elements in array or list using python

I am learning python. I want to store one by one element in array or list. this is my code.

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

in dcode variable the elements are overriding not appending. i want to insert it one by one. please help me.

append是不是变量的方法,因此,如果您想将tree.xpath(ecode)附加到dcode ,则应编写dcode.append(tree.xpath(ecode))而不是dcode.append =这是一个赋值,而不是方法调用。

tree.xpath(...)返回一个列表,因此,如果要将其与所选元素的现有列表dcode合并,则可以这样做

dcode.extend(tree.xpath(ecode))

You can do it this way, it appends new value to the list and it makes more sense...sort of data structure

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM