Python list lookup with partial match

后端 未结 4 1985
有刺的猬
有刺的猬 2021-01-17 10:36

For the following list:

test_list = [\'one\', \'two\',\'threefour\']

How would I find out if an item starts with \'three\' or ends with \'f

4条回答
  •  隐瞒了意图╮
    2021-01-17 11:02

    http://www.faqs.org/docs/diveintopython/regression_filter.html should help.

    test_list = ['one', 'two','threefour']
    
    def filtah(x):
      return x.startswith('three') or x.endswith('four')
    
    newlist = filter(filtah, test_list)
    

提交回复
热议问题