zoukankan      html  css  js  c++  java
  • 学习python的day7之公共的方法

    公共操作

    目标:运算符,公共方法,容器类型转换

    一、运算符

    运算符 描述 支持的容器类型
    + 合并 字符串、列表、元组
    * 复制 字符串、列表、元组
    in 元素是否存在 字符串、列表、元组、字典
    not in 元素是否不存在 字符串、列表、元组、字典

    1.+:合并

    s1 = 'abc'
    s2 = 'ed2'
    print(s1+s2)
    '''
    输出:
    abced2
    '''
    
    list1 = [1, 2, 3, 4]
    list2 = [23, 'ed', 'w', 5]
    print(list1+list2)
    '''
    输出:
    [1, 2, 3, 4, 23, 'ed', 'w', 5]
    '''
    
    tuple1 = (23, 43, 54)
    tuple2 = (53, 543,5432)
    print(tuple1+tuple2)
    '''
    输出:
    (23, 43, 54, 53, 543, 5432)
    '''

    2.*:复制

    s1 = 'a'
    list1 = [1, 3, 4]
    tuple1 = ('a', 'd')
    print(s1*5)
    print(list1*3)
    print(tuple1*3)
    '''
    输出:
    aaaaa
    [1, 3, 4, 1, 3, 4, 1, 3, 4]
    ('a', 'd', 'a', 'd', 'a', 'd')
    '''

    3.in:判断是否存在     注:字典是用键查询

    s1 = 'asc'
    list1 = ['a', 'b', 'c']
    tuple1 = ('w', 's', 'r')
    dict1 = {'name':'s', 'age':18}
    print('a' in s1)
    print('b' in list1)
    print('w' in tuple1)
    print('name' in dict1)
    print('s' in dict1)
    '''
    输出:
    True
    True
    True
    True
    False
    '''

    4.not in:判断是否不存在     注:字典是用键查询

    s1 = 'asc'
    list1 = ['a', 'b', 'c']
    tuple1 = ('w', 's', 'r')
    dict1 = {'name':'s', 'age':18}
    print('aa' not in s1)
    print('bs' not in list1)
    print('wd' not in tuple1)
    print('names' not in dict1)
    '''
    输出:
    True
    True
    True
    True
    '''

    二、公共方法

    函数  描述
    len() 计算容器中的元素个数
    del 或del() 删除
    max() 返回容器中的最大值
    min() 返回容器中的最小值
    range(start,end,step) 生成从start到end的数字,步长为step,供for循环使用
    enumerate()

    函数用于将一个可遍历的数据对象(如列表元组或字符串)组合为一个索引序列,

    同时列出数据和数据下标,一般用于for循环当中 

    len():计算容器中的元素个数

    s1 = 'abcd'
    list1 = [1,2,3,4]
    tuple1 = (1,2,3,4)
    set1 = {1,2,3}
    dict1 = {'name':'z', 'age':28, 'sex':''}
    print(len(s1))
    print(len(list1))
    print(len(tuple1))
    print(len(set1))
    print(len(dict1))
    '''
    输出:
    4
    4
    4
    3
    3
    '''

    del  或del():可以删除整个容器,只有列表、字典可以只删除容器中的数据

    s1 = 'abcd'
    del s1[0]
    print(s1)
    
    list1 = [1,2,3,4]
    del list1[2]
    del list1
    print(list1)
    
    tuple1 = (1,2,3,4)
    del tuple1
    print(tuple1)
    
    set1 = {1,2,3}
    del set1
    print(set1)
    
    dict1 = {'name':'z', 'age':28, 'sex':''}
    del dict1['name']
    del dict1
    print(dict1)

    max()或min():求最大值或最小值

    s1 = 'abcd'
    list1 = [1,2,3,4]
    tuple1 = (1,2,3,4)
    set1 = {1,2,3}
    print(max(s1))
    print(min(s1))
    
    print(max(list1))
    print(min(list1))
    
    print(max(tuple1))
    print(min(tuple1))
    
    print(max(set1))
    print(min(set1))

    range(start,end,step):不包含end(即不包含结束位),如果不写start(开始位)则默认为0,不写step(步长),默认为1

    for i in range(0,5,1):
        print(i)
    '''
    输出:
    0
    1
    2
    3
    4
    '''
    
    for i in range(0,5,3):
        print(i)
    '''
    输出:
    0
    3
    '''

    enumerate():返回结果是元组,元组第一个数据是原迭代对象的数据对应下标,第二个数据是原迭代对象的数据

    语法:enumerate(可遍历对象,start=0)   注意:start参数用来设置遍历数据下标的起始值,默认为0

    list1 = ['a', 'b', 'c', 'd']
    for i in enumerate(list1):
        print(i)
    '''
    输出:
    (0, 'a')
    (1, 'b')
    (2, 'c')
    (3, 'd')
    '''
    for i in enumerate(list1,1):
        print(i)
    '''
    输出:
    (1, 'a')
    (2, 'b')
    (3, 'c')
    (4, 'd')
    '''
  • 相关阅读:
    权值线段树 学习笔记
    Codeforces Round #585 (Div. 2)
    a题解
    01Trie学习笔记
    学习笔记--线段树合并与分裂
    [NOIP10.6模拟赛]1.merchant题解--思维+二分
    [NOIP10.6模拟赛]2.equation题解--DFS序+线段树
    [NOIP10.5模拟赛]1.a题解--离散化+异或线段树
    [NOIP10.5模拟赛]3.c题解--思维
    [NOIP10.3模拟赛]3.w题解--神奇树形DP
  • 原文地址:https://www.cnblogs.com/scheduled/p/13836316.html
Copyright © 2011-2022 走看看