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)