第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 while 循環(huán)對給定范圍內(nèi)的列表值求和

使用 while 循環(huán)對給定范圍內(nèi)的列表值求和

Smart貓小萌 2023-06-13 19:14:31
我正在嘗試對列表的值求和,但前提是列表值在特定范圍內(nèi)(例如在 5 和 -4 之間,或<= 0)使用while循環(huán)來解決這個問題。不要使用額外的列表。如果我使用is smaller than在檢查列表值的語句中,代碼將不起作用。使用“<”時我哪里出錯了?我的示例數(shù)據(jù):# listulis = [ 5 , 4 , 4 , 3 , 1 , -2 , -3 , -5 , -7 , -7 ] #e      0   1   2   3   4    5    6    7    8    9# list lengthulis_l  = len (ulis)# to count totaltotal_1 = 0# index nr liste       = 0# list of numbers used for total lprt    = []while與> = 0;一起使用 這有效:print ( " # while command in progress " )while ( ulis[e] >= 0) and ( e < ulis_l ):       total_1 = total_1 + ulis [e]    lprt.append (ulis [e])    e = e + 1    print (total_1)相同的代碼,>= 0更改為<= 0不起作用,我不明白發(fā)生了什么:print ( " # while command in progress " )while ( ulis[e] <= 0) and ( e < ulis_l ):    total_1 = total_1 + ulis [e]    lprt.append (ulis [e])    e = e + 1    print (total_1)我用這段代碼來檢查輸出:print ( " " )print ( " " )print ( " # Total sum " )print (total_1)print ( " " )print ( " # values used form org list ulis for Total sum " )print (lprt)print ( " " )print ( " # ulis n org values " )print ( ulis_l )對于第一個循環(huán),打印如下: # while command in progress 59131617 # Total sum17 # values used form org list ulis for Total sum[5, 4, 4, 3, 1] # ulis n org values10但對于第二個循環(huán),我看到: # while command in progress  # Total sum0 # values used form org list ulis for Total sum[] # ulis n org values10
查看完整描述

2 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

不要使用while,它會在第一個錯誤結果處停止。您的循環(huán)不起作用,因為您測試的第一個值大于零,因此不滿足循環(huán)條件:


>>> ulis[0] <= 0

False

當不應將值添加到總數(shù)時,您不想停止循環(huán)。您想要忽略該值并繼續(xù)處理下一個值。如果必須使用while循環(huán),則在語句中使用單獨的測試if:


while e < ulis_l:

    if ulis[e] <= 0:

        # You can use += here instead of total_1 = total_1 + ...

        total_1 += ulis[e]

        lprt.append(ulis[e])

    e = e + 1

    print(total_1)

那是因為您想訪問列表中的每個值ulis以測試是否需要包含它們。這是上述循環(huán)的演示:


>>> ulis = [5, 4, 4, 3, 1, -2, -3, -5, -7, -7]

>>> ulis_l = len(ulis)

>>> e = total_1 = 0

>>> lprt = []

>>> while e < ulis_l:

...     if ulis[e] <= 0:

...         # You can use += here instead of total_1 = total_1 + ...

...         total_1 += ulis[e]

...         lprt.append(ulis[e])

...     e = e + 1

...     print(total_1)

...

0

0

0

0

0

-2

-5

-10

-17

-24

>>> total_1

-24

>>> lprt

[-2, -3, -5, -7, -7]

為此使用for循環(huán)更好也更容易,您可以直接遍歷列表中的值:


for value in ulis:

    if value <= 0:

        lprt.append(value)

        total_1 += value

        print(total_1)

我將調(diào)用移至print()測試中,因此它僅在我找到有效值時打?。?/p>


>>> lprt, total_1 = [], 0

>>> for value in ulis:

...     if value <= 0:

...         lprt.append(value)

...         total_1 += value

...         print(total_1)

...

-2

-5

-10

-17

-24

如果您只想對一系列符合特定條件的值求和,您還可以將生成器表達式放入函數(shù)調(diào)用中,使用sum()函數(shù):


total_1 = sum(value for value in ulis if value <= 0)

對于相同的結果,這是一個更緊湊的表達式:


>>> sum(value for value in ulis if value <= 0)

-24

您的第一個示例之所以有效,是因為輸入是按降序排列的;第一個小于 0 (-2) 的值只能跟隨更多小于 0 的值。如果您的輸入很大,以這種方式限制迭代次數(shù)可能是一個非常聰明的想法。while但是,您不需要循環(huán),您可以只使用以下break語句:


# first loop, values greater than 0

for value in ulis:

    if value <= 0:

        # input is sorted in descending order,

        # so all remaining values _will_ be smaller.

        # To save time, we can end the loop early.

        break


    # only sums values greater than 0

    total_1 += value

    lprt.append(value)

如果您想在循環(huán)中使用此屬性<= 0,則需要更改循環(huán)訪問列表的順序。您可以使用此處的reversed()功能執(zhí)行此操作:


# second loop, summing values smaller than or equal to 0 

for value in reversed(ulis):

    if value > 0:

        # input is iterated over in ascending order,

        # so all remaining values _will_ be greater than zero.

        # To save time, we can end the loop early.

        break


    # only sums values smaller than or equal to 0

    total_1 += value

    lprt.append(value)

當您將 a 添加print()到后一個版本時,您可以看到值以相反的順序相加:


>>> lprt, total_1 = [], 0

>>> for value in reversed(ulis):

...     if value > 0:

...         break

...     total_1 += value

...     lprt.append(value)

...     print(total_1)

...

-7

-14

-19

-22

-24

>>> lprt

[-7, -7, -5, -3, -2]

如果您需要lprt正確的正向順序,您可以通過獲取具有負索引的完整切片來再次反轉(zhuǎn)順序:


lprt = lprt[::-1]   # reverse the lprt list

如果您有兩個條件,例如值介于 5 和 -4 之間,并且輸入列表非常大但仍然排序,那么您可以考慮使用二進制搜索來查找輸入列表的開始和結束索引,然后使用range()類型生成這兩點之間的索引。標準庫為此提供了bisect模塊。


請注意,這確實意味著值必須按升序排序,而不是降序。


考慮到range()將stop索引視為未包含在生成的索引中,因此如果您測試value >= -4和value <= 5,那么您想使用它bisect.bisect_right()來查找第一個索引,其中的值都將大于 5:


import bisect


# ascending order! ulis = ulis[::-1] 


start_index = bisect.bisect_left(ulis, -4)  # values >= -4

stop_index = bisect.bisect_right(ulis, 5)  # value <= 5


total_1 = sum(ulis[index] for index in range(start_index, stop_index))


查看完整回答
反對 回復 2023-06-13
?
慕標5832272

TA貢獻1966條經(jīng)驗 獲得超4個贊

此條件永遠不會為真:ulis[e] <= 0
您使用了and,因此兩個條件 (( ulis[e] <= 0)( e < ulis_l )) 都必須為真。第一個條件永遠不會成立。
此問題與 jupyter notbook 無關。

查看完整回答
反對 回復 2023-06-13
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號