+ 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?
10 Antworten
+ 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.
+ 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)
+ 11
Mirielle ,
*readability counts*
+ 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)
+ 3
Ipang I'll try that code too and see how it goes!
+ 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?
+ 2
And others might say lambda is scary.
As long as it works, it's all good
+ 1
These all seem to be interesting
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