#Python学习笔记(day6)——工厂函数

int(), str(), tuple(), list(), dict(), set(), forzenset()

 

int():

class int(object)
 |  int(x=0) -> integer
 |  int(x, base=10) -> integer

 

str():

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str

 

tuple():

class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items

 

list():

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items

list的列表生成式,可举一反三,类推其他工厂函数。

把生成元素放在列表生成式首位,其次是for循环或条件判断语句。

譬如生成一个1-10的平方数列表。[x * x for x in range(1,11)]

生成1-10内偶数平方的列表 [ x * x for  x in range(1,11) if x%2 == 0]

两层for循环也阔以  [ m + n for m in ['a','b'] for n in ['c','d']]

最关键的一点是要知道返回列表的元素表达式

例子:

a = [ i for i in range(100) if not (i%2) and i % 3]          #1-100内能被2整除但不能被3整除,若也能被3整除只需要在(i%3)前加not

 

 

set:

class set(object)
 |  set() -> new empty set object
 |  set(iterable) -> new set object

 

frozenset():
class frozenset(object)
 |  frozenset() -> empty frozenset object
 |  frozenset(iterable) -> frozenset object

 

dict():

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:  #二元的iterable
 |      d = {}
 |      for k, v in iterable: 
 |          d[k] = v
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
 |      in the keyword argument list.  For example:  dict(one=1, two=2)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值