欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python常用的小知识

程序员文章站 2024-02-19 12:18:16
...
from math import ceil
from collections.abc import *
import difflib
import re
import sys
import time
#from iteration_utilities import deepflatten
from collections import Counter
import random


class Example():

    #字符串反转
    def foo1(self):
        mystr = "ABC"
        return mystr[::-1]

    #首字母大写
    def foo2(self):
        my_string = "my name is chaitanya baweja"
        return my_string.title()

    #找出唯一字符
    def foo3(self):
        my_string = "aavvccccddddeee"
        my_string = set(my_string)
        return ''.join(my_string)

    #重复打印n次字符和列表
    def foo4(self):
        n  =3
        my_string = "abcd"
        my_list = [1, 2, 3]

        return my_string*n,my_list*n
    #列表生成
    def foo5(self):
        my_list = [1,2,3,4]

        dencen_list = [i*2 for i in my_list]
        return dencen_list

    #字符串拆分列表
    def foo6(self):
        string_1 = "My name is Chaitanya Baweja"
        string_2 = "sample/ string 2"
        print(string_1.split())
        print(string_2.split('/'))

    #多个字符串组合一个字符
    def foo7(self):
        list_of_strings = ['My', 'name', 'is', 'Chaitanya', 'Baweja']
        print("".join(list_of_strings))

    #检查字符串是否是回文
    def foo8(self):
        my_string = "abcba"
        if my_string == my_string[::-1]:
            print("是回文")
        else:
            print("不是回文")

    #统计列表汇总元素出现的次数
    def foo9(self):

        my_list = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd']

        counter = Counter(my_list)
        print(counter)
        print(counter['b'])
        print(counter.most_common(1))


    #判断两个字符串中每个单词出现的次数是否相同
    def foo10(self):
        str_1, str_2, str_3 = "acbde", "abced", "abcda"
        cnt_1,cnt_2,cnt_3 = Counter(str_1),Counter(str_2),Counter(str_3)
        if cnt_1 == cnt_2:
            print("cnt1和cnt2相同")
        elif cnt_1 == cnt_3:
            print("cnt1和cnt3相同")

    #使用枚举函数得到key ,value
    def foo11(self):
        my_list = ['a', 'b', 'c', 'd', 'e']
        for key,value in enumerate(my_list):
            print("{0}:{1}".format(key,value))

    #检查对象的内存使用情况
    def foo12(self):
        nums = 21
        print(sys.getsizeof(nums))

    #字典合并,键值相同的不合并
    def foo13(self):
        dict_1 = {'apple': 9, 'banana': 6}
        dict_2 = {'banana': 4, 'orange': 8}
        return {**dict_1,**dict_2}

    #计算代码执行一段时间之后的时间
    def foo14(self):
        start_time = time.time()
        # Code to check follows
        for i in range(10 ** 5):
            a, b = 1, 2
            c = a + b
        # Code to check ends
        end_time = time.time()
        time_taken_in_micro = (end_time - start_time) * (10 ** 6)

        print(time_taken_in_micro)

    #列表展开,多层嵌套的和单层的
    def foo15(self):
        l = [[1, 2, 3], [3]]
        return [item for sublist in l for item in sublist]
        l = [[1, 2, 3], [4, [5], [6, 7]], [8, [9, [10]]]]
        print(list(deepflatten(l)))

    #列表采样
    def foo16(self):
        my_list = ['a', 'b', 'c', 'd', 'e']
        num_samples = 2
        sample = random.sample(my_list,num_samples)
        print(sample)

    #整数转成数字列表
    def foo17(self):
        num = 123456
        num_str = list(map(int,str(num)))
        return num_str


    def foo18(self):
        l = [1,2,3,4,4]
        if len(l) == len(set(l)):
            print("唯一")
        else:
            print("不唯一")




if __name__=='__main__':
    example = Example()
    #print(example.foo1())
    #print(example.foo2())
    #print(example.foo3())
    #print(example.foo4())
    #print(example.foo5())
    #example.foo6()
    #example.foo7()
    #example.foo8()
    #examp#le.foo9()
    #example.foo10()
    #example.foo11()
    #example.foo12()
    #print(example.foo13())
    #example.foo14()
    #print(example.foo15())
    #example.foo16()
    #print(example.foo17())
    example.foo18()








相关标签: 算法

上一篇: MIME

下一篇: php时间函数用法分析