+ 7

Adjacent number multiplication in a string.

Hello, sololearners. Let's assume I have a list with 6 digit numbers as a string. The length of list is N (unknown). So we have: ls = ['123456', '789123', '456789', and so on... ] How can I loop through each of the string, and multiply each number? e.g. 123456 = 1x2x3x4x5x6 = 720 e.g. 789123 = 7x8x9x1x2x3 = 3024 I tried nested for loop, such that: n = 0 for x in ls: for l in x: n += int(l)*int(l) and tried to compare products of values followed by if statements, but it didn't work. Any suggestions how can I loop through each of the string, and multiply each number?

29th Jan 2023, 4:09 PM
Lamron
Lamron - avatar
10 odpowiedzi
+ 13
Lamron , first step is to get the string of numbers to individual digits: > use a for loop to iterate over the list. take the string of digits and convert it to a list. this will separate the digits, but they are still strings. > in the same step we can use map() function and make each of the *string digits* an integer value. ... temp_lst = list(map(int, list(<string_of_digits>))) second step is to multiply the digits: > to get the product, we can use an additional loop and build the result of all digits. or > we can use the prod() method from the math module, that takes a list of int numbers and returns the result.
29th Jan 2023, 4:47 PM
Lothar
Lothar - avatar
+ 12
Lamron , your assumption is correct. if we have a list with 5 strings of digits, and we iterate over them, we will get each of the strings from the list. > in each loop iteration we get 1 string e.g. => '123456', that we can convert to a list => ['1', '2', '3', '4', '5', '6'] > applying map(int, ...) converts the list as => [1, 2, 3, 4, 5, 6]. > now as we have a list of integers, we can calculatd the product. *this is the basic version using 2 nested for loops* nums_lst = ['123456', '789123', '456789', '123'] for num in nums_lst: total = 1 for dig in num: total *= int(dig) print(total)
29th Jan 2023, 5:31 PM
Lothar
Lothar - avatar
+ 11
Mirielle , *readability counts*
29th Jan 2023, 5:44 PM
Lothar
Lothar - avatar
+ 4
Thanks for explanation and guidance, Lothar Thanks for your cheat Mirielle
29th Jan 2023, 5:33 PM
Lamron
Lamron - avatar
+ 3
You can also use the ord() function to get the digit character's ASCII code. With a bit of value manipulation we can have a for...loop do something that we want. Here's how it goes ... lst = [ "123456", "789123", "3278433" ] def digit_prod( s ): if not s.isdigit(): return 0 # for non-numeric strings rv = 1 for c in s: rv *= ord( c ) - 48 return rv result = list( map( digit_prod, lst ) ) print( result ) # 720 3024 12096 (Edited)
30th Jan 2023, 5:17 AM
Ipang
+ 3
Ipang I'll try that code too and see how it goes!
30th Jan 2023, 8:58 AM
Lamron
Lamron - avatar
+ 2
Lothar , what do you mean by this: " > use a for loop to iterate over the list. take the string of digits and convert it to a list. list will separate the digits, but they are still strings. " Does it mean, if I have in total 5 items in **ls** , I will have to make a separate list for each of these items to split digits? Or is it implemented somehow different?
29th Jan 2023, 4:54 PM
Lamron
Lamron - avatar
+ 2
And others might say lambda is scary. As long as it works, it's all good
29th Jan 2023, 5:37 PM
Lamron
Lamron - avatar
+ 1
These all seem to be interesting
29th Jan 2023, 6:45 PM
Lamron
Lamron - avatar
0
Oparah anord welcome to sololesrn. Don't hesitate to read this post, and make your way through courses that suit you the best: https://www.sololearn.com/Discuss/3021159/?ref=app
31st Jan 2023, 3:27 PM
Lamron
Lamron - avatar