0

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
i'mgnome
  • 483
  • 1
  • 3
  • 17
  • 1
    you're not actually storing `p` anywhere, so `price` wouldn't change, –  May 05 '22 at 20:59

1 Answers1

1

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

Yolao_21
  • 765
  • 1
  • 4
  • 10