【问题标题】:Python: converting denary (decimal) strings from a list to characters [duplicate]Python:将十进制(十进制)字符串从列表转换为字符[重复]
【发布时间】:2013-12-16 09:47:38
【问题描述】:

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

# 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”应返回“hello.\nmy name is 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.

谁能提出一个可能的解决办法?谢谢。 为大量代码道歉。

这不是重复的...

【问题讨论】:

  • “拒绝”是一个官方术语吗?
  • @MartijnPieters:Denary 是一个不太常见的十进制术语
  • @AswinMurugesh:原来如此;在我的经验中相当罕见,但我也不是以英语为母语的人。
  • @MartijnPieters 它对我来说看起来不像是重复的......这与我正在寻找的完全不同。
  • 我是以英语为母语的人,我不认为“denary”是一个常见的术语——如果不检查谷歌我也不知道它是什么意思。并不是说一旦人们弄清楚它在这种情况下的含义就真的很重要,但这肯定不是我方言的一部分。

标签: python list decimal


【解决方案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.

【讨论】:

  • 感谢您的帮助。 :)
  • Martijn 的解决方案是正确的,就像他的答案一样。也就是说,整个设计很奇怪——你使用自己的自定义代码点映射集而不是使用现有标准是否有充分的理由? ASCII 或任何超集都可以轻松支持您的目标字符集,并且可以更好地与标准库一起使用。
【解决方案2】:

您的大问题实际上是在删除重复部分。 remove 方法实际上删除了第一次出现的值,由于值“30”出现了不止一次,所以你不小心把它从正确的地方删除了。

而不是迭代值:

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

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

doublesList = doublesList[1::2]

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2020-06-20
    • 1970-01-01
    • 2019-01-17
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多