zoukankan      html  css  js  c++  java
  • 基础数据补充

    基础数据补充

    str:

    1.capitalize() ——首字母大写

    2.title()——每个单词首字母大写

    3.swapcase()——大小写反转

    4.center——居中

    name = "alex"
    print(name.center(20))
            alex        
    print(name.center(20,"*"))
    ********alex********
    
    

    5.find()——查找从左向右只查找一个,没有返回-1

    index()——查找从左向右只查找一个,没有就报错

    6."_".join(可迭代对象,对象都为字符串)——拼接

    7.name.format()格式化输出

    • %s
    • f
    • name.format
    name = "alex{},{},{}"
    print(name.format(1,2,3))#按照顺序位置进行填充
    #结果:alex1,2,3
    name = "alex{2},{0},{1}"
    print(name.format("a","b","c"))#按照索引值进行填充
    #结果:alexc,a,b
    name = "alex{a},{b},{c}"
    print(name.format(a = 1,c = 11,b = 67))#按照关键字进行填充
    #结果:alex1,67,11
    

    list——

    字符串的+ 和 - 都会开辟新的空间

    定义方式

    list(“123”)

    其他方法

    1.sort()——默认升序

    lst = [ 1 , 52 , 465 , 65]
    lst.sort()
    print(lst)
    #结果:[1, 52, 65, 465]
    lst = ["你好","我好","大家好"]
    lst.sort()#按照编码排序
    
    lst = [ 1,2,3,4,5,6,7,8,9]
    lst.reverse()#反转
    
    lst.sort(reverse=True)#降序
    
    
    

    列表的加

    方式一

    lst = [1,2,3]

    lst1 = [4,5,6]

    lst.extend(lst1)

    方式二:

    print(lst+lst1)
    
    new_lst = lst *5
    
    print(id(new_lst[0]) is new_lst[-4])
    

    面试题

    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)
    

    tuble

    可以加 可以乘 与列表一样 不可变共用 可变也共用

    tu = (1)#数据类型()中的数据类型
    print(type(tu))
    #<class, 'int'>
    tu = (1,)#元组
    

    dict

    定义一个字典:

    print(dict(k=1,l=1))
    #{'k': 1, 'l': 1}
    

    随机删除 popitem 有返回值,以元组的形式返回的是被删除的键值对

    python3.6默认删除最后一个 字典默认有序但是字典是无序的

    fromkeys(可迭代对象,值被共享)#批量添加键值对,得有变量接受

    dic = {}
    dic1 = dic.fromkeys("123",[23])
    print(dic)
    print(dic1)
    #{}
    #{'1': [23], '2': [23], '3': [23]}
    
    dic = {}
    dic = dic.fromkeys("123",[23])#批量添加键值对,键是可迭代对象,值会被共用,尽量不要写可变数据类型
    print(dic)
    dic["1"].append(5)
    print(dic)
    {'1': [23], '2': [23], '3': [23]}
    {'1': [23, 5], '2': [23, 5], '3': [23, 5]}
    
    

    set

    set()——空集合

    {}——空字典

    定义集合

    set(”123“)迭代添加的

    bool

    bool:False

    数字:0

    字符串:”“

    列表:[]

    元组:()

    字典:{}

    集合:set()

    其他:None

    机制:3>10

    数据类型之间转化

    str——list

    name ="alex"

    name=name.split()

    列表转字符串

    lst =["2",“3”,“4”,]

    “ ”.join(lst)

    字典转字符串

    dic = {"1":2}

    print(str(dic),type(str(dic)))

    python数据类型:

    可变

    list dict set

    不可变

    int bool str tuple

    有序

    list tuple str int bool

    无序

    dic set

    取值方式

    索引: str list tuple

    直接取值:set int bool

    键:dict

    以后遇见的坑

    for 死循环

    #错误示范  列表会自动补位,列表一般从后往前删
    lst = [1,2,3,4]
    for i in lst:
        lst.pop()
    print(lst)
    
    #正确示范
    lst = [1,2,3,4]
    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)
    
    
    

    字典和集合在迭代的时候不能改变原来的大小,即不能删不能增加

    #字典的删除方式
    dic = dict.fromkeys("12345",1)
    dic1 = dic.copy()
    for i in dic1:
        dic.pop(i)
    print(dic)
    
    

    二次编码:

    ascii ——没有中文

    gbk ——英文8b(位)1字节(B) 中文16b 2B

    unicode——英文16b 2B 中文 32b 4B

    utf-f——英文8b 1B 欧洲文16b 2B 亚洲文 24b 3B
    encode("utf-8")#编码

    name = "alex"
    s1 = name.encode("utf-8")
    #b'alex'  b——bytes
    print(s1.decode("utf_8"))
    #alex
    

    decode#解码 以什么编码集进行编码,就要用什么进行解码

  • 相关阅读:
    剑指office--------重建二叉树
    剑指office--------二进制中1的个数
    剑指office--------最小的K个数 (待补充)
    剑指office--------二维数组的查找
    剑指office--------替换空格
    Redis集群
    一致性hash算法
    Zab协议(转)
    Redis线程模型
    Http Cookie和session
  • 原文地址:https://www.cnblogs.com/ciquankun/p/11176570.html
Copyright © 2011-2022 走看看