Replace point by comma in Python 3

2

Hello, I have a list of decimals that I converted to String, in order to change the points by commas. But with "replace" it does not allow me to make the change, I'm doing it this way:

   for i in range(len(UCL)): #UCL de corte
      UCL[i].replace(".",",")
      print(UCL[i])

The original UCL list is of the form:

    ['2.363636368', '2.17391304', '3.875', '2.282828286', '2.383838386', '2.616161618', '2.363636368', '4.85', '4.75', '2.282828289', '2.616161618', '0.666666666', '3.0', '3.272727272', '2.434343439', '0.666666666', '0.666666666', '2.538461538', '0.666666666', '3.875', '2.616161618', '2.393939396', '0.666666666', '0.666666666', '0.666666666', '0.666666666', '2.383838384']

My idea is to print the same but changing the points by commas within each string of the UCL list.

    
asked by Jorge Ponti 25.07.2017 в 23:42
source

3 answers

1

The problem is that str is immutable and replace therefore returns a copy of the string (can not modify the original string). You can create a new list using list compression with:

nueva_UCL = [c.replace('.', ',') for c in UCL]

If you prefer to use a normal for and modify the original list you should do:

for i in range(len(UCL)):
    UCL[i] = UCL[i].replace(".", ",")

I guess you want to modify the list as well as print it. If you only want to print using commas, without modifying the original list, you can do (in Python 3):

print(*(c.replace(".", ",") for c in UCL), sep='\n')

Or for Python 2 / Python3:

for c in UCL:
    print(c.replace(".", ","))
    
answered by 25.07.2017 / 23:48
source
1

Jorge, in Python the chains like several other objects are immutable, do this:

s = "1.4"
print(s.replace(".",","))
print(s)

This is going to come back to you:

1,4
1.4

With this we confirm that s is still valid "1.4". What you must do is generate a new chain by assigning:

s = s.replace(".",",")
print(s)

And now yes

1,4

In your example you should do the following:

UCL[i] = UCL[i].replace(".",",")
    
answered by 25.07.2017 в 23:52
1

I used this and if it served me:

(tudato.replace(".",',') or :)

after the replace you mention what you want to replace, then why you want it to appear, and or means or change . or : by%% of%

    
answered by 26.07.2017 в 00:20