简体   繁体   中英

How can I replace the first element of each line in a list in PYTHON?

I'm a little beginner, i have a list and I need to change the first ";" with "\\n[" , and the third ";" with "]" i have this:

print(lista)

>A0A024;167;188;22;DiPPE
>A0AV;1;25;25;DiWC
>A0AV6;38;58;21;Diwc
>A0AV7;408;432;25;Diwc

i try:

lista1=str(lista).replace(";","\n[",1)   

but only replace the first in the list:

>A0A024
[167;188;22;DiPPE
>A0AV;1;25;25;DiWC
>A0AV6;38;58;21;DiwC
>A0AV7;408;432;25;DiwC

need to be:

>A0A024
[167;188]22;DiPPE
>A0AV
[1;25]25;DiWC
>A0AV6
[38;58]21;DiwC
>A0AV7
[408;432]25;DiwC

Create the data

parts = """A0A024;167;188;22;DiPPE
A0AV;1;25;25;DiWC
A0AV6;38;58;21;Diwc
A0AV7;408;432;25;Diwc""".split("\n")

Go over the data of lines, split at ; and recombine as wanted:

for idx,line in enumerate(parts):
    # make it a list without any ;
    pp = line.split(";")
    # make it a string and reassign into parts
    parts[idx]= pp[0] + "\n[" + pp[1] + ";" + pp[2] + "]" + ";".join(pp[3:])

print(parts)

for p in parts:
    print(p)

Output:

# data as list  
['A0A024\n[167;188]22;DiPPE', 'A0AV\n[1;25]25;DiWC', 
 'A0AV6\n[38;58]21;Diwc', 'A0AV7\n[408;432]25;Diwc']

# data linewise 
A0A024
[167;188]22;DiPPE
A0AV
[1;25]25;DiWC
A0AV6
[38;58]21;Diwc
A0AV7
[408;432]25;Diwc

You can use str.replace and a list comprehension .

  • The first replace replaces all ';' to ']' .

  • The second replaces the 3 first ']' to ';' .

  • And the last one replaces the first ';' to '\\n[' .

data = [">A0A024;167;188;22;DiPPE",
        ">A0AV;1;25;25;DiWC",
        ">A0AV6;38;58;21;Diwc",
        ">A0AV7;408;432;25;Diwc"]

res = [s.replace(';', ']').replace(']', ';', 3). replace(';', '\n[', 1) for s in data]

for s in res:
    print(s)    

You can split each line on ; and create a new line by formatting the parts:

def format_line(line):
    return '{0}\n[{1};{2}]{3};{4}'.format(*line.split(';'))

Using this function, you can do:

data = """A0A024;167;188;22;DiPPE
A0AV;1;25;25;DiWC
A0AV6;38;58;21;Diwc
A0AV7;408;432;25;Diwc"""

lines = data.split('\n')
out = '\n'.join([format_line(line) for line in lines])

Output:

print(out)

A0A024
[167;188]22;DiPPE
A0AV
[1;25]25;DiWC
A0AV6
[38;58]21;Diwc
A0AV7
[408;432]25;Diwc

here a solution with re

import re

# my guess on how lista looks like, it is more useful to show the actual variable, than the output btw.
lista = ">A0A024;167;188;22;DiPPE\n>A0AV;1;25;25;DiWC\n>A0AV6;38;58;21;Diwc\n>A0AV7;408;432;25;Diwc"

lista = lista.split("\n")
lista1 = []
for elem in lista:
    lista1.append(re.sub(r'(.+?);(.+?;.+?);', r'\1\n[\2]', elem))

print(*lista1, sep='\n')

for the first elem in the list r'(.+?);(.+?;.+?);' will match >A0A024;167;188; , and it will substitute each of the 2 groups ( >A0A024 , and 167;188 ) in the match with this pattern r'\\1\\n[\\2]' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM