Python字符串操作
1.遍历
labels=['a','b','c','d']
for i in labels:
print(i,end=" ")
***
output:
a b c d
for i in range(0,len(labels)):
print(labels[i])
***
output:
a b c d
**************************enumerate************************************
gresult=[{'class': 'person', 'conf': '0.79', 'position': [655, 173, 235, 503]},
{'class': 'person', 'conf': '0.79', 'position': [579, 89, 212, 587]},
{'class': 'recyc', 'conf': '0.82', 'position': [551, 428, 166, 137]}]
for i,label in enumerate(gresult):
print(i,label)
***
output:
0 {'class': 'person', 'conf': '0.79', 'position': [655, 173, 235, 503]}
1 {'class': 'person', 'conf': '0.79', 'position': [579, 89, 212, 587]}
2 {'class': 'recyc', 'conf': '0.82', 'position': [551, 428, 166, 137]}
2.切割
懒得打了…
#!/usr/bin/python3
a = "Hello"
b = "Python"
print("a + b 输出结果:", a + b)
print("a * 2 输出结果:", a * 2)
print("a[1] 输出结果:", a[1])
print("a[1:4] 输出结果:", a[1:4])
if( "H" in a) :
print("H 在变量 a 中")
else :
print("H 不在变量 a 中")
if( "M" not in a) :
print("M 不在变量 a 中")
else :
print("M 在变量 a 中")
print (r'\n')
print (R'\n')
***
output:
a + b 输出结果: HelloPython
a * 2 输出结果: HelloHello
a[1] 输出结果: e
a[1:4] 输出结果: ell
H 在变量 a 中
M 不在变量 a 中
\n
\n
path_video="F:\\Deeplearning\\yolov5-master\\original\\d01.mp4"
name=path_video.split("\\")[-1]
***
output:
d01.mp4
name_=name.split(".")[0]
***
output:SetROI\d04
name_=name.split(".")[1]
***
output:mp4
- 设置切割次数:
s = '小明,20,北京,139XXXXXXX'
sp_lst = s.split(',', 1)[0]
print(sp_lst)
>>>'小明'
- 反向切割并设置次数
s = '小花,20,北京,138XXXXXXX'
sp_lst = s.rsplit(',', 1)[-1]
print(sp_lst)
>>>'138XXXXXXX'
3.查找
#判断列表中是否包含子串:
# 创建列表
List = ['Lu','Xiao','Yang','Qi']
# 判断列表中是否含有字符串
if 'Yang' in List:
print('1')
else:
print('0')