亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

更新:在 python 中查找數字列表的最小公倍數

更新:在 python 中查找數字列表的最小公倍數

智慧大石 2023-09-12 16:59:47
您好,我已經用我的代碼更新了此內容:輸入->a=[16,21, 56, 40]def find_primes(a):    num_factors=[]     for num in a:       # print('1',num)        list_of_factors=[]        i=2        while num>1:            #print('2',num)            if num%i==0:                list_of_factors.append(i)               # print('3',list_of_factors)                num=num/i               # print('4',num)                i=i-1                            i+=1         num_factors.append(list_of_factors)      d = {}    a_list = []    for i in num_factors:        for j in i:            d[j] = d.get(j, 0) + 1            dictionary_copy = d.copy()    a_list.append(dictionary_copy)      print(a_list)    return num_factors    find_primes(a)這是我得到的輸出:[{2: 10, 3: 1, 7: 2, 5: 1}][[2, 2, 2, 2], [3, 7], [2, 2, 2, 7], [2, 2, 2, 5]]這是我的問題:理解因為它是一本字典,所以鍵的值會累積。我想計算每個列表中唯一整數的數量。例如。[{2:4},{3:1,7:1},{2:3,7:1},{2:3,5:1}] 而不是上面代碼的輸出中給出的內容。之后,我想獲得每個整數的最大出現次數來計算 LCM。2^4 * 3 * 7 * 5請建議我們如何改進代碼。感謝您的幫助。
查看完整描述

1 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

這可能是其中一種方法。


numbers=list(map(int,input().split()))

numbers.sort()

maximum=numbers[0]  #gcd of the input numbers cannot be greater than minimum number in the list.Therefore we are retrieving the mininum number of the list.

    gcd=1               #Initial value of gcd.

    for i in range(1,(maximum+1)):

        flag=0

        for j in range(len(numbers)):

            num=numbers[j]

            if num % i ==0:      #check if the every number in the input is divisible by the value of i

                 flag=1          #if divisible set flag=1 and then check the other numbers of the input.

            else:

                 flag=0          #if any of the number of the input is not divisible then flag =0 and go to next iteration of i.

                 break

        if flag==1:

            gcd=i         #if flag=1 that means every number of the input is divisible by the value of j.Update the value of gcd.

    

    print(gcd)

可以通過以下方式完成:


for i in num_factors:

    d={}

    for j in i:

       

            try:

             d[j]=d[j]+1

            except KeyError:

             d[j]=1


    dictionary_copy = d.copy()

    a_list.append(dictionary_copy)  

print(a_list)

return num_factors


查看完整回答
反對 回復 2023-09-12
  • 1 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號