[英]"Unsupported operand type for +: NoneType" when trying to append a character to a string
I am trying to solve the following question:我正在尝试解决以下问题:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)字符串“PAYPALISHIRING”在给定数量的行上以锯齿形图案写入,如下所示:(您可能希望以固定字体显示此图案以提高可读性)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"然后逐行阅读:“PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:编写将采用字符串并在给定行数的情况下进行此转换的代码:
string convert(string s, int numRows);字符串转换(字符串 s,int numRows);
I have written the following code but I am getting error in the bold line " TypeError: unsupported operand type(s) for +: 'NoneType' and 'unicode'"我已经编写了以下代码,但在粗体行中出现错误“TypeError: unsupported operand type(s) for +: 'NoneType' and 'unicode'”
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows==1:
return s
templist=[None]*numRows
ret=" "
curRow=0
goingDown=0
for each_char in s:
if templist[curRow]:
templist[curRow]=each_char
else:
**templist[curRow]=templist[curRow] + each_char**
if (curRow==numRow-1 or curRow == 0):
goingDown = not goingDown
if goingDown:
curRow=curRow+1
else:
curRow=curRow-1
for each_str in templist:
ret=ret+each_str
print ret
Am I doing something wrong in this.我在这方面做错了什么。 It will be great if someone can point out the problem here.如果有人可以在这里指出问题,那就太好了。 Thanks in advance提前致谢
Your conditions seem to be reversed in the following lines:您的情况似乎在以下几行中颠倒了:
if templist[curRow]:
templist[curRow]=each_char
else:
**templist[curRow]=templist[curRow] + each_char**
It should probably read:它可能应该是:
if templist[curRow]:
templist[curRow]=templist[curRow] + each_char
else:
templist[curRow]=each_char
This ensures that it only appends to the string in templist[curRow]
if that string already exists (is not None
), and so avoids the error of adding a string to None
.这确保它仅在templist[curRow]
中的字符串已经存在(不是None
)的情况下附加到该字符串,因此避免了将字符串添加到None
的错误。
A better way might be to set templist = [""] * numRows
, ie a list of empty strings, then just add to it using templist[curRow] += each_char
, which will always work because you can add a character to an empty string.更好的方法可能是设置templist = [""] * numRows
,即空字符串列表,然后使用templist[curRow] += each_char
添加到其中,这将始终有效,因为您可以将字符添加到空字符串细绳。
Yes, you're doing something wrong.是的,你做错了什么。
The line templist=[None]*numRows
makes your templist
variable hold a bunch of None
s.行templist=[None]*numRows
使您的templist
变量包含一堆None
s。 You then proceed to take one of those None
s, and try to "add" one of them to a string with the statement that you have in bold: templist[curRow]=templist[curRow] + each_char
(ie the righthand side of this assignment evaluates to None + each_char
).然后,您继续使用其中一个None
,并尝试将其中一个“添加”到字符串中,并使用粗体显示的语句: templist[curRow]=templist[curRow] + each_char
(即右侧赋值为None + each_char
)。
You can't add a string and a None
in Python, hence the error.您不能在 Python 中添加字符串和None
,因此会出现错误。
string = 'PAHNAPLSIIGYIR'
string = list(string)
rows = []
def make_output(string, num_rows):
i = 0
while i < num_rows:
if i%2 == 0:
a = []
while len(a) < 4:
if len(string)==0:
break
a.append(string.pop(0))
rows.append(a)
if i%2 != 0:
a = []
while len(a) < 7:
a.append(string.pop(0))
rows.append(a)
i += 1
for i in range(len(rows)):
if i%2 == 0:
print(' '.join(rows[i]))
if i%2 != 0:
print(' '.join(rows[i]))
make_output(string, 3)
You can use a function like this, where you designate lengths for even and odd rows, then you can pop
from your string.您可以使用这样的函数,在其中指定偶数行和奇数行的长度,然后可以从字符串中pop
。 Then you can just print
even and odd rows with proper spacing.然后您可以print
具有适当间距的偶数行和奇数行。
(xenial)vash@localhost:~/python/AtBS$ python3.7 solve.py PAHN APLSIIG YIR (xenial)vash@localhost:~/python/AtBS$ python3.7 solve.py MM * H YAESVS * NIA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.