zoukankan      html  css  js  c++  java
  • python.day.04——常用数据类型(一):str&List

    一、常用字符串操作

    • upper(x)把字母变成大写
    • lower(x)把字母变成小写
    str_1="hello,python,lower,'666','777'"
    #upper()把字母编程大写
    #lower()把字母变成小写
    print(str_1.upper())
    print(str_1.lower())
    • split(str,num)  对字符串进行切割,返回一个列表:str-分隔符,默认为所有的空字符,包括空格,换行(\n),制表符(\t)等;num -- 分割次数
    str_1 = "hello,python,lower,'666','777'"
    print(str_1.split('l'))   # ['he', '', 'o,python,', "ower,'666','777'"]
    print(str_1.split(','))   # ['hello', 'python', 'lower', "'666'", "'777'"]
    print(str_1.split(',', 2))   # ['hello', 'python', "lower,'666','777'"]
    • strip(chars ) 移除字符串头尾指定的字符(默认为空格或换行符)或字符序列,返回一个字符串:chars -- 移除字符串头尾指定的字符序列
    str_2="6hello,python,lower,'666','7776'"
    str_3="8888hello,python,lower,'666','77768888'"
    print(str_2.split('6'))   # ['', "hello,python,lower,'", '', '', "','777", "'"]
    print(str_3.split('8'))   # ['', '', '', '', "hello,python,lower,'666','7776", '', '', '', "'"]
    • join  把join前面的字符串/字符 跟join传进来的字符串  每一个都要进行拼接
    str_2="6hello,python,lower,'666','7776'"
    print('e6'.join(str_2))
    
    控制台输出:
    6e6he6ee6le6le6oe6,e6pe6ye6te6he6oe6ne6,e6le6oe6we6ee6re6,e6'e66e66e66e6'e6,e6'e67e67e67e66e6'

    备注:常使用来对字符进行拼接,或者将列表数据类型转换成字符串数据类型

    new_numbers = []
    for n in List:
      new_numbers.append(str(n))
    numbers = new_numbers
    num = ''.join(numbers)
    print("加密后输出的整数为:%s" % num)

    二、列表的处理

    • 切片操作
    • # 切片操作
      name = ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      print('1:', name)
      print('2:', name[0], name[2])
      print('3:', name[1:3]) #切片
      print('4:', name[-2:]) #切片
      print('5:', name[::-1])   #切片的结果是倒序排列
      print('6:', name[:-1])    #切片的结果是,输出到倒数第二位  -1-1=-2
      
      控制台输出
      1: ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      2: trek redline
      3: ['cannondale', 'redline']
      4: ['specialized', 'trek']
      5: ['trek', 'specialized', 'redline', 'cannondale', 'trek']
      6: ['trek', 'cannondale', 'redline', 'specialized']
    • 增加:在列表的后面新增元素 append();在列表的任何位置中插入元素insert();
    • name = ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      name.append("guapi")           #在列表中添加新的元素,附加到列表的末尾
      name.insert(1, "nihao")        #在列表的任何位置添加元素
      name.insert(3, "doudou")
      name[2] = "guyun"              #修改列表中的元素
    • 索引:若不知道该元素的位置可以通过index()来进行搜索
    • name = ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      print("1:", name.index("trek"))
      print("2:", name[name.index("trek")])
      
      控制台输出:
      1: 0
      2: trek
    • 删除:直接删除元素remove();指定位置开始删除pop(x):若要使用remove()进行删除,必须先索引一下自己的要删除的元素是否存在,若不存在系统会抛出异常;pop()若不含参数则,从末尾开始删除
    • name.remove("guyun")          #直接删除元素
      del name[1]                   #根据元素的下标,来删除元素
      name.pop()                    #不输入下标,删除默认最后一个值
      name.pop(1)
    • 清空列表clear();统计某个元素的个数count()
    • #name.clear()   #清空列表
      #print(name.count("trek"))   #count 统计个数
    • 反转与排序:reverse()  /  sort()
    • name.reverse()        #reverse 反转
      print(name)
      name.sort()           #sort 排序 (按照ASCTT中的排序方法)
      print(name) 
      
      控制台输出:
      ['trek', 'specialized', 'redline', 'cannondale', 'trek']
      ['cannondale', 'redline', 'specialized', 'trek', 'trek']
    • 更改:更改列表中的元素
    • list_1=[1,2,'666']
      list_1[2]='python'  #更改为字符串
      print(list_1)
      list_1[2]=666  #更改为整形
    • 复制:copy()
    • name = ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      name3 = name.copy()
      print('1:name--', name)
      print('2:name3--', name3)
      name.insert(1, "susu")
      print('3:name--', name)
      print('4:name3--', name3)
      
      import copy
      name4 = copy.copy(name)      #浅copy
      print('5:name--', name)
      print('6:name4--', name4)
      name5 = copy.deepcopy(name)   #深copy
      print('7:name--', name)
      print('8:name5--', name5)
      name6 = name[:]              #浅copy
      name7 = list(name)           #浅copy
      print('9:name6--', name6)
      print('10:name7--', name7)
      
      控制台输出:
      1:name-- ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      2:name3-- ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      3:name-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
      4:name3-- ['trek', 'cannondale', 'redline', 'specialized', 'trek']
      5:name-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
      6:name4-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
      7:name-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
      8:name5-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
      9:name6-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
      10:name7-- ['trek', 'susu', 'cannondale', 'redline', 'specialized', 'trek']
  • 相关阅读:
    Symbol
    对象的附加属性
    怪异盒模型
    javascript的三大组成部分
    让目标对象滚动到视口位置
    隐藏单个盒子的滚动条
    uni-app判断有没有安装这个app,如果有的话直接打开,没有的话跳转到下载页面
    js 读取json文件
    openlays 使用 svg标注,动态修改svg颜色
    iview table组件内容过多用“...”代替,鼠标悬停显示全部内容
  • 原文地址:https://www.cnblogs.com/Rosay9204781/p/9157307.html
Copyright © 2011-2022 走看看