简体   繁体   中英

How to split string in python

I am trying to display the result that has been solved using a problem solver of pulp

so I used for loop command to display all optimal slotuin

  for vi in prob.variables():
            if vi.varValue == 1:
                print(vi.name)        

and this a sample from the output

Days_1_7251___MLK

Days_1_7251___NTI

Days_1_7269___MED

Days_1_7271___MED

Days_1_7271___MLK

Days_1_7271___RCO

What I need is to split the result and get the value after the second underscore

EX:

Orginal output: Days_1_7271___MLK

The value after extracting: 7271___MLK

I tried the below command to split, but I got only the number (7271)

vi.name.split("_")[2];

This will work in that case

"_".join(vi.name.split("_")[2:])

You can use the maxsplit argument with split() to only split till second _ :

x = "Days_1_7251___MLK"
data = x.split('_', maxsplit=2)[-1] # 7251___MLK

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