Python - 使用切片取得最後K個列表項目的總和

WBOY
發布: 2023-09-06 11:17:06
轉載
1470 人瀏覽過

Python - 使用切片获取最后K个列表项的总和

在Python中,切片方法允許我們從序列(如字串、列表或元組)中提取特定元素。它提供了一種簡潔靈活的方式來處理較大序列中的子序列。在本文中,我們將探討如何使用切片操作來取得清單中最後K個元素的和。

演算法

To find the sum of the last K items in a list, we can follow a simple algorithm:

  • 接受清單和K的值作為輸入。

  • 使用切片運算子從清單中提取最後K個項目。

  • 計算提取項目的總和。

  • Return the sum as the output.

#文法

sequence[start:end:step]
登入後複製

在這裡,slice方法接受三個可選參數:

  • start (optional): The index of the element where the slice should start. If not provided, it defaults to the beginning of the sequence.

  • #end(可選):切片應該結束的元素的索引(不包括)。如果未提供,則預設為序列的末端。

  • step (optional): The step or increment value for selecting elements. If not provided, it defaults to 1.

#The start, end and step values can be positive or negative integers, allowing you to traverse the sequence in both forward and backward directions.

範例1:使用切片方法求最後K個項目的總和

透過在切片中指定負索引,我們可以從清單的末端開始向後遍歷。以下是使用切片取得最後K個清單項目的總和的語法:

In the below example, we have a list my_list containing 10 elements. We want to find the sum of the last 4 items in the list. By using the slice operator [-K:], we specify the range from the fourth −to−last element to the end of the list. The sum() function then calculates the sum of the extracted elements, resulting in 280.

my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
K = 4
sum_of_last_k = sum(my_list[-K:])
print("Sum of last", K, "items:", sum_of_last_k)
登入後複製

輸出

Sum of last 4 items: 340
登入後複製

範例2:使用集合模組中的tail函數

來自collections模組的tail函數是一種方便的方法,用於從序列中提取最後N個元素。它允許您避免使用負索引進行切片。

在下面的範例中,我們從collections模組匯入deque類,並將所需的最大長度(maxlen)指定為N。透過將numbers列表和maxlen=N傳遞給deque,我們建立一個僅保留最後N個元素的deque物件。使用list(tail_elements)將deque物件轉換為列表,可以得到尾部元素[6, 7, 8, 9, 10]。

from collections import deque

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
N = 5
tail_elements = deque(numbers, maxlen=N)
print(list(tail_elements))
登入後複製

輸出

[6, 7, 8, 9, 10]
登入後複製

Example 3: Using the islice function from the itertools module

The islice function from the itertools module allows you to extract a specific subsequence from an iterable, such as a list or string, by providing the start, stop, and step values.

#In the below example, we import the islice function from the itertools module. By passing the numbers list along with the start, stop, and step values to islice(numbers, start, stop, step), we extract the islice(numbers, start, stop, stepquep), we extract the qujence [6, 8, 10]. Converting the result to a list using list(islice(...)) enables us to print the subsequence

from itertools import islice

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
start = 5
stop = 10
step = 2
subsequence = list(islice(numbers, start, stop, step))
print(subsequence)
登入後複製

輸出

[6, 8, 10]
登入後複製

Conclusion

在本文中,我們討論如何使用切片方法來取得最後k個項目的總和。切片方法提供了一種簡潔且有效率的方式來執行此類計算,並使得取得清單最後k個項目的總和變得容易。切片方法也可以用於其他目的,如提取子序列,跳過具有步長值的元素,反轉序列,取得最後k個元素等。

以上是Python - 使用切片取得最後K個列表項目的總和的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!