python高级特性和高阶函数及使用详解-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
python高级特性和高阶函数及使用详解-创新互联

python高级特性

黄浦ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

1、集合的推导式

•列表推导式,使用一句表达式构造一个新列表,可包含过滤、转换等操作。

语法:[exp for item in collection if codition]

if codition - 可选

•字典推导式,使用一句表达式构造一个新列表,可包含过滤、转换等操作。

语法:{key_exp:value_exp for item in collection if codition}


•集合推导式

语法:{exp for item in collection if codition}


•嵌套列表推导式

2、多函数模式

函数列表,python中一切皆对象。

# 处理字符串
str_lst = ['$1.123', ' $1123.454', '$899.12312']
def remove_space(str):
  """
  remove space
  """
  str_no_space = str.replace(' ', '')
  return str_no_space
def remove_dollar(str):
  """
  remove $
  """
  if '$' in str:
    return str.replace('$', '')
  else:
    return str
def clean_str_lst(str_lst, operations):
  """
    clean string list
  """
  result = []
  for item in str_lst:
    for op in operations:
      item = op(item)
    result.append(item)
  return result
clean_operations = [remove_space, remove_dollar]
result = clean_str_lst(str_lst, clean_operations)
print result

文章标题:python高级特性和高阶函数及使用详解-创新互联
文章链接:http://scgulin.cn/article/ddosip.html