Python 如何剥离这些0值而不剥离以零结尾的非零值?

Python 如何剥离这些0值而不剥离以零结尾的非零值?,python,Python,我正在处理心率数据,我想去掉当天心率从未达到的数字 一些代码: result_list = [ '0 instances of 44 bpm', '0 instances of 45 bpm', '10 instances of 46 bpm', '22 instances of 47 bpm', '354 instances of 65 bpm', '20 instances of 145 bpm' ] strip_zero = [x

我正在处理心率数据,我想去掉当天心率从未达到的数字

一些代码:

result_list = [
    '0 instances of 44 bpm', 
    '0 instances of 45 bpm', 
    '10 instances of 46 bpm', 
    '22 instances of 47 bpm', 
    '354 instances of 65 bpm', 
    '20 instances of 145 bpm'
]

strip_zero = [x for x in result_list if not '0 instances' in x]

print(strip_zero)
结果:

['22 instances of 47 bpm', '354 instances of 65 bpm']
如果我使用这个:
“\'0实例”
而不是这个:
“0个实例”


0个实例均未被删除

请改用
startswith

result_list = [
    '0 instances of 44 bpm', 
    '0 instances of 45 bpm', 
    '10 instances of 46 bpm', 
    '22 instances of 47 bpm', 
    '354 instances of 65 bpm', 
    '20 instances of 145 bpm'
]

strip_zero = [x for x in result_list if not x.startswith('0 instances')]

print(strip_zero)

改用
startswith

result_list = [
    '0 instances of 44 bpm', 
    '0 instances of 45 bpm', 
    '10 instances of 46 bpm', 
    '22 instances of 47 bpm', 
    '354 instances of 65 bpm', 
    '20 instances of 145 bpm'
]

strip_zero = [x for x in result_list if not x.startswith('0 instances')]

print(strip_zero)

您还可以拆分数字(第一个空格之前的任何数字)并检查它是否为零:

如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
结果列表=[
“44 bpm的0个实例”,
“0个45 bpm的实例”,
“10个46 bpm实例”,
“47 bpm的22个实例”,
“354个65 bpm的实例”,
“145 bpm的20个实例”
]
如果r.split('',1)[0]!='0']
打印(非零)
输出:

[
'10 instances of 46 bpm', 
'22 instances of 47 bpm', 
'354 instances of 65 bpm', 
'20 instances of 145 bpm'
]

您还可以拆分数字(第一个空格之前的任何数字)并检查它是否为零:

如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
结果列表=[
“44 bpm的0个实例”,
“0个45 bpm的实例”,
“10个46 bpm实例”,
“47 bpm的22个实例”,
“354个65 bpm的实例”,
“145 bpm的20个实例”
]
如果r.split('',1)[0]!='0']
打印(非零)
输出:

[
'10 instances of 46 bpm', 
'22 instances of 47 bpm', 
'354 instances of 65 bpm', 
'20 instances of 145 bpm'
]

我只想检查第一个字符是否等于“0”,这样就不用扫描每个字符串了

strip_zero=[x代表结果列表中的x,如果x[0]!='0']

应该更快,更容易阅读。

我只需要检查第一个字符是否等于“0”,这样就不用扫描每个字符串了

strip_zero=[x代表结果列表中的x,如果x[0]!='0']

应该更快,更易于阅读。

您可以将正则表达式与一起使用。可能的重复项您可以将正则表达式与一起使用。可能的重复项