0

I have written a for loop which gives me all the values of a specific letters place in the alphabet.

For example the word hello will give me the numbers 8, 5, 12, 12 and 14. Now I want to add them to another word which is the same length for e.g abcde, which would be 1, 2, 3, 4 and 5. Now I want to add the two numbers together but keeping the individual numbers for example 8+1, 5+2, 12+3, 12+4 and 14+5.

This is the code I have so far

for letter in message:
    if letter.isalpha() == True:
        x = alphabet.find(letter)

for letter in newkeyword:
    if letter.isalpha() == True:
        y = alphabet.find(letter)

When I try adding x and y, I get a single number. Can someone help?

m00am
  • 5,910
  • 11
  • 53
  • 69
  • Convert `x` and `y` to string and then do concatenation. Like `str(x) + '+' + str(y)` . And use `eval()` for evaluating expression! – Gaurav Vichare Sep 12 '15 at 16:21

2 Answers2

0

You are looking for zip function. It zips 2 or more iterables together. For e.g.

l1 = 'abc'
l2 = 'def'
zip(l1, l2)
# [('a', 'd'), ('b', 'e'), ('c', 'f')] in python 2.7

and

list(zip(l1, l2))
# [('a', 'd'), ('b', 'e'), ('c', 'f')] in python 3    

So here is a solution for your problem:

l = list(zip(message, newkeyword))
[str(alphabet.find(x)) + '+' + str(alphabet.find(y)) for x, y in l]
kmad1729
  • 1,484
  • 1
  • 16
  • 20
0

If you are planning to do further calculations with the numbers consider this solution which creates a list of tuples (also by using zip, as @Kashyap Maduri suggested):

messages = zip(message, newkeyword)
positions = [(alphabet.find(m), alphabet.find(n)) for m, n in messages]
sums = [(a, b, a + b, "{}+{}".format(a,b)) for a, b in positions]

Each tuple in the sums list consists of both operands, their sum and a string representation of the addition.
Then you could for example print them sorted by their sum:

for a, b, sum_ab, sum_as_str in sorted(sums, key = lambda x: x[2]):
    print(sum_as_str)

Edit

when i run the program i want it to give me the answer of those sums for e.g 14+5=19 i just want the 19 part any ideas? – Shahzaib Shuz Bari

This makes it a lot easier:

messages = zip(message, newkeyword)
sums = [alphabet.find(m) + alphabet.find(n) for m, n in messages]

And you get a list of all the sums.

m00am
  • 5,910
  • 11
  • 53
  • 69
  • thanks how can i actually get the answer of the sums from there – Shahzaib Shuz Bari Sep 12 '15 at 17:53
  • What exactly do you mean by the answer of the sums? If you need the results to be individual lists you can use `zip*` (the inverse of the zip function) like this: `all_as, all_bs, int_sums, str_sums = zip(*sums)`. Does this help? – m00am Sep 12 '15 at 18:03
  • when i run the program i want it to give me the answer of those sums for e.g 14+5=19 i just want the 19 part any ideas? – Shahzaib Shuz Bari Sep 12 '15 at 18:41
  • by any chance do you know how to implement a modulus so that if the sum is greater than 26 it loops back to 1 for example 20+20 should become 14 – Shahzaib Shuz Bari Sep 12 '15 at 19:06
  • This is not the best place to ask this as it is a completely different question. Also: 1) This can easily be googled 2) There I also a good SO question you could take a look at: http://stackoverflow.com/questions/4432208/how-does-work-in-python 3) That is kind of a basic question. you might want to take a look at http://www.diveintopython3.net/ This is a very good book covering many important features of python. The modulus operator is (briefly) explained in chapter 2.3.2 – m00am Sep 12 '15 at 19:19