[英]Strip specific characters in a substring
I have the following list: 我有以下清单:
dates = ["05/05/19", "05/12/18"]
And I need to strip the day, which would be the second numeric value. 我需要删除日期,这将是第二个数字值。
How would I be able to do so? 我该怎么做? Im allowed to use Numpy
我被允许使用Numpy
My attempt but it returns the same thing: 我的尝试,但返回相同的内容:
import numpy as np
dates = ["05/05/19", "05/17/18"]
dates1 = np.array(dates)
dates2 = np.char.strip(dates1, "/")
print(dates2)
Split on "/" and pick the second element. 在“ /”上分割并选择第二个元素。
days=[ x.split("/")[1] for x in dates]
days
['05', '12']
Use string split
with slicing and join
: 使用字符串
split
与切片和join
:
dates = ['/'.join(x.split('/')[::2]) for x in dates]
Or a regex: 或正则表达式:
import re
dates = [re.sub(r'/\d+/', '/', x) for x in dates]
# ['05/19', '05/18']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.