Home > Blockchain >  List element replacement is not working after using multiple solutions
List element replacement is not working after using multiple solutions

Time:05-06

I have a list where most element are numbers in form of strings and a few normal strings that I got from a dataset.

Here's the first 50 items:

['-', '-', '-', '-', '-', '404000000', '110000000', '4926000', '15000000', '-', '-', '-', '-', '-', '14900000', '66100000', '15000000', '13000000', '20000000', '-', '20000000', '30000000', '-', '-', '-', '-', '278000000', '-', '17000000', '275000000', '121000000', '-', '12000000', '-', '29000000', '-', '267000000', '500000000', '50000000', '-', '356000000', '-', '-', '-', '20000000', '-', '-', '-', '-', '-']

I want to replace those '-' and 'undisclosed' with the number 0. I followed this answer on a stackoverflow question but that tutorial did not work and I even tried this but still didn't work.

for p in price:
  if p != '-' and p != 'undisclosed':
    p = int(p)
  else:
    p = 0

CodePudding user response:

As the comment says, you are not actually changing the value in the list. You can try with enumerate:


price = ['-', '-', '-', '-', '-', '404000000', '110000000', '4926000', '15000000', '-', '-', '-', '-', '-', '14900000', '66100000', '15000000', '13000000', '20000000', '-', '20000000', '30000000', '-', '-', '-', '-', '278000000', '-', '17000000', '275000000', '121000000', '-', '12000000', '-', '29000000', '-', '267000000', '500000000', '50000000', '-', '356000000', '-', '-', '-', '20000000', '-', '-', '-', '-', '-']

for n,p in enumerate(price):
    if p != '-' and p != 'undisclosed':
        price[n] = int(p)
    else:
        price[n] = 0

  • Related