1

我是 ac coder,但是 python 是不同的,我遇到了一个问题

items = [
     ["/ank/homepage.py","Home"],
     ["/ank/package.py","Packages"],
     ["/status.py","Status"],
     ["/task.py","Task"],
     ["/report.py","Report"]
]

static_html='<table id="main_nav" align="center"><tr>'

for each_item in items:
    if each_item in items:
        if isinstance(each_item, list):
            static_html=  static_html + '<td><a href=" ', each_item[0], ' "> ', each_item[1], ' </a></th> '

static_html+='</tr></table>'

print static_html

然后,IDE 向我发送错误 /usr/bin/python2.7

/home/tsuibin/code/aps/dxx/test_tmp.py
Traceback (most recent call last):
  File "/home/tsuibin/code/aps/dxx/test_tmp.py", line 39, in <module>
    printMainNavigation()
  File "/home/tsuibin/code/aps/dxx/test_tmp.py", line 20, in printMainNavigation
    static_html=  static_html + '<td><a href=" ', each_item[0], ' "> ', each_item[1], ' </a></th> '
TypeError: can only concatenate tuple (not "str") to tuple
Process finished with exit code 1
4

2 回答 2

5

您将逗号放在字符串连接中:

static_html=  static_html + '<td><a href=" ', each_item[0], ' "> ', each_item[1], ' </a></th> '

因此 Python 将这些元素视为一个元组 ( str0 + (str1, str2, str3))。改用+

static_html=  static_html + '<td><a href=" ' + each_item[0] + ' "> ' + each_item[1] + ' </a></th> '

更好的是,使用字符串格式:

static_html += '<td><a href="{0[0]}"> {0[1]} </a></th> '.format(each_item)

在 python 中连接一系列字符串时,实际上使用 alist()作为中介会更快。构建列表,然后用于''.join()获取输出:

static_html = ['<table id="main_nav" align="center"><tr>']
for each_item in items:
     static_html.append('<td><a href="{0[0]}"> {0[1]} </a></th> '.format(each_item))

static_html.append('</tr></table>')
static_html = ''.join(static_html)

请注意,您根本不需要测试each_item in items您已经从循环中的列表中获取了该项目。那只是额外的工作,没有任何作用。

于 2013-01-04T07:25:37.660 回答
4

其他人已经指出了您的错误消息的原因。我冒昧地重新编写了代码。我所做的主要事情是避免字符串连接——在 Python 中,字符串是不可变的,因此连接需要创建一个全新的字符串。避免这种情况的常用方法是将字符串的片段放入列表中,然后使用字符串的join()方法将列表的所有元素连接在一起。

另一个主要变化是使用string.format()方法来创建片段。

items = [
     ["/ank/homepage.py","Home"],
     ["/ank/package.py","Packages"],
     ["/status.py","Status"],
     ["/task.py","Task"],
     ["/report.py","Report"]
]

# Start a list of fragments with the start of the table.
html_fragments = [
    '<table id="main_nav" align="center"><tr>'
]

for item in items:
    # No need for the 'if item in items' here - we are iterating
    # over the list, so we know its in the list.
    #
    # Also, this ifinstance() test is only required if you cannot
    # guarantee the format of the input. I have changed it to allow
    # tuples as well as lists.
    if isinstance(item, (list, tuple)):
        # Use string formatting to create the row, and add it to
        # the list of fragments.
        html_fragments.append('<td><a href="{0:s}">{1:s}</a></td>'.format(*item))

# Finish the table.
html_fragments.append ('</tr></table>')

# Join the fragments to create the final string. Each fragment
# will be separated by a newline in this case. If you wanted it
# all on one line, change it to ''.join(html_fragments).
print '\n'.join(html_fragments)

我得到的输出:

<table id="main_nav" align="center"><tr>
<td><a href="/ank/homepage.py">Home</a></td>
<td><a href="/ank/package.py">Packages</a></td>
<td><a href="/status.py">Status</a></td>
<td><a href="/task.py">Task</a></td>
<td><a href="/report.py">Report</a></td>
</tr></table>
于 2013-01-04T07:27:30.277 回答