zoukankan      html  css  js  c++  java
  • Python小技巧

    1.查看变量所占内存大小

    1 import sys
    2 a,b,c= 'I','love','python'
    3 print(sys.getsizeof(a)) # 50 ,查看变量占用的内存,不同的字符串消耗不同的内存

    2.翻转字符串

     1 List = ['I','love','python'] 

    方法1

    List.reverse() # 翻转,自身的改变 ['python','love','I']
    
    

    方法2

    List = List[::-1] # ['python','love','I']

    3,字符串切片

    1 List = List[::1] # ['I', 'love', 'python']
    2 List = List[::2] # ['I', 'python'] 切片,步长为2
    3 List = List[::3] # ['I'] 步长为3

    4.字符串连接

    print(' '.join(List)) # I love python 使用空格组合列表中的字符串

    5.转换嵌套列表

    import itertools
    List = [[1,2],[3,4],[5,6]]
    l=list(itertools.chain.from_iterable(List)) 
    print(l)# [1, 2, 3, 4, 5, 6]

    6.转置矩阵

    matrix = [[1,2,3],[4,5,6]]
    print(list(zip(*matrix))) # [(1, 4), (2, 5), (3, 6)]

    7.比较列表,取交集,差集比较来比较

    a = ['I','love','PYTHON','love']
    b = ['I','love','python']
    print(set(a))# {'PYTHON', 'love', 'I'} 去重
    print(set(a).difference(set(b))) # {'PYTHON'}
    print(set(a).intersection(b)) # {'love', 'I'}

     8.牛客上标准的输入

    import sys
    n = sys.stdin.readline()
  • 相关阅读:
    http
    jquery
    wsgi
    urls控制器
    模板template
    ORM
    C++中获取汉字拼音首字缩写/全拼及生僻字的处理
    C语言函数strstr
    CString 成员函数用法
    判断字符串中是否存在中文
  • 原文地址:https://www.cnblogs.com/shuangcao/p/12738527.html
Copyright © 2011-2022 走看看