今天来学习几个字符串处理的技巧。
字符串,或者说字符型数据,可以是你的数据里的基因名、细胞样本名、临床信息、分组等等,但凡他们需要调整和探索,都需要用到字符串处理的函数和方法。
xs = ["The birch canoe slid on the smooth planks." ,
"Glue the sheet to the dark blue background.",
"It's easy to tell the depth of a well." ]
x = xs[0]
x
'The birch canoe slid on the smooth planks.'
len(xs)
3
len(x)
42
如果提供的参数是列表或者是字典,就返回列表或列表的长度(元素个数)。如果提供的参数是字符串,就返回字符个数。
x.split(" ")
['The', 'birch', 'canoe', 'slid', 'on', 'the', 'smooth', 'planks.']
x[4:9]
'birch'
'ch' in x
True
x.startswith("T")
True
x.endswith(".")
True
x.replace("o","A",1) #只替换一个
'The birch canAe slid on the smooth planks.'
x.replace("o","A")
'The birch canAe slid An the smAAth planks.'
x.replace("o","")
'The birch cane slid n the smth planks.'
字符串的所有属性都不支持列表,如果需要批量操作,就要用到列表推导式或者循环
[len(x) for x in xs]
[42, 43, 38]
[x.split(" ") for x in xs]
[['The', 'birch', 'canoe', 'slid', 'on', 'the', 'smooth', 'planks.'],
['Glue', 'the', 'sheet', 'to', 'the', 'dark', 'blue', 'background.'],
["It's", 'easy', 'to', 'tell', 'the', 'depth', 'of', 'a', 'well.']]