How Do I Place an Array Inside of Another Array with Python?

  • Thread starter Thread starter Deleted member 969102
  • Start date Start date
D

Deleted member 969102

Guest
So I've been writing a script for a few days that does a long list of different things. One section of it involves writing a csv with a long list of assets and the last time they were updated, turning multiple lists into a single file.

Here's the code that is writing the results to a .csv:

Code:
i = 1
while i < len(example_link):
    with open(sheet_name, 'a') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow([example_link[0].text.strip(), example_lastmod[0].text.strip()])
    del example_link[0]
    del example_lastmod[0]

I repeat this while section for each different sets of data I am using, like this:

Code:
i = 1
while i < len(example_link):
    with open(sheet_name, 'a') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow([example_link[0].text.strip(), example_lastmod[0].text.strip()])
    del example_link[0]
    del example_lastmod[0]

i = 1
while i < len(example1_link):
    with open(sheet_name, 'a') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow([example1_link[0].text.strip(), example1_lastmod[0].text.strip()])
    del example1_link[0]
    del example1_lastmod[0]

i = 1
while i < len(example2_link):
    with open(sheet_name, 'a') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow([example2_link[0].text.strip(), example2_lastmod[0].text.strip()])
    del example2_link[0]
    del example2_lastmod[0]

This works fine and it does the job, however, it leads to a lot of unnecessary code and if I then want to integrate another list of assets & dates I have to add a whole new section.

I was wondering if there is a way to condense my code?

So I am currently writing example_link[0] and example_lastmod[0] to the csv and then deleting [0] (This then makes [1] > [0] and repeats until the example_lastmod list is empty.) When that is done, in a new while loop, I then write example1_link[0] and example1_lastmod[0] to the end of the same file.

Would something like this work?

Code:
links = [example, example1, example2]
lastmod = [example_l, example1_l, example2_l]

i = 1
while i < len(links[0]):
  
   link_a = links[0]
   lastmod_a = lastmod[0]
  
   with open(sheet_name, 'a') as csv_file:
       writer = csv.writer(csv_file)
       writer.writerow([link_a[0].text.strip(), lastmod_a[0].text.strip()])
   del link_a[0]
   del lastmod_a[0]
  
   if len(link_a) == 1:
       del links[0]
       del lasmod[0]

The above does work, however, I get 400 less results in my CSV when running it for some reason.
 
Last edited by a moderator:
Wrong place... Google it and you should find the answer
 
Wrong place... Google it and you should find the answer
Wrong place? This is the programming section. Please direct me to a more appropriate sub forum...

I have Googled it. My code does work, but the second, condensed, version shaves off 400 critical results.
 
Or something like that ?
Code:
def writeToCSV(example_link, example_lastmod):
    i = 1
    while i < len(example_link):
        with open(sheet_name, 'a') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow([example_link[0].text.strip(), example_lastmod[0].text.strip()])
        del example_link[0]
        del example_lastmod[0]


links = [example, example1, example2]
lastmod = [example_l, example1_l, example2_l]

for link, mod in zip(links, lastmod):
    writeToCSV(link, lastmod)
 
A little nitpicking about thread title and contents, arrays are not exactly the same as the lists in Python, for most tasks lists are better suited, but arrays (which are more useful in specific cases and are located in array module) are closer to "C" arrays and all elements of the array must be of the same data type (numeric/character type) and defined when creating array and they are less flexible than lists but more tuned for efficiency memory wise and speed (this depends on the use case).
 
@elavmunrete, @Grimasaur answer is the one you are looking for.

lists,

Either you can append to the end of the existing array or assign to a existing array position

Code:
array1 = ['a','b'];
array2 = ['c','d'];
array1.append(array2);

print(array1);
print(array1[0]);
print(array1[1])
print(array1[2])
print(array1[2][0])
print(array1[2][1])

>>>

['a', 'b', ['c', 'd']]
a
b
['c', 'd']
c
d
 
links = [example, example1, example2] lastmod = [example_l, example1_l, example2_l] i = 1 while i < len(links[0]): link_a = links[0] lastmod_a = lastmod[0] with open(sheet_name, 'a') as csv_file: writer = csv.writer(csv_file) writer.writerow([link_a[0].text.strip(), lastmod_a[0].text.strip()]) del link_a[0] del lastmod_a[0] if len(link_a) == 1: del links[0] del lasmod[0]

while i <len(links) # length of the list
link_a=links #so it iterates through all the elements in links

I think you got my point.
Instead of using the first element,use i to access all the elements and then delete the entire list
 
Back
Top