I have a list let's say : [1,3,5,6....,n] to very large number, that I need to report each number in the list in a new list but in "modulo 10^9 7" format in python.
How do I do it please?
I tried to search for it and people answer it's (n%m=p) and the solution is p, but I guess that's not it.
CodePudding user response:
To perform the modulo operation with a divisor of 10^9 7 in Python, you can use the % operator. For example:
Copy code x = 4 y = (x % (109 7)) print(y) # Output: 4 This will give you the remainder when x is divided by 109 7. In this case, the output will be 4.
You can also use the % operator to perform modulo operations with other divisors. For example:
Copy code x = 4 y = (x % 3) print(y) # Output: 1 This will give you the remainder when x is divided by 3. In this case, the output will be 1.
CodePudding user response:
Some ways to do it. The first method is called list comprehension, and you can find many examples of it on stack overflow and elsewhere.
list1 = [1, 3, 5, 6, 201, 99121, 191929912, 8129391828989123]
modulus = 10**9 7
list2 = [x % modulus for x in list1] # example of list comprehension
or using map
list2 = list(map(lambda x: x % modulus, list1))
or perhaps the least elegant
list2 = []
for x in list1:
list2.append(x % modulus)