Python進(jìn)階之如何快速將變量插入有序數(shù)組
在我們學(xué)習(xí)python的過程中,學(xué)習(xí)序列是一門必修課。當(dāng)我們掌握了序列過后,便會(huì)學(xué)習(xí)常用的兩個(gè)排序函數(shù)sort()與sorted()。但很少有入門的課程介紹兩個(gè)插入數(shù)列的常見函數(shù),今天我們就來一起看一看如何快速將變量插入有序數(shù)組。
利用sort與sorted排序
原地修改與生成新變量
在我們學(xué)習(xí)python的過程中,列表的快速排序函數(shù)是我們的必修課。想要介紹快速插入有序數(shù)列的方法,我們首先來看兩個(gè)排序函數(shù)的區(qū)別與聯(lián)系。首先我們來看sort(),請看下面的代碼:
import random # 隨機(jī)生成10個(gè)100以內(nèi)的整數(shù) example_list = [random.randint(1,100) for i in range(10)] # 對他們進(jìn)行排序 example_list.sort() print(example_list) >>> [22, 28, 35, 47, 49, 55, 68, 79, 87, 98]
要注意的是,這里的**sort()**函數(shù)并不會(huì)有任何的返回值,而是進(jìn)行原地的排序,請看下面的代碼:
import random example_list = [random.randint(1,100) for i in range(10)] example_list_sort_test = example_list.sort() print(example_list_sort_test) >>> None
當(dāng)我們利用一個(gè)新的變量接收排序后的內(nèi)容時(shí),我們發(fā)現(xiàn)我們得到了None。但**sorted()**與其恰恰相反,其會(huì)新生成一個(gè)變量用來儲(chǔ)存排序后的列表,請看下面的代碼:
import random example_list = [random.randint(1,100) for i in range(10)] example_list_sorted_test = sorted(example_list) print(example_list_sorted_test) >>> [6, 14, 14, 20, 28, 50, 58, 58, 71, 83]
可以看到,我們使用**sorted()**進(jìn)行排序時(shí),生成了新的變量儲(chǔ)存并被我們獲取到了。
常用參數(shù)
當(dāng)然,兩個(gè)排序函數(shù)使用的參數(shù)有很多的相同的內(nèi)容,我們看下面這個(gè)例子:
import random # 導(dǎo)入 random 模塊,用于生成隨機(jī)數(shù)
# 創(chuàng)建一個(gè)包含 10 個(gè)隨機(jī)整數(shù)的列表,每個(gè)數(shù)的范圍在 1 到 100 之間
example_list_argTest = [random.randint(1, 100) for i in range(10)]
# 將列表按升序排序并打印輸出
example_list_argTest.sort()
print(example_list_argTest)
# 將列表按降序排序并打印輸出
example_list_argTest.sort(reverse=True)
print(example_list_argTest)
# 創(chuàng)建一個(gè)包含三個(gè)子列表的列表
example_list_argTest_02 = [[5, 7], [1, 8], [9, 6]]
print(example_list_argTest_02)
# 對子列表按第一個(gè)元素排序并打印輸出
example_list_argTest_02.sort()
print(example_list_argTest_02)
# 對子列表按第二個(gè)元素排序并打印輸出
def takeSecond(test_list):
return test_list[1]
example_list_argTest_02.sort(key=takeSecond)
print(example_list_argTest_02)
# 創(chuàng)建一個(gè)包含四個(gè)字符串的列表
example_list_argTest_03 = ['apple', 'big apple', 'pear', 'hen']
print(example_list_argTest_03)
# 對字符串按長度排序并打印輸出
example_list_argTest_03.sort(key=len)
print(example_list_argTest_03)
>>>[4, 18, 26, 41, 43, 52, 77, 77, 97, 98]
>>>[98, 97, 77, 77, 52, 43, 41, 26, 18, 4]
>>>[[5, 7], [1, 8], [9, 6]]
>>>[[1, 8], [5, 7], [9, 6]]
>>>[[9, 6], [5, 7], [1, 8]]
>>>['apple', 'big apple', 'pear', 'hen']
>>>['hen', 'pear', 'apple', 'big apple']
其中,**sorted()**函數(shù)參數(shù)與其是相同的,下面是常用的參數(shù)值以及參數(shù)的意義:
- key: 參數(shù)可以接受一個(gè)函數(shù)作為參數(shù),該函數(shù)將應(yīng)用于列表中的每個(gè)元素以進(jìn)行排序。該函數(shù)應(yīng)該接受一個(gè)參數(shù)并返回要用于排序的值。
- reverse :一個(gè)可選參數(shù),用于控制列表排序的順序。當(dāng) reverse 為 True 時(shí),列表將按降序排列;當(dāng) reverse 為 False 或未指定時(shí)(默認(rèn)為 False),列表將按升序排列。
利用bisect將變量插入有序序列
獲取插入元素的位置
bisect 用于在已排序的列表中插入元素,并返回插入元素后列表的索引。在其中有兩個(gè)可用的函數(shù),分別是bisect_left() 和 bisect_right(),顯然其主要區(qū)別為一個(gè)會(huì)返回插入左邊的索引,一個(gè)會(huì)返回插入右邊的索引。請看下面這個(gè)例子:
import bisect example_list = [random.randint(1,100) for i in range(10)] example_list.sort() print(example_list) left_index = bisect.bisect_left(example_list_sorted_test,58) print(left_index) right_index = bisect.bisect_right(example_list_sorted_test,58) print(right_index) >>>[9, 11, 16, 22, 40, 59, 60, 68, 83, 99] >>>6 >>>8
除此之外,上述兩個(gè)函數(shù)還有兩個(gè)可選參數(shù),分別如下:
- lo 參數(shù)表示搜索范圍的起始位置,可以用于指定在列表的子區(qū)間中進(jìn)行搜索。
- hi 參數(shù)表示搜索范圍的結(jié)束位置,可以用于指定在列表的子區(qū)間中進(jìn)行搜索。
我們可以利用上述參數(shù)來選擇部分區(qū)間進(jìn)行插入,請看下面這個(gè)例子:
test_list = list(range(10)) print(test_list) # 指定區(qū)間搜索插入 bisect.bisect_left(test_list, 2, 3, 5) >>>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>3
在這個(gè)例子中,我們指定了搜索的區(qū)間插入,并返回了插入的索引位置。
利用insort將元素插入有序序列
如果要將元素插入到列表中而不破壞其排序順序,則可以使用 **insort()**函數(shù)。請看下面這個(gè)簡單的例子:
import bisect sorted_list_example = [1, 3, 4, 6, 8, 9, 11] bisect.insort(sorted_list_example, 7) print(sorted_list_example ) >>> [1, 3, 4, 6, 7, 8, 9, 11]
在上述例子中,我們將自定義的變量插入了有序數(shù)組中。
一個(gè)應(yīng)用的例子
假設(shè)我們要對輸入的成績進(jìn)行評級,其實(shí)可以用上述介紹的方法進(jìn)行編寫,請看下面這個(gè)例子:
def grade(score, breakpoints = [60,70,80,90], grades='FDCBA'):
index = bisect.bisect(breakpoints, score)
return grades[index]
random_grades = [random.randint(1,100) for i in range(10)]
print(random_grades)
print([grade(s) for s in random_grades])
>>>[27, 28, 35, 89, 20, 61, 20, 89, 53, 92]
>>>['F', 'F', 'F', 'B', 'F', 'D', 'F', 'B', 'F', 'A']
通過合理的使用上述插入序列的函數(shù),我們完成了一個(gè)成績評級的函數(shù),并返回了不同成績對應(yīng)的評級。
總結(jié)
在本文中,我們介紹了列表排序以及如何利用python內(nèi)置函數(shù)快速插入列表序列的方法。
到此這篇關(guān)于Python進(jìn)階之如何快速將變量插入有序數(shù)組的文章就介紹到這了,更多相關(guān)Python變量插入有序數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)自動(dòng)登錄人人網(wǎng)并訪問最近來訪者實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)自動(dòng)登錄人人網(wǎng)并訪問最近來訪者實(shí)例,該實(shí)例是在前面登錄人人網(wǎng)實(shí)例基礎(chǔ)上的擴(kuò)展,是非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-09-09
python中print()函數(shù)的“,”與java中System.out.print()函數(shù)中的“+”功能詳解
python中的print()函數(shù)和java中的System.out.print()函數(shù)都有著打印字符串的功能。接下來通過本文給大家分享python中print()函數(shù)的“,”與java中System.out.print()函數(shù)中的“+”功能,需要的朋友參考下吧2017-11-11
Pandas對數(shù)值進(jìn)行分箱操作的4種方法總結(jié)
分箱是一種常見的數(shù)據(jù)預(yù)處理技術(shù)有時(shí)也被稱為分桶或離散化,他可用于將連續(xù)數(shù)據(jù)的間隔分組到“箱”或“桶”中。本文將使用python?Pandas庫對數(shù)值進(jìn)行分箱的4種方法,感興趣的可以了解一下2022-05-05
jupyter %matplotlib inline報(bào)錯(cuò)TypeError:print_svg()&
在Jupyter Notebook使用matplotlib時(shí)出現(xiàn)TypeError錯(cuò)誤,一般是由于ipython和matplotlib版本不兼容造成的,通過安裝ipympl并將魔法命令替換為%matplotlib ipympl,可以解決這個(gè)問題2024-09-09
PyCharm刷新項(xiàng)目(文件)目錄的實(shí)現(xiàn)
今天小編就為大家分享一篇PyCharm刷新項(xiàng)目(文件)目錄的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python Print實(shí)現(xiàn)在輸出中插入變量的例子
今天小編就為大家分享一篇Python Print實(shí)現(xiàn)在輸出中插入變量的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

