python 坑1
1.编码解码
编码:将文字转换成字节形式 encode
name = '小冯'
print(name.encode('utf-8')) # b'\xe5\xb0\x8f\xe5\x86\xaf'
解码:将字节转换成文字形式 decode
name = '小冯'
msg = name.encode('utf-8')
print(msg.decode()) # 小冯
2.基础数据类型补充:
2.1 str:
首字母大写
name = 'xiao,feng'
print(name.capitalize()) # Xiao,feng
**每个单词首字母大写 **
print(name.title()) # Xiao,Feng
**大小写反转 **
name = 'xiao,fenG'
print(name.swapcase()) # XIAO,FENg
居中 -- 填充
print(name.center(20,'*')) # *****xiao,feng******
查找:find index
print(name.find('f')) # 返回索引值 5
print(name.find('y')) # 如果没有 则返回 -1
print(name.index('f')) # 返回索引值 5
print(name.index('y')) # 报错 ValueError: substring not found
**拼接 **
print('_'.join(name)) # x_i_a_o_,_f_e_n_g
格式化: name.format
msg = '啦啦啦{},{},{}'
print(msg.format(1,2,3)) # 啦啦啦1,2,3
msg = '啦啦啦{2},{0},{1}'
print(msg.format(1,2,3)) # 啦啦啦3,1,2
msg = '啦啦啦{q},{w},{e}'
print(msg.format(q = 5,e = 9,w = 3)) # 啦啦啦5,3,9
2.2list:
**排序(默认是升序) 降序sort(reverse=True) **
lst = [1,2,3,4,5]
lst.sort(reverse = True)
print(lst) # [5, 4, 3, 2, 1]
lst.sort()
print(lst) # [1, 2, 3, 4, 5]
反转
lst.reverse()
print(lst) # [5, 4, 3, 2, 1]
查找 index
print(lst.index(4)) # 3
print(lst.index(9)) # 报错 ValueError: 9 is not in list
**统计 count **
print(lst.count(5)) # 1
+ - * 元素都是共用的 会开辟一个新的内存空间
面试:
lst = [[]]
new_lst = lst * 5
new_lst[0].append(10)
print(new_lst) # [[10], [10], [10], [10], [10]]
lst = [1,[]]
new_lst = lst * 5
new_lst[0] = 10
print(new_lst) # [10, [], 1, [], 1, [], 1, [], 1, []]
lst = [1,[]]
new_lst = lst * 5
new_lst[1] = 10
print(new_lst) # [1, 10, 1, [], 1, [], 1, [], 1, []]
lst = [1,2,3]
new_lst = lst * 5
print(id(new_lst), id(new_lst[0])) # 1813566569096 1756851216
2.3tuple:
**(1,) # 元组 **
print(type((1,))) # <class 'tuple'>
**(1) # 括号里数据本身 **
print(type(1)) # <class 'int'>
2.4dict:
popitem 随机删除字典中的键值对
dic = {"key":1,"key2":2,"key3":56}
print(dic.popitem()) # ('key3', 56)
print(dic) # {'key': 1, 'key2': 2}
**fromkeys("可迭代的键",共用的值) -- 坑 **
dic = {}
dic.fromkeys("123",[23]) # 批量添加键值对{"1":[23],"2":[23],"3":[23]}
print(dic) # {}
dic = dict.fromkeys("123456789",1) # 批量添加键值对"键是可迭代对象",值 -- 会被共用
dic["1"] = 18
print(dic) # {'1': 18, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1, '9': 1}
dic(key = 1,key2 =2)
print(dict(key = 1,key2 = 2)) # {'key': 1, 'key2': 2}
2.5set:
**set() -- 空集合 **
set("alex") # 迭代添加
3.坑
print(list('qwe')) # ['q', 'w', 'e']
print(tuple('qwe')) # ('q', 'w', 'e')
print(dict('qwe')) # ValueError: dictionary update sequence element #0 has length 1; 2 is required
print(set('qwe')) # {'q', 'e', 'w'}
lst = [1,2]
for i in lst:
lst.append(3)
print(lst) # 死循环
lst = [1,2,3,4]
for i in lst:
lst.pop()
print(lst) # [1, 2]
lst = [1,2,3,4]
for i in lst:
lst.pop()
print(lst) # [1, 2]
lst = [1,2,3,4]
for i in lst:
lst.pop(0)
print(lst) # [3, 4]
lst = [1,2,3,4]
for i in lst:
lst.remove(i)
print(lst) # [2, 4]
列表删除 -- 从后向前删除
lst = [1,2,3,4,5]
for i in range(len(lst)):
lst.pop()
print(lst)
lst = [1,2,3,4,6]
for i in range(len(lst)-1,-1,-1):
del lst[i]
print(lst)
lst = [1,2,3,4,6]
for i in range(len(lst)):
del lst[-1]
print(lst)
创建一个新的列表,删除旧的列表
lst = [1,2,3,4,5,6]
lst1 = lst.copy()
for i in lst1:
lst.remove(i)
print(lst)
**字典删除 -- 循环的时候不能改变源数据的大小 (可以改变值) **
dict.fromkeys('123456',[])
**创建一个新的字典,删除旧的字典 **
dic = dict.fromkeys("12345",1)
dic1 = dic.copy()
for i in dic1:
dic.pop(i)
print(dic)
集合删除 -- 循环的时候不能改变源数据的大小
4.类型转换:
**list -- str join **
str -- list split
set - list
list - set
5.数据类型:
可变:list ,dict ,set
不可变:int bool str tuple
有序:list,tuple,str,int,bool
无序:dict,set
取值方式:
索引: str list tuple
直接: set ,int ,bool
**键: dict **
bool: False
数字: 0
字符串: ""
列表:[]
元组:()
字典:{}
集合: set()
其他: None
python 坑1的更多相关文章
- Pythonの坑
Python closures and late binding A closure occurs when a function has access to a local variable fro ...
- 【python坑记录】
python的sort函数使用的时候有一个参数cmp.一定注意这里返回值要用1和-1.不能True和False!!!
- Python坑系列:可变对象与不可变对象
在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...
- ArcGIS 字段计算器 Python 坑
最近要处理个简单数据,一个字段中为文本类型,包含各种描述.要求是包含平方米的数值提取出来,变成数值,如果包含多个,则把各个值累加起来. 比如 字段值为 “非法占用100平方米” 处理后结果为 100 ...
- python坑之input获取字符串
space = input("set user quotation:").strip() quotation = int(space* 1024 * 1024) print(quo ...
- IEEE Bigger系列题解
Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...
- 关于python数据序列化的那些坑
-----世界上本来没那么多坑,python更新到3以后坑就多了 无论哪一门语言开发,都离不了数据储存与解析,除了跨平台性极好的xml和json之外,python要提到的还有自身最常用pickle模块 ...
- 安装python爬虫scrapy踩过的那些坑和编程外的思考
这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...
随机推荐
- php 常用操作数组函数
我们有很多操作数组的元素,我们这一节先讲一些.在6.3里面我们会总结更多的数组常用函数.深圳dd马达 下面的几个主要是移动数组指针和压入弹出数组元素的和个函数. 函数 功能 array_shift 弹 ...
- .net Web 项目的文件/文件夹上传下载
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
- am335x system upgrade set/get current cpufreq(二十一)
1 Scope of Document This document describes am335x cpufreq technology insider. 2 Requireme ...
- rbenv mac&&linux 安装简单说明
mac 可以通过brew linux 官方提供了运行脚本 # with curl curl -fsSL https://github.com/rbenv/rbenv-installer/raw/mas ...
- Python入门(一)-打开世界之Hello World
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来用Python向世界说声Hello World,人生 ...
- coci2011 debt 还债
coci2011 debt 还债 Description 有N个人,每个人恰好欠另一个人Bi元钱,现在大家都没有钱,政府想要给其中一些人欠,使得大家都不欠别人钱. 如A欠B 50,B欠C 20,则当政 ...
- vim配置无插件
其实,vim插件会影响编辑器的启动速度,虽然有些插件影响不大,我依然觉得不够,其实通过简易的状态栏,可以显示必要的信息,能自定义颜色和背景甚至透明就足够了. 一.自定义状态栏其实以下内容可以写在一行上 ...
- 【2019.11.20】SDN上机第4次作业
安装OpenDayLight控制器 配置JAVA环境 https://www.opendaylight.org/ 在官网进行下载OpenDayLight控制器 启动OpenDayLight控制器和安装 ...
- Deep Reinforcement Learning with Iterative Shift for Visual Tracking
Deep Reinforcement Learning with Iterative Shift for Visual Tracking 2019-07-30 14:55:31 Paper: http ...
- android: android 布局中的weight 属性
android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异. 示例1:将宽度设置为包裹类型wrap_content或0dp <?xml version=" ...