18714

How can I implement a modulus on a list of numbers?

Question:

I have a list of numbers:

<blockquote>

6,12,24,25,7,27

</blockquote>

How can I make it so that there is modulus at 26 so that if numbers greater than 26 appear in the list it should go back to 1?

For example, in this situation all numbers should be left alone except 27 and that should become 1.

This is the code I'm trying to run:

message = zip(message, newkeyword) positions = [(alphabet.find(m), alphabet.find(n)) for m, n in message] sums = [alphabet.find(m) + alphabet.find(n) for m, n in message] #sums is were the numbers are stored sums%=26

But I get the error:

<blockquote>

TypeError: unsupported operand type(s) for %=: 'list' and 'int'

</blockquote>

Any help is greatly appreciated.

Answer1:

The modulo operator can only be applied to two numbers, not to a whole list. You have to apply it to each item, like this:

mod_sums = [s % 26 for s in sums]

Using a list comprehension you iterate through all numbers in the sums. In the new mod_sums list you save the number s modulo 26.

This basically is the same as:

mod_sums = [] for s in sums: mod_sums.append(s % 26)

Only written in a cleaner and more <em>pythonic</em> way.

You could also add the modulo directly into the first list comprehension:

sums = [(alphabet.find(m) + alphabet.find(n)) % 26 for m, n in message]

Answer2:

As an alternative, if you are willing/able to convert your list to a Numpy array you can achieve this in a really simple and pythonic way (by just applying the modulo to the array):

>>> import numpy as np >>> a = np.array([6,12,24,25,7,27]) >>> a array([ 6, 12, 24, 25, 7, 27]) >>> a%26 array([ 6, 12, 24, 25, 7, 1])

Recommend

  • syntax error, unexpected T_LNUMBER, expecting ',' or ';' [closed]
  • user-defined conversion operators precedence, compiles in g++ but not clang++
  • Display values from one associative array whose key exists as the value in another array
  • Access violation writing location when reading character via scanf_s
  • JqGrid server side query conflicts with search
  • VHDL modulo 2^32 addition
  • How to calculate the ISO week day using the ordinal day and the day of the week of the 1st of Januar
  • How can I implement a modulus on a list of numbers?
  • What is the meaning of the percent character in this code?
  • Three-way xor-like function
  • SQL SUM operation of multiple subqueries
  • product of two functions
  • Can you put a pimpl-Class inside a vector
  • Add negative elements in matrix to whole row under the condition that all elements except diagonal a
  • python mysql fetch query
  • create recurring activejob fails
  • Execute mathematical expression and set the value to variable in SQL
  • Fastest way to decode a hexadecimal digit
  • Matrix Circular Shift
  • Removing Labels from Legend in ggplot2
  • JAXB Root Class Rename using binding.xml
  • Subquery returns more than 1 row
  • jQuery file download plugin
  • Version in docker-compose is unsupported
  • Can one add a complex type item to ListModel?
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • JSON Error when parsing “… has no method 'replace'”
  • Cuda Clang and OS X Mavericks
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • Converting MP3 duration time