0

I have a problem with a program in Python and I can't find an answer to my question. Let's suppose I have a list containing all the chars of the alphabet, that means from A to Z.

list = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

I want to take a random char form that list. Let's suppose Z. Now I want to iterate over that list 13 times. Let me explain better: if I am on the Z, Z + 13 must be M because I would do A,B,C,D,E,F,G,H,I,J,K,L,M. If I would be on the A and I'd want to use 25 for example it would be easy because it would be Z the final result. But if I put a 27 (so it goes over the max numbers of items in the list) how can I restart the list after the 25 so A + 27 would be B?

OverLoop
  • 3
  • 1
  • 2
  • 6
    Perhaps you're looking for the modulus operator, `%`? – xnx Apr 04 '18 at 14:05
  • I think you are looking for modulo function, https://docs.python.org/3.6/reference/expressions.html#binary-arithmetic-operations – ExtractTable.com Apr 04 '18 at 14:06
  • You are looking for the [modulus](https://stackoverflow.com/questions/4432208/how-does-work-in-python) operator. – Prostagma Apr 04 '18 at 14:06
  • I don't get it, how could the % operator achieve that? – OverLoop Apr 04 '18 at 14:06
  • `codecs.encode()` with `"rot13"` – Jean-François Fabre Apr 04 '18 at 14:08
  • You could do something like this: `element = l[(r + n) % len(l)]`, where `r` is the random index number of the list: `[0-25]`, `n` is the number you choose e.g. `13` and then you use the modulus operator `%` with the list's length, so that the value you will get will range from `0` to `len(list)-1`, that is, within bounds. – Vasilis G. Apr 04 '18 at 14:09
  • pls stop using `list` as a variable name. – Jean-François Fabre Apr 04 '18 at 14:09
  • 1
    "Iteration" has nothing to do with this. You aren't "iterating" over the list "13 times"; for any given input letter you want *one* output letter. The point is to find the *corresponding* one. The `%` operator helps you do this by using math to determine the index of the corresponding letter. – Karl Knechtel Apr 04 '18 at 14:09

4 Answers4

1

I think you can access the list element by list[index % len(list)]

ardilgulez
  • 1,856
  • 18
  • 19
1

Little example with a predifined index with value 34:

l = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
index=34
print(l[index%len(l)]) # result 'I'
silgon
  • 6,890
  • 7
  • 46
  • 67
1

To solve your issue, you could use % len(lst):

lst = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
print(lst[0])  # A
print(lst[(0+27) % len(lst)])  # B

Explanation:

lst[0] gives A. lst[0+27] does not exist (length of lst is 26). There you should be using % operator which follows a circular path through lst.

27 % 26 = 1 

So, in effect you call lst[1] when you do lst[(0+27) % len(lst)].

Note: Do not use list as your variable name. It shadows the in-built list. Try lst or something else.

Austin
  • 25,759
  • 4
  • 25
  • 48
1

You can do this:

l = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
n = 13 # Chosen number (you can assign any integer you want)
r = 25 # Index of Z
element = l[(r + n) % len(l)]
print("Element: ", element)

Output:

Element:  M

You can use the randint function from the random module to assign a random number for r, as well as n.

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29