Home > Blockchain >  Appending two lists with many elements in Python
Appending two lists with many elements in Python

Time:09-12

The following code gives me A for i=1,2. But then I want A for each i to append and give combined A i.e. for both i=1,2. I present the current and expected outputs.

import pandas as pd
import numpy as np

A=[]

for i in range(1,3):
    file_loc = f"C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\{i}\\Test.xlsx"
    df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
    A=df["N"].to_numpy()
    
    A = [x for x in A if str(x) != 'nan']
    
    A = [eval(e) for e in A]
    print([A])

The current output is

[[1], [2], [3], [4], [5]]
[[8], [4], [6], [2], [3]]

The expected output is

[[[1], [2], [3], [4], [5]],[[8], [4], [6], [2], [3]]]

CodePudding user response:

You can append [A] to some other array within each iteration, and print the array containing both arrays in the end

import pandas as pd
import numpy as np

A = []
X = []

for i in range(1,3):
    file_loc = f"C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\{i}\\Test.xlsx"
    df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
    A=df["N"].to_numpy()
    
    A = [x for x in A if str(x) != 'nan']
    
    A = [eval(e) for e in A]
    X.append([A])

print(X)

CodePudding user response:

I change your code with remove misisng values by Series.dropna and ast.literal_eval for avoid eval, then create new list B and append to out list:

import ast

out,B=[],[]

for i in range(1,3):
    file_loc = f"C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\{i}\\Test.xlsx"
    df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
    
    B = [ast.literal_eval(x) for x in df["N"].dropna()]
    out.append(B)

print(out)
  • Related