python_day1
2024-05-06 04:54:19
目录
- 算术运算
- 列表方法
- 匿名函数lambda
- filter/map
- 文件
算术运算
// 只返回整数部分
% 只返回余数
**幂运算
列表方法
In [1]: #向列表添加元素In [2]: member = [1,2,3,]In [3]: member.append(4)In [4]: member
Out[4]: [1, 2, 3, 4]In [5]: #获取列表长度In [6]: len(member)
Out[6]: 4In [7]: member.append?
Signature: member.append(object, /)
Docstring: Append object to the end of the list.
Type: builtin_function_or_methodIn [8]: append?
Object `append` not found.In [9]: #扩展列表In [10]: member.extend([1,2])In [11]: member
Out[11]: [1, 2, 3, 4, 1, 2]In [12]: #参数必须是列表In [13]: #insert(位置,元素)In [14]: member.insert(1,"牡丹")In [15]: member
Out[15]: [1, '牡丹', 2, 3, 4, 1, 2]In [16]:
匿名函数lambda
**lambda的使用:**关键是给匿名函数赋值
精简代码,调用次数少的函数用lambda就可以了
In [16]: def ds(x):...: return 2*x+1...:In [17]: ds(5)
Out[17]: 11In [18]: lambda x:2*x+1
Out[18]: <function __main__.<lambda>(x)>In [19]: g=lambda x:2*x+1In [20]: g(5)
Out[20]: 11
In [21]: def add(x,y):...: return x+y...:In [22]: add_1=lambda x,y:x+yIn [23]: add_1(1,2)
Out[23]: 3
filter/map
filter筛选true/map映射
In [24]: #filter过滤器In [25]: help(filter)
Help on class filter in module builtins:class filter(object)| filter(function or None, iterable) --> filter object|| Return an iterator yielding those items of iterable for which function(item)| is true. If function is None, return the items that are true.|| Methods defined here:|| __getattribute__(self, name, /)| Return getattr(self, name).|| __iter__(self, /)| Implement iter(self).|| __next__(self, /)| Implement next(self).|| __reduce__(...)| Return state information for pickling.|| ----------------------------------------------------------------------| Static methods defined here:|| __new__(*args, **kwargs) from builtins.type| Create and return a new object. See help(type) for accurate signature.In [26]: filter(None,[1,0,True,False])
Out[26]: <filter at 0x4cdb2c8>In [27]: list( filter(None,[1,0,True,False]))
Out[27]: [1, True]In [28]: #筛选奇数的filterIn [29]: def odd(x):...: return x%2...:In [30]: temp=range(10)In [31]: filter(odd,temp)#只需要传入函数名即可,第二个参数是函数的参数,筛选tru...: e的值
Out[31]: <filter at 0x4d26c08>In [32]: show = filter(odd,temp)In [33]: list(show)
Out[33]: [1, 3, 5, 7, 9]In [34]: #代码精简版In [35]: list(filter(lambda x:x%2,range(10)))
Out[35]: [1, 3, 5, 7, 9]In [36]: #map映射In [37]: list(map(lambda x:x*2,range(10)))
Out[37]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
文件
Character Meaning--------- ---------------------------------------------------------------'r' open for reading (default)'w' open for writing, truncating the file first'x' create a new file and open it for writing'a' open for writing, appending to the end of the file if it exists'b' binary mode't' text mode (default)'+' open a disk file for updating (reading and writing)'U' universal newline mode (deprecated)
help(open)
In [2]: f = open("nihao.txt")In [3]: f
Out[3]: <_io.TextIOWrapper name='nihao.txt' mode='r' encoding='cp936'>In [4]: f.read()
Out[4]: '111111111111111\n22222222222222\n3333333333333333\n444444444444444\n555
5555555555555\nxxxxxxxxxxxx\ndddddddddddddddddd\nffffffffffffffffffff\nggggggggg
gggggggggggg\nrrrrrrrrrrrrrrrrrrrrrr\njjjjjjjjjjjjjjjjjj'In [5]: list(f)
Out[5]: []In [6]: list(f)
Out[6]: []In [7]: f.close()In [8]: f = open("nihao.txt")In [9]: f.read(5)
Out[9]: '11111'In [10]: f.tell()
Out[10]: 5In [11]: help(f.seek)
Help on built-in function seek:seek(cookie, whence=0, /) method of _io.TextIOWrapper instanceChange stream position.Change the stream position to the given byte offset. The offset isinterpreted relative to the position indicated by whence. Valuesfor whence are:* 0 -- start of stream (the default); offset should be zero or positive* 1 -- current stream position; offset may be negative* 2 -- end of stream; offset is usually negativeReturn the new absolute position.In [12]: f,seek(30,0)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-8c5a4658ebdf> in <module>
----> 1 f,seek(30,0)NameError: name 'seek' is not definedIn [13]: f = open("nihao.txt")In [14]: f.read(5)
Out[14]: '11111'In [15]: f.tell()
Out[15]: 5In [16]: f.seek(30,0)
Out[16]: 30In [17]: f.readline()
Out[17]: '2\n'In [18]: f.write('hello world')
---------------------------------------------------------------------------
UnsupportedOperation Traceback (most recent call last)
<ipython-input-18-9347961d9f7c> in <module>
----> 1 f.write('hello world')UnsupportedOperation: not writableIn [19]: f.close()In [20]: f = open("nihao.txt",'w')In [21]: f.write('hello world')
Out[21]: 11In [22]: f.close()
python_day1相关推荐
- 用于特征选择的F-Score打分及其Python实现
F-Score(非模型评价打分,区别与 F1_score )是一种衡量特征在两类之间分辨能力的方法,通过此方法可以实现最有效的特征选择.最初是由台湾国立大学的Yi-Wei Chen提出的(参考< ...
- 第一章 Python初探
目录 一.python类别 二.python编码 2.1 编码类型 2.1.1 ASCII编码 2.1.2 GB2312编码 2.1.3 GBK编码 2.1.4 ANSI(扩展的ASCII编码) 2. ...
- python中keyerror什么意思_python 的keyerror问题
我在敲<笨办法学python>这本书时遇到的问题,习题38. 我的代码如下: # create a mapping of state to abbreviation states = { ...
最新文章
- 极客新闻——19、如何从单体架构平滑过渡到微服务
- csv data set config使用介绍
- 【推荐】iOS汉字转拼音第三方库
- 沸腾了!苏宁全员涨薪,每月最高多1万6!网友:这一刻,我希望我是苏宁人...
- python 如何理解 numpy 数组操作中的 axis 参数?
- Mysql统计分组区间的人数和 1
- 隐藏SyntaxHighlighter滚动条
- ReactJs和React Native的那些事
- GitHub 免费开放开发者计划,增加系列新优势
- matlab空域内图像增强之灰度变换增强
- DOM、Window操作
- 干货 | 100亿+数据量,每天50W+查询,携程酒店数据智能平台实践
- MS Navision专业BBS
- 颁奖 | 阿D给你发年终奖啦~
- STM32时钟系统与时钟启动顺序详解
- 阜城中学2021高考成绩查询,河北省衡水市阜城中学2020-2021学年高二上学期开学考试英语试题...
- html保存至心愿单按钮,王者荣耀添加心愿单有什么用 心愿单怎么实现
- LocalDate和LocalDate比较相隔多少天及相隔几年几月几天
- 社群运营——你专属的新媒体流量入口! | 黎想
- Bottle 中文文档