python常用的小知识

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()








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值