python “无法连接‘str’和‘list’对象”不断出现:(
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2609460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"cannot concatenate 'str' and 'list' objects" keeps coming up :(
提问by user313022
I'm writing a python program. The program calculates Latin Squares using two numbers the user enters on a previous page. But but an error keeps coming up, "cannot concatenate 'str' and 'list' objects" here is the program:
我正在编写一个python程序。该程序使用用户在前一页输入的两个数字来计算拉丁方。但是一个错误不断出现,“无法连接'str'和'list'对象”这里是程序:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgi
import cgitb
cgitb.enable()
def template(file, **vars):
return open(file, 'r').read() % vars
print "Content-type: text/html\n"
print
form = cgi.FieldStorage() # instantiate only once!
num_1 = form.getfirst('num_1')
num_2 = form.getfirst('num_2')
int1r = str(num_1)
int2r = str(num_2)
def calc_range(int2r, int1r):
start = range(int2r, int1r + 1)
end = range(1, int2r)
return start+end
int1 = int(int1r)
int2 = int(int2r)
out_str = ''
for i in range(0, int1):
first_line_num = (int2 + i) % int1
if first_line_num == 0:
first_line_num = int1
line = calc_range(first_line_num, int1)
out_str += line
print template('results.html', output=out_str, title="Latin Squares")
回答by Justin Ethier
range
returns a list object, so when you say
range
返回一个列表对象,所以当你说
line = calc_range(first_line_num, int1)
You are assigning a list to line
. This is why out_str += line
throws the error.
您正在为 分配一个列表line
。这就是out_str += line
抛出错误的原因。
You can use str()
to convert a list to a string, or you can build up a string a different way to get the results you are looking for.
您可以使用str()
将列表转换为字符串,或者您可以以不同的方式构建字符串以获得您正在寻找的结果。
回答by Matti Virkkunen
By doing out_str += line
, you're trying to add a list (from calc_range) to a string. I don't even know what this is supposed to be doing, but that's where the problem lies.
通过这样做out_str += line
,您正在尝试将列表(来自 calc_range)添加到字符串中。我什至不知道这应该做什么,但这就是问题所在。
回答by samtregar
You didn't say what line you're getting the error from, but I'm guessing it's:
你没有说你从哪一行得到错误,但我猜是:
out_str += line
The first variable is a string. The second is a list of numbers. You can't concatenate a list onto a string. I don't know what you're trying to do exactly, but how about:
第一个变量是一个字符串。第二个是数字列表。您不能将列表连接到字符串上。我不知道你到底想做什么,但怎么样:
out_str += ", ".join(line)
That will add the numbers joined by commas onto out_str.
这会将逗号连接的数字添加到 out_str 上。
回答by Daniel G
calc_range()
returns a list; however, you are attempting to add it to a string (out_str
).
calc_range()
返回一个列表;但是,您正试图将其添加到字符串 ( out_str
) 中。
It looks like your code is unfinished - don't you want to do something with the range of numbers returned by calc_range()
? Like, say, something with the form?
看起来您的代码未完成 - 您不想对返回的数字范围做些什么calc_range()
吗?比如说,有什么形式?
line = ''.join(num_1[index] for index in calc_range(first_line_num, int1))
I don't know if that's what you want - but maybe something like that?
我不知道这是否是你想要的——但也许是那样的?