I have a list of string like this:
list = ["A", "B", "C", "E", "0,2344 | 0,234 | 0,2345 | 0,265 | 0.2235 |"]
Output expected shoud be:
list = ["A", "B", "C", "E", "0,2344", "0,234", "0,2345", "0,265", "0.2235"]
Can anyone suggest me some ways to do this?
From what I understood from the comment. You can try this.
list_a=[j.strip() for i in list_a for j in i.split('|')]
Unrelated to the question: Don't use built-in / keywords name as variable names.
you need:
new_list = []
for l in list1:
y = l.split("|")
new_list.extend([j.strip() for j in y if j])
print(new_list)
Output:
['A', 'B', 'C', 'E', '0,2344', '0,234', '0,2345', '0,265', '0.2235']
You could do something like this,
lst = ["A", "B", "C", "E", "0,2344 | 0,234 | 0,2345 | 0,265 | 0.2235 |"]
output = []
for item in lst:
if item.find("|"):
values = item.replace("|", "").strip().split()
for value in values:
output.append(value.strip())
else:
output.append(item)
print(output)
Or better you could use list comprehension like this,
lst =[subitem.strip() for item in lst for subitem in item.split('|') if subitem]
print(lst)
And output will be,
['A', 'B', 'C', 'E', '0,2344', '0,234', '0,2345', '0,265', '0.2235']
hope this helps!
ans= ''.join(i for i in '["A", "B", "C", "E", "0,2344 | 0,234 | 0,2345 | 0,265 | 0.2235 |"]'. replace('|','","'));
ans= ''.join(i for i in ans. replace(',""',''))
print(ans.replace(' ',''))
above code gives expected output as written below.
["A","B","C","E","0,2344","0,234","0,2345","0,265","0.2235"]
Please do as follows:
list = ["A", "B", "C", "E", "0,2344 | 0,234 | 0,2345 | 0,265 | 0.2235 |"]
new_list = new_list = [ch.strip() for word in list for ch in word.split('|')]
new_list.remove('')
Thanks
I want to remove | character in the last element of list 1 and split last element of list 1 into last 5 elements of list 2
尝试这个
result = list[0:-1]+list[-1].replace(' ', '').strip('|').split('|')
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.