【问题标题】:Convert String to numbers separated by dots where a=1 and z=26将字符串转换为由点分隔的数字,其中 a=1 和 z=26
【发布时间】:2020-12-27 06:12:44
【问题描述】:

我想将字符串转换为整数,如下所示,用“。”分隔 与a=1,b=2,...z=26
text = 'Hello World"

预期输出:

     H.E.L .L. O  W. O. R. L. D
---> 8.5.12.12.15 23.15.18.12.4
#split the words
text ="Hello world"
text = text.upper()
splitted_text = text.split(" ")

#map characters to integers
import string
for x, y in zip(range(1, 27), string.ascii_lowercase):
    print(x, y)

我不知道如何从这里开始..

【问题讨论】:

  • 所以...将字符串拆分为单词,将单词中的每个字母映射为一个整数,用. 字符连接整数?你有没有尝试过这样的事情?
  • 请为您的问题提供明确的输入和输出。还要解释你到目前为止尝试了什么,什么没有奏效。您的输入和输出之间没有明显的相关性。

标签: python python-3.x regex pandas


【解决方案1】:
text = "Hello World".lower()
li = []
for i in text:
    if i!=' ':
        li.append(str(ord(i)-96))
    
print('.'.join(li))

这应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2023-03-07
    • 1970-01-01
    • 2019-08-23
    • 2018-02-16
    • 2012-01-18
    • 2020-09-02
    • 2011-12-15
    相关资源
    最近更新 更多