Home > Blockchain >  How can I check the arrays in an array list - Python
How can I check the arrays in an array list - Python

Time:10-26

I have a list of arrays with two values each, they correspond to two columns of an excel. I would like to know how can I check the second value of each array?

This is the structure...

[['AIGSALPPMM', 'AVIVANVS'], ['DKEPBARCLA', 'DAVIDSNKC'], ['DAVKEMBDBL', 'DAVIDSNKC'], ['DAKEMJPMPB', '']]

With this code I retrieve the values from the tables and create a list that returns me several arrays with the values of these two columns that I need to check.

#Read the excel and convert to list

df = pd.read_excel('check_counterparties.xlsx', sheet_name = 'counterparties_cdwu', usecols = "A,B", skiprows=4)
dataList =  df.values.tolist()

#    print(dataList)

As you can see the last array has the second value empty.

What I want is to check if the second value of each array is empty. If it is, I want to return the two values from this array that has the second empty value.

Any idea how I can do that?

CodePudding user response:

main_arr = [['AIGSALPPMM', 'AVIVANVS'], ['DKEPBARCLA', 'DAVIDSNKC'], ['DAVKEMBDBL', 'DAVIDSNKC'], ['DAKEMJPMPB', '']]

for arr in main_arr:
    if arr[1] == '':
       print (arr)

CodePudding user response:

Use list comprehension:

lst = [['AIGSALPPMM', 'AVIVANVS'], ['DKEPBARCLA', 'DAVIDSNKC'], ['DAVKEMBDBL', 'DAVIDSNKC'], ['DAKEMJPMPB', '']]

>>> [sublist for sublist in lst if len(sublist[-1].strip())==0]
[['DAKEMJPMPB', '']]

CodePudding user response:

Use a boolean mask to filter your rows:

df = pd.read_excel('check_counterparties.xlsx', sheet_name='counterparties_cdwu',
                   usecols=['A', 'B'], skiprows=4)

out = df[df['B'] == '']

Output:

# DataFrame format
>>> out
            A B
3  DAKEMJPMPB  

# List format
>>> out.to_dict(orient='split')['data']
[['DAKEMJPMPB', '']]

CodePudding user response:

Here you go:

your_list = [['AIGSALPPMM', 'AVIVANVS'], ['DKEPBARCLA', 'DAVIDSNKC'], ['DAVKEMBDBL', 'DAVIDSNKC'], ['DAKEMJPMPB', '']]
new_list = [[val[0], val[0]] if val[-1]=='' else val for val in your_list] 
new_list

Correct me if I misunderstood your question

  • Related