Compare values of two arrays

Asked

Viewed 1,327 times

2

Hello I need to compare 2 values of 2 arrays, but I’m not if succeeding:

plan = [{'Cod': '11518', 'qtdPallet': '176', 'qtdLastro': '22', 'qtdCx': '12', 'Prod': 'Exemplo1', 'FC': '1'}, {'Cod': '972', 'qtdPallet': '72', 'qtdLastro': '9', 'qtdCx': '24', 'Prod': 'Exemplo2', 'FC': '2'}]
dados = [{'qtdCx': '133', 'qtdUn': '00', 'Cod': '11518'}, {'qtdCx': '345', 'qtdUn': '5', 'Cod': '13'}, {'qtdCx': '234', 'qtdUn': '03', 'Cod': '13205'}, {'qtdCx': '3545', 'qtdUn': '09', 'Cod': '16'}, {'qtdCx': '1884', 'qtdUn': '03', 'Cod': '978'}]

def calcula():
    for plax in plan:
        for dad in dados:
            if dad['Cod'] == plax['Cod']:
                print(plax["Prod"])

calcula()

I compare the codes of each and if they are equal I will use them; but it is not working. What am I doing wrong? I’ve had this problem for a long time and I can’t solve it in any way!

  • 1

    I ran your code and when the Cod of the two arrays is 11518 he prints Exemplo1. It’s not right that? Running on the Ideone

  • Idem here - your approach may not be the best possible, but it is technically correct. What error did you get? Is the data you are using the same? Codes are always strings like here, or in your data are sometimes integer and sometimes string?

  • My data is strings but I need to keep turning one by one to int, is there any way to do this with everyone? (Except the product name) And as for the approach, what would be the best approach to be taken?

1 answer

1

def converte(dados): # retorna uma nova lista de dict  para DADOS, com os item para INT
    novo = []
    for i in dados:
        for j in i:
            i[j] = int(i[j])
            novo.append(i)
    if len(novo)%2 == 0:
        return novo[::2]
    else:
        return novo[::3]


def verifica(plan, dados): # mesma coisa da sua funcao calcula faz a checagem do Cod com todas as ocorrencias porem retorna a String de Prod
    for i in range(len(plan)):
        for j in range(len(dados)):
            if plan[i]['Cod'] in dados[j]['Cod']:
                return plan[i]['Prod']
            else:
                pass

Browser other questions tagged

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