简体   繁体   中英

how to split the list and get the last value in python

I have a list in this format. input: ['210#680#200#553','211#680#230#554','211#682#210#557']

I want to grep the last number values based on the delimiter value of each element in my list. my expected output is

output = ['553','554','557']

How to use python code to get this logic?

This answer certainly isn't as concise as the other one, but hopefully it allows you to understand the process more clearly.

lst =  ['210#680#200#553','211#680#230#554','211#682#210#557']
return_list = []

for string in lst:
    last_num = string.split("#")[-1] #Splits string and gets last element
    return_list.append(last_num) #Adds last element to return_list
    
print(return_list) #Prints ['553', '554', '557']

An idea is to look for what is called list comprehension , which is a way to easily process each element of a list in python. The result will be a new list whose i-th element will correspond to the result of the aforementioned processing of the i-th element of the initial list.

In your problem, you could use something like:

output = [x.split("#")[-1] for x in input_list]

output = [w.split("#")[-1] for w in input]

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