简体   繁体   English

将数组中的元素相乘(Python)

[英]Multiplying elements in an array together (Python)

I'm attempting a question where I need to find the largest product of 2 digits contained within a long number: 我正在尝试一个问题,我需要找到一个长数字中包含的2位数的最大乘积:

Number = 73167176531330624919225119674426574742355349194934

lst = [int(i) for i in str(Number)]
print(lst)

new_lst = []

for i in lst:
    new_lst.append(i*(i+1))
    print(new_lst)

However, what I am attempting above gives the result 56, how do I multiply 7*3, then 3*1, and so on... 但是,我上面尝试的结果是56,如何乘以7 * 3,然后是3 * 1,依此类推......

EDIT for H 编辑H

Number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

lst = [int(i) for i in str(Number)]
print(lst)

new_lst = []

for i, value in enumerate(lst):
    new_lst.append(lst[i-13]*(lst[i-12])*(lst[i-11])*(lst[i-10])*(lst[i-9])*(lst[i-8])*(lst[i-7])*(lst[i-6])*(lst[i-5])*(lst[i-4])*(lst[i-3])*(lst[i-2])*(lst[i-1]))
print(max(new_lst))

You are currently multiplying the number i times i + 1. You could use range to do what you want like this: 您目前正在将数字乘以i + i。您可以使用range来执行您想要的操作:

for i in range(len(lst)-1):
        new_lst.append(lst[i]*(lst[i+1]))
        print(new_lst)

You can iterate over a zip the list and a slice of it with the start differing by 1: 你可以迭代一个zip列表和一个切片,开头相差1:

max(a * b for a, b in zip(lst, lst[1:]))

This returns: 54 (because of 9 and 6 in the number) 返回: 54 (由于数字中的96

itertools has a recipe for this called pairwise . itertools有这种所谓的配方pairwise

>>> from itertools import tee
>>> def pairwise(iterable):
...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
...     a, b = tee(iterable)
...     next(b, None)
...     return zip(a, b)
... 
>>> [a*b for a,b in pairwise(map(int, '1234'))]
[2, 6, 12]

a version strictly using integer arithmetic (as opposed to strings): 严格使用整数运算的版本(与字符串相对):

Number = 73167176531330624919225119674426574742355349194934

max_prod, a, b = 0, None, None

Number, prev = divmod(Number, 10)
while Number:
    Number, cur = divmod(Number, 10)
    prod = cur * prev
    if prod > max_prod:
        max_prod, a, b = prod, cur, prev
        # if prod == 81:  # prod will never be bigger; can as well stop here.
        #     break
    prev = cur

print(a, b, max_prod)  # 9 6 54

update to show how this would work for your current question (where you look for the maximum product of 13 adjacent digits): 更新以显示这将如何适用于您当前的问题(您在哪里查找13个相邻数字的最大乘积):

Number = int('731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861'
             '560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895'
             '044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904'
             '915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881'
             '220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514'
             '929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434'
             '506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912'
             '883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724'
             '428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357'
             '2972571636269561882670428252483600823257530420752963450')

def digit_prod(i):
    '''
    return the product of all the decimal digits in i
    '''
    if i == 0:
        return 0
    prod = 1
    while i:
        i, r = divmod(i, 10)
        if r == 0:
            return 0
        prod *= r
    return prod


max_prod, res = 0, None

mod = 10**13
while Number:
    i = Number % mod
    prod = digit_prod(Number % mod)
    if prod > max_prod:
        max_prod, res = prod, i
        # if prod == 9**13: # prod will never be bigger; can as well stop here.
        #     break
    Number //= 10

print(res, max_prod)  # 5576689664895 23514624000

Number = 73167176531330624919225119674426574742355349194934 编号= 73167176531330624919225119674426574742355349194934

lst = [int(i) for i in str(Number)] print(lst) lst = [int(i)for i in str(Number)] print(lst)

new_lst = [] new_lst = []

for i in xrange(len(lst)-1): new_lst.append(lst[i]*lst[i+1]) print new_lst for x in xrange(len(lst)-1):new_lst.append(lst [i] * lst [i + 1])print new_lst

You want something like this perhaps: 你可能想要这样的东西:

Number = 73167176531330624919225119674426574742355349194934

print(max(int(x)*int(y) for x, y in zip(str(Number)[:-1], str(Number)[1:])))

Answer will be 54 (9*6) 答案是54(9 * 6)

You don't need to create a list to calculate the maximum of an iterable. 您无需创建列表来计算可迭代的最大值。 This will be inefficient. 这将是低效的。 Here is a functional solution using max , map and mul : 这是一个使用maxmapmul的功能解决方案:

from operator import mul

x = '73167176531330624919225119674426574742355349194934'

res = max(map(mul, map(int, x), map(int, x[1:]))) # 54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM