python部落刷题宝备忘(一)
2016-05-17 19:42
316 查看
[size=11]
python部落网址:http://python.freelycode.com/
[/size]
python部落网址:http://python.freelycode.com/
[/size]
阅读更多
divmod(x, y) == x//y, x%y # 返回商和余数
enumerate # 给出迭代对象的序列和对应的元素
a = [2, 3, 4]
for index, item in enumerate(a):
print index
print item
eval() # 执行字符串中的表达式
x = 5
eval('x+1') == 6
execfile()
Read and execute a Python script from a file.
>>> a = {"a": 1, "b":2}
>>> getattr(a, "c", 4) #从字典a中取出相应的键值,如果没有则返回第三个参数。具体参考下面的。
>>> 4
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
hex(number) -> string # 返回十六进制
Return the hexadecimal representation of an integer or long integer.
list tuple dict 值相同,但是id不相同
int('13', base=6) = 9 # 把字符串按照base进制转换为10进制
# map 函数用法
>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
# filter 用法
>>> filter(function, range(1, 11)) # 过滤掉迭代对象里面使function为False的元素
# reduce用法 3.0以上版本需要 import functools
reduce(function, sequence)
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).
with open() as f: #使用with打开文件避免忘记close()而且打开超大文件时避免内存溢出
f.read()
pow(2, 4) == 16 # 幂运算
"".join([c for c in reversed("123")]) == '321'
round(1.639, 2) == 1.64 round(2.675, 2) == 2.67 # 0.5以上才进位
set([1,1,2]) == set([1,2])
sorted([1, 2]) == [1, 2]
x = [1, 2, 3]
y = [4, 5, 6]
zip(x, y) == [(1, 4), (2, 5)]
lists = [[]] * 3 #这里可以理解一个列表重复了三次,所以这三个列表的指向是一样的。
lists[0].append(3)
lists == [[3], [3], [3]]
lists = [[] for i in range(3)] # 这三个是独立的列表
# 字符串方法
'hello'.capitalize() == 'Hello' # 首字母大写
'g'.center(5, "o") == 'oogoo'
'google'.count('o', 0, 2) == 1 # S.count(sub[, start[, end]]) -> int S[start:end].
'usa13'.isalnum() == True
Return True if all characters in S are alphanumeric
'usa123'.islower() == True
','.join(['a', 'b']) == 'a,b'
# 如果没有参数lstrip把左边的空格去掉,如果有参数,则顺序去掉参数中的字符。
'www.example.com'.lstrip('cmowz.') == 'example.com'
'aBc'.swapcase() == 'AbC'
'4'.zfill(3) == '004' # 在字符串左边补零
# 可变参数的使用
params1 = (8, 9)
params2 = {"a": 4, "b": 5}
def minus(a, b):
return a - b
print "%d,%d" % (minus(*params1), minus(**params2))
>>> -1, -1
root ------------- lib ----------------- a.py
| |
| --------- _________
|
------main.py
main.py中第一个语句为:
from lib.a import *
则空白处的文件名为?
这是在考你package和module的概念,为了在main.py中把a.py做为module导入,那么lib目录就应该成为一个package,因此lib下面就必须要包含__init__.py文件,所以答案是“__init__.py”。
相关文章推荐
- Python错误、调试及测试相关备忘
- python 调用 lvs 脚本 [备忘]
- Python基本知识点备忘
- python爬虫备忘(4)
- windows+python环境下安装TensorFlow经验--备忘
- python 3.1下的字符串转换(备忘)
- Python 学习过程一些小的知识点汇总 备忘
- Python 编程小备忘
- python备忘六:list 列表之列表基本特性
- numpy等python相关的包安装备忘
- python中import学习备忘笔记
- Python错误、调试及测试相关备忘
- Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import(转)
- Python基本知识点备忘
- Python之备忘
- python备忘
- 【备忘】最新python数据分析升级版视频教程
- Python:xpath备忘
- 备忘--python常见面试题
- Python学习备忘1