假设

l = ['abc', 'mn', 'aq', 'liuming']

 

我要过滤出以a开头的元素,方法有以下两种

 

方法1:

l = ['abc', 'mn', 'aq', 'liuming']
list1 = [e for e in l if e.startswith('a')]
print(list1)  #['abc', 'aq']

 

方法2:

def f(n):
    return  n.startswith('a')
 l = ['abc', 'mn', 'aq', 'liuming']
 r = filter(f, l)
 print(list(r)) #['abc', 'aq']

 

好了, 今天就get到这么点东西!

相关文章:

  • 2021-04-23
  • 2021-05-25
  • 2022-02-14
  • 2022-01-15
  • 2021-09-27
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
相关资源
相似解决方案