python列表推导式的结构探究

美女程序员鼓励师

1、列表推导式结构包含在一对方括号中,一个表达式,后面是for子句,然后是零个或多个for或if子句。

2、其结果将是一个新列表,根据for和if子句的内容计算表达式。

实例

from collections import Counter

 

def filter_unique(lst):

  temp_list = []

  for item, count in Counter(lst).items():

    if count > 1:

      temp_list.append(item)

  return temp_list

 

# EXAMPLES

filter_unique([1, 2, 2, 3, 4, 4, 5]) # [2, 4]

以上就是python列表" title="python列表">python列表推导式的结构探究,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

以上是 python列表推导式的结构探究 的全部内容, 来源链接: utcz.com/z/546275.html

回到顶部