1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        【Python】分享幾個簡單易懂的Python技巧,能夠極大的提高工作效率哦!

        共 4389字,需瀏覽 9分鐘

         ·

        2021-07-18 16:59

        今天和大家來分享幾個關(guān)于Python的小技巧,都是非常簡單易懂的內(nèi)容,希望大家看了之后能夠有所收獲。

        01

        將字符串倒轉(zhuǎn)


        my_string = "ABCDE"reversed_string = my_string[::-1]print(reversed_string)--------------------------------------# Output# EDCBA


        02

        將英文單詞的首字母大寫
        通過title()方法來實現(xiàn)首字母的大寫
        my_string = "my name is xiao ming"# 通過title()來實現(xiàn)首字母大寫new_string = my_string.title()print(new_string)-------------------------------------# output# My Name Is Xiao Ming


        03

        給字符串去重


        my_string = "aabbbbbccccddddeeeff"# 通過set()來進行去重temp_set = set(my_string)# 通過join()來進行連接new_string = ''.join(temp_set)print(new_string)--------------------------------# output# dfbcae


        04

        拆分字符串

        Python split()通過指定分隔符對字符串進行切片,默認的分隔符是" "

        string_1 = "My name is xiao ming"string_2 = "sample, string 1, string 2"
        # 默認的分隔符是空格,來進行拆分print(string_1.split())
        # 根據(jù)分隔符","來進行拆分print(string_2.split(','))------------------------------------# output# ['My', 'name', 'is', 'xiao', 'ming']# ['sample', ' string 1', ' string 2']


        05

        將字典中的字符串連詞成串


        list_of_strings = ['My', 'name', 'is', 'Xiao', 'Ming']
        # 通過空格和join來連詞成句print(' '.join(list_of_strings))-----------------------------------------# output# My name is Xiao Ming


        06

        查看列表中各元素出現(xiàn)的個數(shù)

        from collections import Counter
        my_list = ['a','a','b','b','b','c','d','d','d','d','d']count = Counter(my_list) print(count) # Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})
        print(count['b']) # 單獨的“b”元素出現(xiàn)的次數(shù)# 3
        print(count.most_common(1)) # 出現(xiàn)頻率最多的元素# [('d', 5)]


        07

        合并兩字典

        dict_1 = {'apple': 9, 'banana': 6}dict_2 = {'grape': 4, 'orange': 8}# 方法一combined_dict = {**dict_1, **dict_2}print(combined_dict)# 方法二dict_1.update(dict_2)print(dict_1)# 方法三print(dict(dict_1.items() | dict_2.items()))---------------------------------------# output # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}# {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}# {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}


        08

        查看程序運行的時間

        import time
        start_time = time.time()######################### 具體的程序..........########################end_time = time.time()time_taken_in_micro = (end_time- start_time) * (10 ** 6)print(time_taken_in_micro)

        09

        列表的扁平化
        有時候會存在列表當(dāng)中還嵌套著列表的情況,
        from iteration_utilities import deepflattenl = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]
        print(list(deepflatten(l, depth=3)))-----------------------------------------# output# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


        10

        查看列表當(dāng)中是否存在重復(fù)值


        def unique(l):    if len(l)==len(set(l)):        print("不存在重復(fù)值")    else:        print("存在重復(fù)值")
        unique([1,2,3,4])# 不存在重復(fù)值
        unique([1,1,2,3])# 存在重復(fù)值


        11

        數(shù)組的轉(zhuǎn)置


        array = [['a', 'b'], ['c', 'd'], ['e', 'f']]transposed = zip(*array)print(list(transposed)) ------------------------------------------# output# [('a', 'c', 'e'), ('b', 'd', 'f')]


        12

        找出兩列表當(dāng)中的不同元素

        def difference(a, b):    set_a = set(a)    set_b = set(b)    comparison = set_a.difference(set_b)    return list(comparison)
        # 返回第一個列表的不同的元素difference([1,2,6], [1,2,5])# [6]


        13

        將兩列表變成鍵值對
        將兩個列表合并成一個鍵值對的字典
        def to_dictionary(keys, values):    return dict(zip(keys, values))    keys = ["a", "b", "c"]    values = [2, 3, 4]print(to_dictionary(keys, values))-------------------------------------------# output# {'a': 2, 'b': 3, 'c': 4}


        14

        對字典進行排序
        根據(jù)字典當(dāng)中的值對字典進行排序
        d = {'apple': 9, 'grape': 4, 'banana': 6, 'orange': 8}# 方法一sorted(d.items(), key = lambda x: x[1]# 從小到大排序# [('grape', 4), ('banana', 6), ('orange', 8), ('apple', 9)]sorted(d.items(), key = lambda x: x[1], reverse = True) # 從大到小排序# [('apple', 9), ('orange', 8), ('banana', 6), ('grape', 4)]# 方法二from operator import itemgetterprint(sorted(d.items(), key = itemgetter(1)))# [('grape', 4), ('banana', 6), ('orange', 8), ('apple', 9)]


        15

        列表中最大/最小值的索引

        list1 = [2030507090]
        def max_index(list_test): return max(range(len(list_test)), key = list_test.__getitem__)
        def min_index(list_test): return min(range(len(list_test)), key = list_test.__getitem__)
        max_index(list1)# 4min_index(list1)# 0
        往期精彩回顧




        本站qq群851320808,加入微信群請掃碼:
        瀏覽 96
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            啊用力用力用力流了好多水 | 久免费视频 | 美女被艹网站 | 国产精品久久久久久久白皙女 | 蜜桃网站在线观看 | 不要播放器的av网站 | 亚洲一区二区三区无码在线观看 | 翔田千里| 国产精品丰满 | 国产三级视屏 |