Wlgls 冲鸭!

Python基础操作

2019-06-05

列表

刷leetcode的题顺便复习一下python的相关知识 本人最主要的还是让我记得一些重要内容,详细细节就不细述了,如果有需要再行记录

1、切片


>>>s = [1, 2, 3, 4]
>>>s[1:]
[2, 3, 4]

2、相关脚本操作符

>>>[1, 2, 3] + [4, 5]
[1, 2, 3, 4, 5]
>>>[0] * 2
[0, 0]

3、相关方法 append(), extend(), reverse(), insert(), remove(), pop(), insert(), count(), index()

"""
# len(list)
返回值: list长度
"""
>>>len([1])
1
>>>len([])
0

"""
# max(list)
list: 不能为空
return: 返回列表最大值
"""
>>> max([1, 2])
2
>>> max([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
>>> max([1])
1

"""
# list.append(obj)
obj: 任何对象
return: 无返回值
"""
>>> list = []
>>> list.append(1)
>>> list
[1]
>>> list.append('123')
>>> list
[1, '123']


"""  
# list.count(obj)
return: 返回元素在列表中的次数
"""
>>> list.count(2)
0
>>> list = [1, 1, 2, 3]
>>> list.count(1)
2
>>> list.count(4)
0

"""  
# list.extend(list2)
list2: 列表元素
return: 无返回值
"""
>>> list
[1, 1, 2, 3]
>>> list.extend([5, 6])
>>> list
[1, 1, 2, 3, 5, 6]
>>> list((1, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

"""  
# list.index(obj)
onj 对象
return 返回所查找对象的索引位置,如果未找到则抛出异常
"""
>>> list
[1, 1, 2, 3, 5, 6]
>>> list.index(1)
0
>>> list.index(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 4 is not in list

"""
# list.insert(index, obj)
index: 对象 obj 需要插入的索引位置。
obj: 要插入列表中的对象。
return: 无返回值
"""


"""
# list.pop([index=-1])
obj: 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值
return: 该方法返回从列表中移除的元素对象。超出界限抛出异常
"""
>>> list = [1]
>>> list.pop(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range

"""
# list.remove(obj)
obj: 列表中要移除的对象。
return: 无返回值,如果对象不存在,抛出异常
"""
>>> list.remove(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

"""
# list.reverse()
return: 无返回值,但是会反转元素
"""

"""
# list.sort(cmp=None, key=None, reverse=False)
cmp: 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
key: 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse: 排序规则,reverse = True 降序, reverse = False 升序(默认)。
return: 无返回值

"""

内置函数

官方

map()

map(function, iterable, ...)

  • function: 函数
  • iterable: 一个或多个iterable
  • return: 返回一个iterator

我们给map函数传入一个函数和一个或多个iterable,然后map将传入的函数一次作用于iterable的每个元素

需要注意的是:

如果传入多个iterable,则function必须接受这么多参数,并且,迭代器会在最短的序列耗尽时停止

它可以和lambda表达式结合

>>> def f(x):
...     return x
... 
>>> a = map(f, [1, 2])
>>> a
<map object at 0x7f6d60f15ac8>
>>> type(a)
<class 'map'>
>>> def f(x, y):
...     return x+y
... 
>>> b = map(f, [1, 2], (2, 3, 4))
>>> list(b)
[3, 5]

filter()

filter(function, iterable)

  • function:
  • iterable:
  • return: 返回一个iterator

filter()用于过滤掉不符合条件的元素,使用符合条件的元素来构建一个新的iterator。

>>> def f(x):
...     return x>0
... 
>>> a = filter(f, [-1, 0, 1, 2])
>>> type(a)
<class 'filter'>
>>> list(a)
[1, 2]

map 和 filter 的替代

虽然mapfilter都十分强大,但是由于引入了列表推导式和生成器表达式,其实两个函数变得没有那么重要了,实际上,Guido曾明确表示mapfilter引入只是为了实现lambda表达式

所以,在某些时候来使用列表推导式和生成器表达式反而是更好的替代,因为他们更容易被阅读。

>>> def foo(x):
...     return x**2
... 
>>> 
>>> list(map(foo, range(4)))
[0, 1, 4, 9]
>>> [foo(x) for x in range(4)]
[0, 1, 4, 9]
>>> 
>>> list(filter(lambda n: n % 2, range(6)))
[1, 3, 5]
>>> [i for i in range(6) if i %2]
[1, 3, 5]

eval()

zip()

chr() and ord()

id()

id([object])

  • object: 对象
  • return: 返回对象的内存地址

format()

format函数主要是通过{}:来替代以前的%。并且他更灵活。

调用此方法,我们只需要将格式化的内容的位置用{}占位,其余所有的定义都在format()函数中执行。

我们简单的举几个例子来说明他的使用方法

# 这是最常见的匹配方式,依次匹配
>>> '{},{}'.format('hello', 'world')        # 顺序匹配
'hello,world'

# 通过指定位置,我们不必要求一一对应
>>> '{0}{1}'.format('hello', 'world')       # 指定位置匹配
'helloworld'

# 而且指定位置匹配,也可以使format函数中的内容可以重复使用
>>> '{0}{1}:{1}'.format('hello', 'world')
'helloworld:world'

# 参数匹配使的format函数更加灵活
>>> '{a},{b}'.format(a='hello', b = 'world')   # 参数匹配
'hello,world'

# 参数匹配也使得我们可以通过字符串。
>>> x = {'a':'hello', 'b':'world'}
>>> '{a},{b}'.format(**x)
'hello,world'

除此之外,我们还可以更近一步来指定格式

>>> '{:b}'.format(3)
'11'

可以使我们以二进制的形式输出 官方文档