python basic data types and data types of the supplementary set, advanced coding, a small pool of data, copying depth

1. The set (set)

Dictionary is a collection of no value, follow: unique, unordered, the elements required can hash (immutable)

Collection is a collection of disorder is variable

The method of operation of the collection:

增:
    s.update("3456")  # 迭代添加
    print(s)
    s.add("怼怼")
    print(s)
删:
    s = {1,2,3,"123",False,(1,2,3,4)}
    print(s)
    s.pop()   #随机删除
    s.remove(3) # 通过元素删除
    s.clear()    # 清空
    del s        #删除整个集合
    print(s)
改:
    先删除,在添加
查:
    for循环
    天然去重   --  唯一
其他操作:
    s1 = {1,2,3,4,5,6,7}
    s2 = {3,4,5,6}
    print(s1 - s2)  #差集
    print(s1 | s2)  #并集   (合集)
    print(s1 & s2)  #交集
    print(s1 ^ s2)  #对称差集  -- 反交集
    print(s1 > s2)  # 超集   -- 父集
    print(s1 < s2)  # 子集
    冻结集合(可变 转换成 不可变)  -- 更不常用
    f_s = frozenset({1,2,3,4,5})
    dic = {f_s:"1"}
    print(dic)

2. Supplementary data types

str:
首字母大写:
s = "alex wusir"
s1 = s.capitalize()
print(s1)

每个单词首字母大写:
s = "alex wusir"
s1 = s.title()
print(s1)

统计出现的次数:
s = "alex awusair"
print(s.count("a"))

大小写互换:
s = "alex"
print(s.swapcase())

查找:
s = "alex taibai"
print(s.find("c"))     # find 查找不到的时候返回 -1
print(s.index("C"))    # index 查找不到就报错

找到所有a的下标:
for i in range(len(s)):
    if s[i] == 'a':
        print(i)
        
list:
li = list("815678234")   # 迭代定义的方式
print(li)

统计:
print(li.count("1"))

查看:
print(li.index("1"))

反转:
li.reverse()
print(li)

排序:
li.sort()  # 升序
li.sort(reverse=True)  # 降序
print(li)

tuple:
tu = tuple("12345")   # 迭代定义元组
统计:
print(tu.count("3"))
查找:
print(tu.index("1"))

dict:
dic = dict(k=1,k1=123,k2="aa",k3=222,k4=1123123)   # 定义方式
print dic

随机删除:
dic.popitem()
python3.6版本 默认删除最后个键值对
python3.5版本以前 随机删除
print(dic)

批量创建字典:
dic = {}
dic1 = dict.fromkeys("abcdef",[4,5,6])
第一个参数可迭代对象
第二个参数是每个键对应的值 -- 用的都是同一个内存地址

set:
s = set("1234")   # 迭代定义方式
print(s)

数据类型转换:
    字符串转数字的时候必须全都是十进制数
    字符串转列表
    print(s.split())
    列表转字符串(可迭代的对象中不能出现数字)
    print(''.join(li))
    除字典外,容器数据类型之间可以直接相互转换

基础数据类型分类
str,int,bool,list,tuple,dict,set
有序:
    str,int,bool,list,tuple
无序:
    dict,set
可变:
    list,dict,set
不可变:
    str,int,bool,tuple
访问方式:
    直接访问:int,bool,set
    顺序访问:list,tuple,str
    通过键访问: dict

3. Advanced coding

encode() 编码
decode() 解码
用什么编码集编码,就用什么编码集解码

4. Small data pool:

什么是小数据池?
小数据池就是Python中一种提高效率的方式,固定数据类型使用同一个内存地址
支持类型  - int,str,bool
    数字 : -5 ~ 256 # 重点记住
    字符串:
        1.字符串在做乘法的时候总长度不能超过20,进行驻留   #重点记住
        2.自己定义的字符串 长度不限制 字符串必须(字母,数字.下划线)组          成,进行驻留
        3.特殊字符(中文除外)定义1个时候,进行驻留
        4.字符串*1 其实就是赋值

id View memory address

Analyzing the same memory address s

Determining whether or not the same value ==

note:

The same memory address, the same as the value of certain

The same value, not necessarily the same memory address

5. Copy shades:

Assignment (=): the same memory address pointed to a number of variables

Shallow copy: only the first copy of Element

Shallow copy will create a new list (container),

Elements of the newly created list of elements of the original list and use the same memory space

Deep Copy: Copy all - immutable data types is a memory address pointing to the original data,

Variable data types open up new space

Guess you like

Origin www.cnblogs.com/changyifei-8/p/11010368.html