• Python 小方法


    1.获取本地时间
    (1)
    t=time.localtime()
    a=t.tm_year
    b=t.tm_mon
    c=t.tm_mday
    d=t.tm_hour
    e=t.tm_min
    f=t.tm_sec
    format_time="{}年{}月{}日{}时{}分".format(a,b,c,d,e)
    print(format_time)
    #输出:2021年6月13日15时23分
    (2)
    date = time.localtime(time.time())
    format_time = time.strftime('%y%m%d%H%M%S', date)
    print(format_time)
    #输出:210613162826

    2.数字前面加0
    x=123
    k = str(x)
    other_url = k.zfill(5)#数字5是将x变成5位数,前面用0补齐
    print(other_url )
    #输出:00123

    3.冒泡排序
    xuhaos=[15,165,12,663,654,2,232,17,52]
    for x in range(0, len(xuhaos) - 1):
      for i in range(0, len(xuhaos) - 1):
        if xuhaos[i + 1] > xuhaos[i]:
          b = xuhaos[i]
          xuhaos[i] = xuhaos[i + 1]
          xuhaos[i + 1] = b
    print(xuhaos)
    #输出:[663, 654, 232, 165, 52, 17, 15, 12, 2]

    4.txt文件读写
    (1)写
    with open('data.txt', 'w', encoding='utf8') as f:
    或写入列表数据
      ress=[123,456,789]
      f.writelines(ress)
      f.flush()#刷新权限
      f.close()#关闭
    (2)读
    with open('call_log.txt', 'r', encoding='utf8') as f:
      f.seek(0)
      ress = []
      while True:
        res = f.readline()
        if len(res)==0:
          break
        ress.append(res)
    或指定读取多少行
    for i in range(5):
      res1 = f.readline()
      ress.append(res1)

    5.寻找字符串中字符的位置
    (1)发现"字符的多个位置
    updata='fsvf"65;,.fgbg"dfvfd"12df'
    sub = '"'
    all_index = [substr.start() for substr in re.finditer(sub, updata)]
    print(all_index )
    #输出:[4, 14, 20]
    (2)发现"字符的一个位置
    updata='fsvf"65;,.fgbg"dfvfd"12df'
    sub = '"'
    all_index =updata.find(sub)
    print(all_index )
    #输出:4

    6.文件读取内容
    path='D://demo/demo.bin'
    files = {'file': open(path, 'rb')}

    7.列表去除重复
    lists=[11,22,11,22,33,44,55,55,66]
    print(list(set(lists)))
    #输出:[33, 66, 11, 44, 22, 55]

    8.筛选数字与字母
    crazystring = 'dade142.!0142f[., ]ad'
     # 只保留数字
     new_crazy = filter(str.isdigit, crazystring)
     print(''.join(list(new_crazy))) #输出:1420142
     # 只保留字母
     new_crazy = filter(str.isalpha, crazystring)
     print(''.join(list(new_crazy))) #输出:dadefad
     # 只保留字母和数字
     new_crazy = filter(str.isalnum, crazystring)
     print(''.join(list(new_crazy))) #输出:dade1420142fad
     # 如果想保留数字0-9和小数点'.' 则需要自定义函数
     new_crazy = filter(lambda ch: ch in '0123456789.', crazystring)
     print(''.join(list(new_crazy))) #输出:142.0142.

    9.保留小数点后面几位数
    danjia=54.235
    jinjia=1.2365
    chajia = round(danjia-jinjia, 3)#数字3代表保留3位小数点
    print(chajia)
    #输出:52.998

    10.小数转换成百分数
    a = 0.3214323
    b= "%.3f%%" % (a * 100)#数字3代表百分数后面保留3位小数点
    print(b)
    #输出:32.143%

    11.99乘法表
    for y in range(1,10):
      for x in range(1,y+1):
        print('{}*{}={}'.format(x,y,x*y),end=' ')
      print(' ')

    1*1=1
    1*2=2 2*2=4
    1*3=3 2*3=6 3*3=9
    1*4=4 2*4=8 3*4=12 4*4=16
    1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
    1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
    1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
    1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
    1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

    12.字符串去掉换行或空格
    a='dddjlkn '
    strs = a.strip(' ')
    print(strs)
    #输出:dddjlkn

    13.字符串替换
    a='sfdvv'
    b=a.replace('vv','ab')
    print(b)
    #输出:sfdab

    14.随机数生成
    num=random.randint(1,10)
    print(num)
    #输出:1到10输出一个随机整数

  • 相关阅读:
    软件开发 [CJOJ 1101] [NOIP 模拟]
    OI中卡常数技巧
    疫情控制 [NOIP2012]
    开车旅行 [NOIP 2012]
    观光公交 [NOIP 2011] [思维推导]
    选择客栈 [NOIP 2011]
    2016级算法期末上机-F.中等·AlvinZH's Fight with DDLs II
    2016级算法期末上机-E.中等·ModricWang's Fight with DDLs II
    2016级算法期末上机-D.简单·AlvinZH's Fight with DDLs I
    2016级算法期末上机-C.简单·Bamboo's Fight with DDLs III
  • 原文地址:https://www.cnblogs.com/1527275083gj/p/14880632.html
Copyright © 2020-2023  润新知