Home > Software design >  Python append list and for statement
Python append list and for statement

Time:10-28

I want to get a list with variables and filter it

I succeeded in removing unnecessary words, but

filtered = [] does not work from here. print(filtered) = []

What I want is 'USD' or (and) 'EUR' according to the variable in filtered.

Append seems to be the problem... I don't know the cause

And in the second code, why is the POA not deleted? Can't delete specific characters from list?


def test():
    response = [{'1': 0, '2': '2A', '3': True, '4': True, 'QA': ['NZDUSD','NZDEUR','NZDYEN','NZDGBP']}]
    if response == []:
        return

    else:
        response = str(response [0]['QA'])
        response = response.replace('NZD','')
        
        filtered = []
        for data in [response]:
            if data in ['USD','EUR']:
                filtered.append(data)
            # print(data)
        
        print(filtered)
        if 'USD' in filtered and 'EUR' in filtered:
            print ('USDEUR')
        elif 'USD' in filtered:
            print ('USD')
        elif 'EUR' in filtered:
            print ('EUR')

test()
def test():
    response = [{'1': 0, '2': '2A', '3': True, '4': True, 'QA': ['POAUSD','POAEUR','POAYEN','POAGBP']}]
    if response == []:
        return

    else:
        response = response [0]['QA']
        for i in range(len(response)):
            if response[i] == 'POA':
                response[i] = ''
        # print(response)
        filtered = []
        for data in [response]:
            if data in ['USD','EUR']:
                filtered.append(data)
            print(data)
        print(filtered)
        if 'USD' in filtered and 'EUR' in filtered:
            return print('USDEUR')
        elif 'USD' in filtered:
            return print('USD')
        elif 'EUR' in filtered:
            return print('EUR')

test()

CodePudding user response:

About your 1st code: response is a string and not a list, so "for data in response" will yield individual characters, not the words you expect.

For it to work as you intend, modify this part of the code:

...
    else:
        response = response [0]['QA']
        response = [ word.replace('NZD','') for word in response]
        # print("result: ",response) # testing statement
        
        filtered = []
        for data in response:
...

About your 2nd code:

if response[i] == "POA":

will always fail; you want to replace it and the following line (amongst other solutions) with:

if response[i].startswith('POA'):
   response[i] = response[i][3:] # or ... = response[i].replace('POA','')

And further on,

for data in response:

instead of

for data in [response]:

(explanation: [response] is a list containing response as its unique value, so for data in [response]: is equivalent to "data = response")

CodePudding user response:

if I understood correctly what you wanted to do is this:

def test():
    response = [{'1': 0, '2': '2A', '3': True, '4': True, 'QA': ['NZDUSD','NZDEUR','NZDYEN','NZDGBP']}]
    if response == []:
        return

    else:
        response = str(response [0]['QA'][0])
        response = response.replace('NZD','')
        
        filtered = []
        for data in [response]:
            if data in ['USD','EUR']:
                filtered.append(data)
            # print(data)
        
        print(filtered)
        if 'USD' in filtered and 'EUR' in filtered:
            print ('USDEUR')
        elif 'USD' in filtered:
            print ('USD')
        elif 'EUR' in filtered:
            print ('EUR')

test()

the output is:

['USD']
USD

but here's a tip for the next time, when you are posting please name your variables somewhat clear with more meaning to each variable- everything was 'response' which is not a good name to use again and again(and of course its bad practice to recycle variable name over and over in the same script) but I think you got my point :)

  • Related