Python:将列表中的denary(十进制)字符串转换为字符

时间:2013-11-30 09:25:59

标签: python list decimal

我正在尝试使用以下代码将denary字符串转换为字符,但我无法正确转换它们。

# Set to true if testing. Will show lists that show the process.
testing = True

# Asks for the denary.
text = list(input('Enter the denary: '))

# Defining the list. 0 is there to allow the remover to work correctly.
doublesList = ['0']

# Split the numbers into doubles and append them to doublesList.
for i in range(len(text) - 1):
    current_item = text[i]
    next_item = text[i + 1]
    doubles = current_item + next_item
    doublesList.append(doubles)

if testing:
    print('\nunedited:\n', doublesList,'\n')

# Remove unnecessary numbers.
for item in doublesList:
    doublesList.remove(item)

if testing:
    print('edited:\n', doublesList,'\n')

# Replace the numbers with their letters.

for item in doublesList:
    if item == '01':
        doublesList[doublesList.index('01')] = 'a'
    elif item == '02':
        doublesList[doublesList.index('02')] = 'b'
    elif item == '03':
        doublesList[doublesList.index('03')] = 'c'
    elif item == '04':
        doublesList[doublesList.index('04')] = 'd'
    elif item == '05':
        doublesList[doublesList.index('05')] = 'e'
    elif item == '06':
        doublesList[doublesList.index('06')] = 'f'
    elif item == '07':
        doublesList[doublesList.index('07')] = 'g'
    elif item == '08':
        doublesList[doublesList.index('08')] = 'h'
    elif item == '09':
        doublesList[doublesList.index('09')] = 'i'
    elif item == '10':
        doublesList[doublesList.index('10')] = 'j'
    elif item == '11':
        doublesList[doublesList.index('11')] = 'k'
    elif item == '12':
        doublesList[doublesList.index('12')] = 'l'
    elif item == '13':
        doublesList[doublesList.index('13')] = 'm'
    elif item == '14':
        doublesList[doublesList.index('14')] = 'n'
    elif item == '15':
        doublesList[doublesList.index('15')] = 'o'
    elif item == '16':
        doublesList[doublesList.index('16')] = 'p'
    elif item == '17':
        doublesList[doublesList.index('17')] = 'q'
    elif item == '18':
        doublesList[doublesList.index('18')] = 'r'
    elif item == '19':
        doublesList[doublesList.index('19')] = 's'
    elif item == '20':
        doublesList[doublesList.index('20')] = 't'
    elif item == '21':
        doublesList[doublesList.index('21')] = 'u'
    elif item == '22':
        doublesList[doublesList.index('22')] = 'v'
    elif item == '23':
        doublesList[doublesList.index('23')] = 'w'
    elif item == '24':
        doublesList[doublesList.index('24')] = 'x'
    elif item == '25':
        doublesList[doublesList.index('25')] = 'y'
    elif item == '26':
        doublesList[doublesList.index('26')] = 'z'
    elif item == '27':
        doublesList[doublesList.index('27')] = ' '
    elif item == '28':
        doublesList[doublesList.index('28')] = '.'
    elif item == '29':
        doublesList[doublesList.index('29')] = ''
    elif item == '30':
        doublesList[doublesList.index('30')] = '\n'

# Print the finished list as a string.
print(''.join(doublesList))

使用上面的代码,输入代码“2908051212152830132527140113052709192702150228”应该返回“你好。\ n我的名字是bob。”。但是,正如您所看到的,它返回“hello.my nam \ ne is bob。”:

Enter the denary: 2908051212152830132527140113052709192702150228

unedited:
 ['0', '29', '90', '08', '80', '05', '51', '12', '21', '12', '21', '15', '52', '28', '83', '30', '01', '13', '32', '25', '52', '27', '71', '14', '40', '01', '11', '13', '30', '05', '52', '27', '70', '09', '91', '19', '92', '27', '70', '02', '21', '15', '50', '02', '22', '28'] 

edited:
 ['29', '08', '05', '12', '12', '15', '28', '13', '25', '27', '14', '01', '13', '30', '05', '27', '09', '19', '27', '02', '15', '02', '28'] 

hello.my nam
e is bob.

有人可以建议一个可能的解决方案吗?谢谢。 为大量代码道歉。

这不重复...

2 个答案:

答案 0 :(得分:1)

成对循环输入;您可以使用地图字符串从那里转换为字符

characters = ' abcdefghijklmnopqrstuvwxyz . \n'

result = []

for pair in zip(text[::2], text[1::2]):
    codepoint = int(''.join(pair))
    if not (1 <= codepoint <= 30) or codepoint == 29: continue
    result.append(characters[codepoint])
print(''.join(result))

演示:

>>> text = '2908051212152830132527140113052709192702150228'
>>> characters = ' abcdefghijklmnopqrstuvwxyz . \n'
>>> result = []
>>> for pair in zip(text[::2], text[1::2]):
...     codepoint = int(''.join(pair))
...     if not (1 <= codepoint <= 30) or codepoint == 29: continue
...     result.append(characters[codepoint])
... 
>>> result
['h', 'e', 'l', 'l', 'o', '.', '\n', 'm', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'b', 'o', 'b', '.']
>>> print(''.join(result))
hello.
my name is bob.

答案 1 :(得分:0)

你的重大问题实际上是删除重复部分。 remove方法实际上删除了值的第一个外观,并且由于值“30”出现多次,因此您不小心将其从正确的位置删除。

而不是迭代值:

# Remove unnecessary numbers.
for item in doublesList:
    doublesList.remove(item)

您应该只访问每一秒的值(以下行假定您在开头保持“0”:

doublesList = doublesList[1::2]

但是,建议将列表直接拆分为对,而不必处理“中间对”。