需要更好的逻辑来添加字符串的每个ith字符与其反向字符串的每个第i个字符

时间:2016-11-22 07:31:35

标签: python string list add

我是python编码的新手,并以一些硬编码方式构建一些基本逻辑。

问题陈述简单易行。

让我说我有话:hello,反之为olleh

添加每个第i个字符的值,即h = 8和o = 15将为23并且返回转换为字符串,即23 = w(对于a = 1,b = 2,依此类推)

最后输出将是:wqxqw

我的代码如下:

given = list(raw_input())

# forward & reversing the list
forward_str = given
reverse_str = given[::-1]

# converting letters(a-z) to number(1-26) 
forward_nums = []
for f in forward_str:
    forward_nums.append(ord(f) - ord('a') + 1)

reverse_nums = []
for r in reverse_str:
    reverse_nums.append(ord(r) - ord('a') + 1)


# Addtion of element-wise from both lists

# from operator import add
# map(add, list1, list2)

addition = [sum(x) for x in (zip(forward_nums,reverse_nums))]


# For letters a-z ,numbers should be in range 1-26,so i > 26 is added back
valid_nums = []
for i in addition:
    if i <= 26:
        valid_nums.append(i)
    elif i > 26:
        x = i %26
        valid_nums.append(x)  


# converting number(1-26) to letters(a-z)
import string
num2alpha = dict(zip(range(1, 27), string.ascii_lowercase))

chars =[]
for i in valid_nums:
    chars.append(num2alpha[i])

#converting list to string
output = "".join(chars)


print "forward_str : ",forward_str
print "reverse_str : ",reverse_str,"\n -----------------------------------------"
print "forward_nums : ",forward_nums
print "reverse_nums : ",reverse_nums,"\n----------------------------------------"
print "additon : ",addition
print "valid_nums : ",valid_nums,"\n---------------------------------------------"
print "converted_list : ",chars 
print "final output : ",output

输入:helloworld

输出是:

forward_str :  ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
reverse_str :  ['d', 'l', 'r', 'o', 'w', 'o', 'l', 'l', 'e', 'h'] 
 -----------------------------------------
forward_nums :  [8, 5, 12, 12, 15, 23, 15, 18, 12, 4]
reverse_nums :  [4, 12, 18, 15, 23, 15, 12, 12, 5, 8] 
----------------------------------------
additon :  [12, 17, 30, 27, 38, 38, 27, 30, 17, 12]
valid_nums :  [12, 17, 4, 1, 12, 12, 1, 4, 17, 12] 
---------------------------------------------
converted_list :  ['l', 'q', 'd', 'a', 'l', 'l', 'a', 'd', 'q', 'l']
final output :  lqdalladql

考虑给定的字符串是小写的。

我正在寻找一些更简单,更灵巧的解决方案。

先谢谢

1 个答案:

答案 0 :(得分:1)

使用列表理解简化到下面

import string
given = list(raw_input())

# forward & reversing the list
forward_str = given
reverse_str = given[::-1]

forward_nums = [ord(i)-ord('a')+1 for i in forward_str]
reverse_nums = [ord(i)-ord('a')+1 for i in reverse_str]

addition = [sum(x) for x in (zip(forward_nums,reverse_nums))]
valid_nums = [i % 26 for i in addition]

chars = [string.ascii_lowercase[i-1] for i in valid_nums]
output = ''.join(chars)
如果您不需要中间结果,可以进一步简化以下

import string
given = list(raw_input())

valid_nums = [ (ord(i)+ord(j)-ord('a')-ord('a')+2) % 26 for i,j in zip(given,given[::-1]) ]
output = ''.join([string.ascii_lowercase[i-1] for i in valid_nums])

from string import ascii_lowercase as al
given = list(raw_input())

valid_nums = [ (al.index(i) + al.index(j)+2) % 26 for i,j in zip(given,given[::-1]) ]
output = ''.join([al[i-1] for i in valid_nums])