简体   繁体   中英

How to remove strings from a list up to a certain character?

So i have a list containing this:

list1= [ '[192.168.1.2] [Entered 123456] successful!' ,  '[192.168.1.1] [Entered 157] unwilling operator! ', '[192.168.1.3] [Entered 56]  not successful' ]

and i only need the list to have any strings up to the 2nd ']' and the list should look like this

list1= [ '[192.168.1.2] [Entered 123456]', '[192.168.1.1] [Entered 157]', '[192.168.1.3] [Entered 56]']

do you have any tips what function should i use or a loop to use, because i tried to use the string index to output up to the 2nd ']' but the strings inside the list have different lengths.

also here is a sample of what i tried for the index that didnt work for me

output_list = [word[0:30] for word in list1]

Split on the square bracket and then slice up to it

eg.

for i in list1:
    res = i.split("]")[:2]
    print(res)

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