Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果列表中的值以特定字符开头,请使用列表替换这些值_Python_List_If Statement_List Comprehension_Conditional Operator - Fatal编程技术网

Python 如果列表中的值以特定字符开头,请使用列表替换这些值

Python 如果列表中的值以特定字符开头,请使用列表替换这些值,python,list,if-statement,list-comprehension,conditional-operator,Python,List,If Statement,List Comprehension,Conditional Operator,我有以下列表,我正在尝试将以23:开头的任何值替换为00:00:00.00 tc = ['00:00:00.360', '00:00:00.920', '00:00:00.060', '00:00:02.600', '23:59:55.680', '00:00:05.960', '00:00:01.040', '00:00:01.140', '00:00:01.060', '00:00:0

我有以下列表,我正在尝试将以
23:
开头的任何值替换为
00:00:00.00

tc = ['00:00:00.360',
      '00:00:00.920',
      '00:00:00.060',
      '00:00:02.600',
      '23:59:55.680',
      '00:00:05.960',
      '00:00:01.040',
      '00:00:01.140',
      '00:00:01.060',
      '00:00:01.480',
      '00:00:00.140',
      '00:00:00.280',
      '23:59:59.800',
      '00:00:01.200',
      '00:00:00.400',
      '23:59:59.940',
      '00:00:01.220',
      '00:00:00.380']
我能用正则表达式得到我需要的东西

tc = [re.sub(r'(\b23:)(.*)',r'00:00:00.00', item) for item in tc]
它给出了如下所示的预期结果

['00:00:00.360',
 '00:00:00.920',
 '00:00:00.060',
 '00:00:02.600',
 '00:00:00.00',
 '00:00:05.960',
 '00:00:01.040',
 '00:00:01.140',
 '00:00:01.060',
 '00:00:01.480',
 '00:00:00.140',
 '00:00:00.280',
 '00:00:00.00',
 '00:00:01.200',
 '00:00:00.400',
 '00:00:00.00',
 '00:00:01.220',
 '00:00:00.380']
但是如果我使用下面的方法而不使用正则表达式,那么结果就不是我所期望的

tc = [item.replace(item, '00:00:00.00') for item in tc if item.startswith('23:')]
结果:

['00:00:00.00', '00:00:00.00', '00:00:00.00']

结果列表仅包含替换项,而不是整个列表。如何使用上述方法获取完整列表?

您需要在三元运算符中包含else语句:

>>> [item.replace(item, '00:00:00.00') if item.startswith('23:') else item for item in tc]
['00:00:00.360',
 '00:00:00.920',
 '00:00:00.060',
 '00:00:02.600',
 '00:00:00.00',
 '00:00:05.960',
 '00:00:01.040',
 '00:00:01.140',
 '00:00:01.060',
 '00:00:01.480',
 '00:00:00.140',
 '00:00:00.280',
 '00:00:00.00',
 '00:00:01.200',
 '00:00:00.400',
 '00:00:00.00',
 '00:00:01.220',
 '00:00:00.380']
甚至只是:

>>> ['00:00:00.00' if item.startswith('23:') else item for item in tc]
['00:00:00.360',
 '00:00:00.920',
 '00:00:00.060',
 '00:00:02.600',
 '00:00:00.00',
 '00:00:05.960',
 '00:00:01.040',
 '00:00:01.140',
 '00:00:01.060',
 '00:00:01.480',
 '00:00:00.140',
 '00:00:00.280',
 '00:00:00.00',
 '00:00:01.200',
 '00:00:00.400',
 '00:00:00.00',
 '00:00:01.220',
 '00:00:00.380']

这与您的问题没有直接关系,但是执行
一些字符串.replace(一些字符串,不管什么)
没有多大意义,因为这会完全替换字符串。你也可以自己写
任何东西。因此,对于您的最终代码示例,这将是
tc=['00:00:00.00',对于tc-if-item.startswith('23:')中的item