Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:列表列表和HTML表格帮助_Python_Html - Fatal编程技术网

Python:列表列表和HTML表格帮助

Python:列表列表和HTML表格帮助,python,html,Python,Html,我在将列表中的值添加到html表中时遇到问题,例如,如果列表包含: food_list = [['A','B'], ['Apple','banana'], ['Fruit','Fruit']] 如何将每个值附加到对应的HTML表中?因此,代码如下所示: <table> <tr><td>A</td><td>Apple</td><td>Fruit</td></tr> <tr>&l

我在将列表中的值添加到html表中时遇到问题,例如,如果列表包含:

food_list = [['A','B'], ['Apple','banana'], ['Fruit','Fruit']]
如何将每个值附加到对应的HTML表中?因此,代码如下所示:

<table>
<tr><td>A</td><td>Apple</td><td>Fruit</td></tr>
<tr><td>B</td><td>Banana</td><td>Fruit</td></tr>
</table>

苹果果
香蕉果
我能得到的最接近的是下面的代码,但是我得到了一个列表索引超出范围的错误

print '<table>'
for i in food_list:
    print '<tr>'
    print '<tr><td>'+i[0]+'</td><td>'+i[1]+'</td><td>'+i[2]+'</td></tr>'
    print '</tr>'
print' </table>'
打印“
对于食品清单中的i:
打印“
打印'+i[0]+'+i[1]+'+i[2]+''
打印“
打印“

该表有两个元素,因此您可以使用0和1作为索引。以下是您重写的示例:

print '<table>'
for i in food_list:
    print '<tr>'
    print '<tr><td>'+i[0]+'</td><td>'+i[1]+'</td></th>'
    print '</tr>'
print' </table>'
打印“
对于食品清单中的i:
打印“
打印'+i[0]+''+i[1]+''
打印“
打印“

该表有两个元素,因此您可以使用0和1作为索引。以下是您重写的示例:

print '<table>'
for i in food_list:
    print '<tr>'
    print '<tr><td>'+i[0]+'</td><td>'+i[1]+'</td></th>'
    print '</tr>'
print' </table>'
打印“
对于食品清单中的i:
打印“
打印'+i[0]+''+i[1]+''
打印“
打印“

我会这样做

# Example data.
raw_rows = [["A", "B"], ["Apple", "Banana"], ["Fruit", "Fruit"]]
# "zips" together several sublists, so it becomes [("A", "Apple", "Fruit"), ...].
rows = zip(*raw_rows) 

html = "<table>"
for row in rows:
   html += "<tr>"
   # Make <tr>-pairs, then join them.
   html += "\n".join(map(lambda x: "<td>" + x + "</td>", row)) 
   html += "</tr>"

html += "</table>"
#示例数据。
生的排数=[“A”、“B”]、[“苹果”、“香蕉”]、[“水果”、“水果”]]
#“zips”将几个子列表组合在一起,因此它成为[(“A”、“Apple”、“Fruit”),…]。
行=zip(*原始行)
html=“”
对于行中的行:
html+=“”
#配对,然后加入他们。
html+=“\n”.join(映射(lambda x:“+x+”,行))
html+=“”
html+=“”

也许不是最快的版本,但它将行包装成元组,然后我们可以迭代并连接它们。

我会这样做

# Example data.
raw_rows = [["A", "B"], ["Apple", "Banana"], ["Fruit", "Fruit"]]
# "zips" together several sublists, so it becomes [("A", "Apple", "Fruit"), ...].
rows = zip(*raw_rows) 

html = "<table>"
for row in rows:
   html += "<tr>"
   # Make <tr>-pairs, then join them.
   html += "\n".join(map(lambda x: "<td>" + x + "</td>", row)) 
   html += "</tr>"

html += "</table>"
#示例数据。
生的排数=[“A”、“B”]、[“苹果”、“香蕉”]、[“水果”、“水果”]]
#“zips”将几个子列表组合在一起,因此它成为[(“A”、“Apple”、“Fruit”),…]。
行=zip(*原始行)
html=“”
对于行中的行:
html+=“”
#配对,然后加入他们。
html+=“\n”.join(映射(lambda x:“+x+”,行))
html+=“”
html+=“”

也许不是最快的版本,但它将行包装成元组,然后我们可以对它们进行迭代并将它们连接起来。

我想您正在寻找以下内容:

print '<table>'
for i in zip(*food_list):
    print '<tr>'
    print '<td>'+i[0]+'</td><td>'+i[1]+'</td><td>'+i[2]+'</td>'
    print '</tr>'
print' </table>'
打印“
对于zip中的i(*食品清单):
打印“
打印'+i[0]+'+i[1]+'+i[2]+''
打印“
打印“

我想你在找这个:

print '<table>'
for i in zip(*food_list):
    print '<tr>'
    print '<td>'+i[0]+'</td><td>'+i[1]+'</td><td>'+i[2]+'</td>'
    print '</tr>'
print' </table>'
打印“
对于zip中的i(*食品清单):
打印“
打印'+i[0]+'+i[1]+'+i[2]+''
打印“
打印“

顺便说一句,如果你能从一开始就对列表进行不同的结构,比如
[['A'、'Apple'、'Fruit']、['B'、'banana'、'Fruit']]
,可能会更有意义。这样,每一行在逻辑上对应一个“东西”(即苹果的一行、香蕉的一行等),加上您试图使用的代码,就可以工作了。顺便说一句,如果您能首先以不同的方式构造列表,可能更有意义,如
[['a'、'apple'、'Fruit']、['B'、'banana'、'Fruit']
。这样,每一行逻辑上对应一个“东西”(即,一行代表苹果,一行代表香蕉,等等),加上您试图使用的代码,就可以工作了。