Home > Net >  I need to modify a certain part of a string from a string in a list
I need to modify a certain part of a string from a string in a list

Time:11-02

im trying to convert a string of a list into a new string. There's always 18 "," in each string. The idea is to erase a 0 for the second and third number and move the first number to the third column. I'll try to find a solution on google but I hope you can give me a hand, thanks.

pipol = ["2,20000,9000,0,CORTE ESPECIAL,0,,0,0,0,0,0,0,0,0,0,0,0,0",  
"1,14640,9000,3,R L2 18 4 8,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"12,14640,4250,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0",
"2,7170,1200,15,,0,,0,0,0,0,0,0,0,0,0,0,0,0",
"1,7170,8830,15,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"4,8700,1200,11,R L2 15 4 8,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"4,6620,1200,3,R L2 15 4 8,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"4,8340,1000,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,8700,7500,13,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"1,8700,6980,15,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"5,6620,1000,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"1,11120,8760,1,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"1,8760,7230,1,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,14640,700,1,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,14640,8760,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0"]

The output should be :

2000    900    2
1464    900    1
1464    425    12
717    120    2
717    883    1
870    120    4
662    120    4
834    100    4
870    750    2
870    698    1
662    100    5
1112    876    1
876    723    1
1464    70    2
1464    876    2

This is my advance:

pipol = ["2,20000,9000,0,CORTE ESPECIAL,0,,0,0,0,0,0,0,0,0,0,0,0,0",  
"1,14640,9000,3,R L2 18 4 8,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"12,14640,4250,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0",
"2,7170,1200,15,,0,,0,0,0,0,0,0,0,0,0,0,0,0",
"1,7170,8830,15,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"4,8700,1200,11,R L2 15 4 8,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"4,6620,1200,3,R L2 15 4 8,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"4,8340,1000,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,8700,7500,13,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"1,8700,6980,15,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"5,6620,1000,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"1,11120,8760,1,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"1,8760,7230,1,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,14640,700,1,,0,,0,0,0,0,0,0,0,0,0,0,0,0", 
"2,14640,8760,3,,0,,0,0,0,0,0,0,0,0,0,0,0,0"]

pipol = [elem.replace("00", "0") for elem in pipol]
pipol = [elem.replace(",0", "") for elem in pipol]
pipol = [elem.replace(",,", "") for elem in pipol]
pipol = [elem.replace(",", "    ") for elem in pipol]
for x in pipol:
    print(x)

and his output

2    200    900    CORTE ESPECIAL    
1    14640    900    3    R L2 18 4 8    
12    14640    4250    3
2    7170    120    15
1    7170    8830    15
4    870    120    11    R L2 15 4 8    
4    6620    120    3    R L2 15 4 8    
4    8340    100    3
2    870    750    13
1    870    6980    15
5    6620    100    3
1    11120    8760    1
1    8760    7230    1
2    14640    70    1
2    14640    8760    3

CodePudding user response:

Using an explicit for loop:

In [3]: new_list = []
In [4]: for i in pipol:
   ...:     temp = i.split(",")
   ...:     new_list.append([int(temp[1][:-1]), int(temp[2][:-1]), 
int(temp[0])])
   ...:

In [5]: new_list
Out[5]:
[[2000, 900, 2],
 [1464, 900, 1],
 [1464, 425, 12],
 [717, 120, 2],
 [717, 883, 1],
 [870, 120, 4],
 [662, 120, 4],
 [834, 100, 4],
 [870, 750, 2],
 [870, 698, 1],
 [662, 100, 5],
 [1112, 876, 1],
 [876, 723, 1],
 [1464, 70, 2],
 [1464, 876, 2]]

CodePudding user response:

Here is mine:

# Take every element of your list
for e in pipol:
    ln = e.split(',')            # Split them using ',' as the separator
    l1 = str( int(ln[1]) / 10 )  # Removing a 0 is, in your case, dividing by 10
    l2 = str( int(ln[2]) / 10 )
    l3 = ln[0]
    print( '\t'.join( (l1, l2, l3 ))) # Output the result
  • Related