<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?(附代碼)分享 | 幾個簡單易懂的Python技巧,極大的提高工作效率

          共 4438字,需瀏覽 9分鐘

           ·

          2021-08-02 23:16

          點擊左上方藍(lán)字關(guān)注我們



          全網(wǎng)搜集目標(biāo)檢測相關(guān),人工篩選最優(yōu)價值內(nèi)容

          編者薦語
          Python提供了高效的高級數(shù)據(jù)結(jié)構(gòu),還能簡單有效地面向?qū)ο?/span>編程。Python語法和動態(tài)類型,以及解釋型語言的本質(zhì),使它成為多數(shù)平臺上寫腳本和快速開發(fā)應(yīng)用的編程語言,隨著版本的不斷更新和語言新功能的添加,逐漸被用于獨立的、大型項目的開發(fā)。

          轉(zhuǎn)載自 | 機器學(xué)習(xí)初學(xué)者


          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()來進(jìn)行去重temp_set = set(my_string)# 通過join()來進(jìn)行連接new_string = ''.join(temp_set)print(new_string)--------------------------------# output# dfbcae


          04

          拆分字符串

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

          string_1 = "My name is xiao ming"string_2 = "sample, string 1, string 2"
          # 默認(rèn)的分隔符是空格,來進(jìn)行拆分print(string_1.split())
          # 根據(jù)分隔符","來進(jìn)行拆分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ìn)行排序
          根據(jù)字典當(dāng)中的值對字典進(jìn)行排序
          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

          END



          雙一流大學(xué)研究生團(tuán)隊創(chuàng)建,專注于目標(biāo)檢測與深度學(xué)習(xí),希望可以將分享變成一種習(xí)慣!

          整理不易,點贊三連↓

          瀏覽 23
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  超碰自拍网站 | 亚洲AV成人漫画无码精品网站 | 成人AC视频 | 免费看天堂的逼 | 亚洲AⅤ电影 |