将整数字符串转换为 Python 中的整数

Turning an integer string to an integer in Python

我正在尝试在 python 中编写一个程序,通过首先将输入单词转换为莫尔斯码然后将点和破折号更改为将被视为二进制数等的 1 和 0 来对项目进行编码。 这是一个代码片段:

def mimary_encode(input):
if input.find('!')!=-1 or input.find('@')!=-1 or input.find('#')!=-1 or input.find('$')!=-1 or input.find('%')!=-1 or input.find('^')!=-1 or input.find('&')!=-1 or input.find('*')!=-1 or input.find('(')!=-1 or input.find(')')!=-1 or input.find('_')!=-1 or input.find('-')!=-1 or input.find('=')!=-1 or input.find('+')!=-1 or input.find('.')!=-1 or input.find('"')!=-1 or input.find("'")!=-1 or input.find(',')!=-1 or input.find(' ')!=-1 or input.find(';')!=-1 or input.find(':')!=-1 or input.find('[')!=-1 or input.find(']')!=-1 or input.find('{')!=-1 or input.find('}')!=-1 or input.find('?')!=-1 or input.find('<')!=-1 or input.find('>')!=-1:
    print "Inputs cannot contain spaces or symbols"
else:base=input
nol=len(input)
if base.find("a")!=-1:
    base=base.replace("a",".-")
if base.find("b")!=-1:
    base=base.replace("a","-...")
if base.find("c")!=-1:
    base=base.replace("c","-.-.")
if base.find("d")!=-1:
    base=base.replace("d","-..")
if base.find("e")!=-1:
    base=base.replace("e",".")
if base.find("f")!=-1:
    base=base.replace("f","..-.")
if base.find("g")!=-1:
    base=base.replace("g","--.")
if base.find("h")!=-1:
    base=base.replace("h","....")
if base.find("i")!=-1:
    base=base.replace("i","..")
if base.find("j")!=-1:
    base=base.replace("j",".---")
if base.find("k")!=-1:
    base=base.replace("k","-.-")
if base.find("l")!=-1:
    base=base.replace("l",".-..")
if base.find("m")!=-1:
    base=base.replace("m","--")
if base.find("n")!=-1:
    base=base.replace("n","-.")
if base.find("o")!=-1:
    base=base.replace("o","---")
if base.find("p")!=-1:
    base=base.replace("p",".--.")
if base.find("q")!=-1:
    base=base.replace("q","--.-")
if base.find("r")!=-1:
    base=base.replace("r",".-.")
if base.find("s")!=-1:
    base=base.replace("s","...")
if base.find("t")!=-1:
    base=base.replace("t","-")
if base.find("u")!=-1:
    base=base.replace("u","..-")
if base.find("v")!=-1:
    base=base.replace("v","...-")
if base.find("w")!=-1:
    base=base.replace("w",".--")
if base.find("x")!=-1:
    base=base.replace("x","-..-")
if base.find("y")!=-1:
    base=base.replace("y","-.--")
if base.find("z")!=-1:
    base=base.replace("z","--..")
if base.find("1")!=-1:
    base=base.replace("1",".----")
if base.find("2")!=-1:
    base=base.replace("2","..---")
if base.find("3")!=-1:
    base=base.replace("3","...--")
if base.find("4")!=-1:
    base=base.replace("4","....-")
if base.find("5")!=-1:
    base=base.replace("5",".....")
if base.find("6")!=-1:
    base=base.replace("6","-....")
if base.find("7")!=-1:
    base=base.replace("7","--...")
if base.find("8")!=-1:
    base=base.replace("8","---..")
if base.find("9")!=-1:
    base=base.replace("9","----.")
if base.find("0")!=-1:
    base=base.replace("0","-----")
if base.find("-")!=-1:
    base=base.replace("-","0")
if base.find(".")!=-1:
    base=base.replace(".","1")
int(base)


mimary_encode("hi")

我知道这可能不是最好的写法,但问题是 python 一直给我的错误是:

Traceback (most recent call last):
  File "C:/Documents and Settings/Moshe's Programming/Desktop/Python       
Projects/Mimary/Mimary attempt 1.py", line 86, in <module>
    mimary_encode("hi")
  File "C:/Documents and Settings/Moshe's Programming/Desktop/Python         
Projects/Mimary/Mimary attempt 1.py", line 83, in mimary_encode
print base + 1
TypeError: cannot concatenate 'str' and 'int' objects

这个错误是什么意思?我该如何解决这个错误?我已经把基数变成整数了,不是吗?

虽然你的代码真的很乱,但它可以工作。但是,您的第一个错误是由于 int("base").

行引起的

如果你写 int("base") 你试图把字符串 "base" 变成一个整数,这是不可能做到的。

然后,您将代码更改为 print base + 1 这也是不可能的,一旦 basestring 并且您不能将字符串和整数与 [=16 相加=] 标志。 所以,你想要做的是:

def mimary_encode(base): 
    #Dowhateveryouwant
    return int(base) #Only if you are sure base contains only integers
print mimary_encode("hi")

错误来自 print base + 1,其中 base 是一个字符串,1 是一个整数。

这是您的函数的替代实现。首先,我将莫尔斯电码编码定义为字典。在函数中,我首先将所有字母转换为小写。然后我使用 get 字典函数来 return 摩尔斯电码值,如果它在字典中,否则它使用空字符串来过滤它。这与过滤不良数据的原始方法不同。在这里,我只查找字典中的数据。最后,我使用生成器将编码的字母连接在一起:code = " ".join((morse.get(c, "") for c in input_string)) 这类似于列表理解,但对于大字符串更有效。

from string import letters

msg = 'I hear 13 knights from the Round Table are here!!!'

def mimary_encode(input_string):
    input_string = ''.join([c.lower() if c in letters else c
                            for c in input_string])
    code = " ".join((morse.get(c, "") for c in input_string))
    return code

morse = {
 '0': '-----',
 '1': '.----',
 '2': '..---',
 '3': '...--',
 '4': '....-',
 '5': '.....',
 '6': '-....',
 '7': '--...',
 '8': '---..',
 '9': '----.',
 'a': '.-',
 'b': '-...',
 'c': '-.-.',
 'd': '-..',
 'e': '.',
 'f': '..-.',
 'g': '--.',
 'h': '....',
 'i': '..',
 'j': '.---',
 'k': '-.-',
 'l': '.-..',
 'm': '--',
 'n': '-.',
 'o': '---',
 'p': '.--.',
 'q': '--.-',
 'r': '.-.',
 's': '...',
 't': '-',
 'u': '..-',
 'v': '...-',
 'w': '.--',
 'x': '-..-',
 'y': '-.--',
 'z': '--..'}

对消息进行编码(之前定义为 msg):

>>> mimary_encode(msg)
'.. .... . .- .-. .---- ...-- -.- -. .. --. .... - ... ..-. .-. --- -- - .... . .-. --- ..- -. -.. - .- -... .-.. . .- .-. . .... . .-. .'

鉴于字典的一对一映射,您可以使用字典理解来反转它:

reverse_morse = {v: k for k, v in morse.iteritems()}

然后您可以反转莫尔斯电码,将其转换回 alpha/numeric 字符串。

>>> ''.join([reverse_morse.get(c, "") for c in mimary_encode(msg).split(" ")])
'ihear13knightsfromtheroundtablearehere'

请注意所有字母都已转换为小写并且感叹号已被删除。