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

Discussion in 'Programming' started by elavmunretea, Jan 27, 2018.

  1. elavmunretea

    elavmunretea BANNED BANNED

    Joined:
    May 14, 2016
    Messages:
    2,220
    Likes Received:
    2,970
    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: Jan 27, 2018
  2. Jinyus

    Jinyus Regular Member

    Joined:
    Jul 6, 2017
    Messages:
    398
    Likes Received:
    154
    Gender:
    Male
    Wrong place... Google it and you should find the answer
     
  3. elavmunretea

    elavmunretea BANNED BANNED

    Joined:
    May 14, 2016
    Messages:
    2,220
    Likes Received:
    2,970
    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.
     
  4. BlackHatRhino

    BlackHatRhino Newbie

    Joined:
    Jan 24, 2017
    Messages:
    26
    Likes Received:
    6
    Gender:
    Male
    Occupation:
    WebDev
    Do you mean nested loops? something like this?
    Code:
    links = [example_link0, example_link1, example_link2]
    
    for link in links:
      i = 0
     
      while i < len(link):
        ...
        i += 1
     
    • Thanks Thanks x 2
  5. rimvy

    rimvy Jr. VIP Jr. VIP

    Joined:
    Oct 8, 2017
    Messages:
    423
    Likes Received:
    141
    Gender:
    Male
    Location:
    localhost
    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)
     
    • Thanks Thanks x 1
  6. weedsmoker

    weedsmoker Junior Member

    Joined:
    May 2, 2011
    Messages:
    191
    Likes Received:
    79
    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).
     
  7. Grimasaur

    Grimasaur Junior Member

    Joined:
    Apr 8, 2016
    Messages:
    171
    Likes Received:
    92
    @elavmunretea to place an array inside another one you use append in python example
    Code:
    array = [a,b]
    array2 = [1,2]
    array1.append(array2)
    
    # array1 = [a,b,[1,2]]
     
    • Thanks Thanks x 1
  8. CoolNicks

    CoolNicks Registered Member

    Joined:
    Aug 21, 2016
    Messages:
    53
    Likes Received:
    22
    Gender:
    Male
    @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
     
    • Thanks Thanks x 1
  9. Ys0Seri0us

    Ys0Seri0us Newbie

    Joined:
    Dec 8, 2017
    Messages:
    10
    Likes Received:
    2
    Gender:
    Male
    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