How to create an Array within the other

Asked

Viewed 142 times

0

I need to create an array that has Indice and values
page_links receives the links of a page

all_links_main = []
for link in page_links:
 all_links_main.append(link.get('href'))

produto = []

for link1 in all_links_main:
 page_one = requests.get(link1)
 print(page_one)
 soup_one = BeautifulSoup(page_one.content, 'html.parser')

I need to include something like: Indice
0 [name, value, image] 1 [name, value, image] ...

But I’m not getting it.

To my mind would be an array within the other (two-dimensional), I am new in python.

  • Do you need within an array position to have the values of name, value and image? Sorry, I didn’t get it right.

  • I need to do it anyway.

  • It would not be better then to create a class that has these attributes and then add the class to the array?

  • I need to take the products of a site, example site a And save the name, value and image of that product in the array to later save in the bank. I don’t know how I could do it like that.

  • 1

    Check this example with class, https://pastebin.com/eJLvBQ4J

  • @Danielmendes had said he would not use with class, but I used had some problems implementing with class to save in the bank the arrays with the executemany made an error: TypeError: 'Produto' object is not iterable you know how I can fix?

Show 2 more comments

1 answer

1

To put a list inside another list, just use the method append in the external list with the internal list as a parameter.

a = []
for _ in range(10):
     a.append([1, 2, 3]) # Lista a agora contém 10 sub-listas [1, 2, 3]

You can also use list comprehension to make the code more compact

a = [[1, 2, 3] for _ in range(10)] # Equivalente ao código anterior
  • I’ll test it here, I think it would be just that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.