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的更多相关文章

  1. Pythonの坑

    Python closures and late binding A closure occurs when a function has access to a local variable fro ...

  2. 【python坑记录】

    python的sort函数使用的时候有一个参数cmp.一定注意这里返回值要用1和-1.不能True和False!!!

  3. Python坑系列:可变对象与不可变对象

    在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...

  4. ArcGIS 字段计算器 Python 坑

    最近要处理个简单数据,一个字段中为文本类型,包含各种描述.要求是包含平方米的数值提取出来,变成数值,如果包含多个,则把各个值累加起来. 比如 字段值为 “非法占用100平方米” 处理后结果为 100 ...

  5. python坑之input获取字符串

    space = input("set user quotation:").strip() quotation = int(space* 1024 * 1024) print(quo ...

  6. IEEE Bigger系列题解

    Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...

  7. 关于python数据序列化的那些坑

    -----世界上本来没那么多坑,python更新到3以后坑就多了 无论哪一门语言开发,都离不了数据储存与解析,除了跨平台性极好的xml和json之外,python要提到的还有自身最常用pickle模块 ...

  8. 安装python爬虫scrapy踩过的那些坑和编程外的思考

    这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...

  9. Python开发者须知 —— Bottle框架常见的几个坑

    Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...

随机推荐

  1. php 正则达达示中的模式修正符

    我们通过元字符和原子完成了正则表达示的入门.有一些特殊情况我们依然需要来处理.深圳dd马达 如果abc在第二行的开始处如何匹配?我不希望正则表达示特别贪婪的匹配全部,只匹配一部份怎么办? 这个时候,我 ...

  2. Linux grep 查找字符所在文件(grep详解)

    查找字符所在文件 grep -ir "S_ROLE"  ./* -i 不区分大小写 -r 查找字符出处 -a   --text   #不要忽略二进制的数据. -A<显示行数& ...

  3. [RN] React Native 幻灯片效果 Banner

    [RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...

  4. 括号匹配(POJ2955)题解

    原题地址:http://poj.org/problem?id=2955 题目大意:给出一串括号,求其中的最大匹配数. 我这么一说题目大意估计很多人就蒙了,其实我看到最开始的时候也是很蒙的.这里就来解释 ...

  5. CSP2019自闭记

    为什么我之前没有写呢,是因为我总是考的太lj,于是就不想写了. 这次不管考没考好都要强迫自己写,因为这是第一次参加提高组+第一次参加CSP. 当然什么初赛/复赛试题/答案什么的是不会出现的. Day ...

  6. 表单提交 curl和浏览器方式

    表单被提交时,每个表单域都会被Url编码之后才在被发送. 浏览器每次向服务器发送url时,会进行编码,然后web服务器再进行解码. 所以,理论上,curl模拟登陆时,所传参数都必须urlencode一 ...

  7. uniapp - 关于调试跨域

    [] 1.右键Chrome图标 2.选中属性(R) 3.在目标(T) chrome.exe后添加 --disable-web-security --user-data-dir --allow-runn ...

  8. 自定义设置jqGrid的标头居中加粗等

    beforeRequest: function () { $("thead th").css("text-align", "center") ...

  9. Springboot属性加载与覆盖优先级与SpringCloud Config Service配置

    参考官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config. ...

  10. Mac下 python2和python3共存

    一般是python2默认安装了,python3没有安装,这时候一般使用命令:brew install python3 进行安装 不同方法安装python的路径是不一样的,如下所示: 接下来就要看具体步 ...