Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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
如何将列表更改为HTML表格?(Python)_Python_Html - Fatal编程技术网

如何将列表更改为HTML表格?(Python)

如何将列表更改为HTML表格?(Python),python,html,Python,Html,这就是我在没有HTML代码时所做的 from collections import defaultdict hello = ["hello","hi","hello","hello"] def test(string): bye = defaultdict(int) for i in hello: bye[i]+=1 return bye 我想把它改成html表格,这是我到目前为止尝试过的,但仍然不起作用 def test2(string):

这就是我在没有HTML代码时所做的

from collections import defaultdict

hello = ["hello","hi","hello","hello"]
def test(string):
    bye = defaultdict(int)
    for i in hello:
        bye[i]+=1
    return bye
我想把它改成html表格,这是我到目前为止尝试过的,但仍然不起作用

 def test2(string):
    bye= defaultdict(int)
    print"<table>"
    for i in hello:
        print "<tr>"
        print "<td>"+bye[i]= bye[i] +1+"</td>"
        print "</tr>"
    print"</table>"
    return bye
def test2(字符串):
bye=defaultdict(int)
打印“”
对我来说,你好:
打印“”
打印“+bye[i]=bye[i]+1+”
打印“”
打印“”
回头见

您可以使用
collections.Counter
计算列表中的出现次数,然后使用此信息创建html表。 试试这个:

from collections import Counter, defaultdict

hello = ["hello","hi","hello","hello"]
counter= Counter(hello)
bye = defaultdict(int)
print"<table>"
for word in counter.keys():
    print "<tr>"
    print "<td>" + str(word) + ":" + str(counter[word]) + "</td>"
    print "</tr>"
    bye[word] = counter[word]
print"</table>"
来自集合导入计数器,defaultdict
你好=[“你好”,“你好”,“你好”,“你好”]
计数器=计数器(你好)
bye=defaultdict(int)
打印“”
对于计数器中的字。键():
打印“”
打印“+str(word)+”:“+str(counter[word])+”
打印“”
拜拜[单词]=计数器[单词]
打印“”
此代码的输出将是(如果需要,可以更改格式):

>
>>> 
>>>嗨:1
>>> 
>>> 
>>>你好:3
>>> 
>>> 

希望这对你有帮助

您可以使用
collections.Counter
计算列表中的出现次数,然后使用此信息创建html表。 试试这个:

from collections import Counter, defaultdict

hello = ["hello","hi","hello","hello"]
counter= Counter(hello)
bye = defaultdict(int)
print"<table>"
for word in counter.keys():
    print "<tr>"
    print "<td>" + str(word) + ":" + str(counter[word]) + "</td>"
    print "</tr>"
    bye[word] = counter[word]
print"</table>"
来自集合导入计数器,defaultdict
你好=[“你好”,“你好”,“你好”,“你好”]
计数器=计数器(你好)
bye=defaultdict(int)
打印“”
对于计数器中的字。键():
打印“”
打印“+str(word)+”:“+str(counter[word])+”
打印“”
拜拜[单词]=计数器[单词]
打印“”
此代码的输出将是(如果需要,可以更改格式):

>
>>> 
>>>嗨:1
>>> 
>>> 
>>>你好:3
>>> 
>>> 

希望这对你有帮助

> P>不能在打印语句的中间赋值。也不能在print语句中连接字符串类型和整数类型

print "<td>"+bye[i]= bye[i] +1+"</td>"

这将是一个准确的、有效的代码翻译,但我不知道你为什么要这样做<代码>再见<代码>是毫无意义的,因为你只是打印1每一次

你不能在一个打印语句的中间分配一个变量。也不能在print语句中连接字符串类型和整数类型

print "<td>"+bye[i]= bye[i] +1+"</td>"

这将是一个准确的、有效的代码翻译,但我不知道你为什么要这样做<代码>再见在这里是毫无意义的,因为您每次只需打印1,Python
collections
模块包含函数,该函数正好完成所需的操作:

>>> from collections import Counter
>>> hello = ["hello", "hi", "hello", "hello"]
>>> print Counter(hello)
Counter({'hello': 3, 'hi': 1})
现在,您需要生成html。更好的方法是为此使用现有库。例如您只需安装它,例如使用:

现在,代码将是:

from jinja2 import Template
from collections import Counter

hello = ["hello", "hi", "hello", "hello"]

template = Template("""
<table>
    {% for item, count in bye.items() %}
         <tr><td>{{item}}</td><td>{{count}}</td></tr>
    {% endfor %}
</table>
""")

print template.render(bye=Counter(hello))
来自jinja2导入模板的

从收款进口柜台
你好=[“你好”,“你好”,“你好”,“你好”]
模板=模板(“”)
{%对于项,计数为bye.items()%}
{{item}}{{count}
{%endfor%}
""")
打印模板.render(bye=Counter(hello))

Python
collections
模块包含函数,该函数准确地执行所需的操作:

>>> from collections import Counter
>>> hello = ["hello", "hi", "hello", "hello"]
>>> print Counter(hello)
Counter({'hello': 3, 'hi': 1})
现在,您需要生成html。更好的方法是为此使用现有库。例如您只需安装它,例如使用:

现在,代码将是:

from jinja2 import Template
from collections import Counter

hello = ["hello", "hi", "hello", "hello"]

template = Template("""
<table>
    {% for item, count in bye.items() %}
         <tr><td>{{item}}</td><td>{{count}}</td></tr>
    {% endfor %}
</table>
""")

print template.render(bye=Counter(hello))
来自jinja2导入模板的

从收款进口柜台
你好=[“你好”,“你好”,“你好”,“你好”]
模板=模板(“”)
{%对于项,计数为bye.items()%}
{{item}}{{count}
{%endfor%}
""")
打印模板.render(bye=Counter(hello))
从集合导入defaultdict
你好=[“你好”,“你好”,“你好”,“你好”]
def测试2(strList):
d=默认dict(int)
对于strList中的k:
d[k]+=1
打印(“”)
对于i/d.items():
打印({0[0]}{0[1]})。格式(i))
打印(“”)
test2(你好)
输出


hi1
你好
从集合导入defaultdict
你好=[“你好”,“你好”,“你好”,“你好”]
def测试2(strList):
d=默认dict(int)
对于strList中的k:
d[k]+=1
打印(“”)
对于i/d.items():
打印({0[0]}{0[1]})。格式(i))
打印(“”)
test2(你好)
输出


hi1
你好

它仍然不起作用。>回溯(最近一次呼叫最后一次):打印“+bye[i]=bye[i]+1+”^SyntaxError:无效syntaxwhat's your expected output here?@AshwiniChaudhary我想要与第一次相同的输出,但是添加了HTML代码,为什么在函数完成之前返回?@ErikaSawajiri第一个和第二个之间有区别,第一个中没有打印任何内容。发布预期的html输出以澄清问题。它仍然不起作用。>回溯(最近一次调用上次):打印“+bye[i]=bye[i]+1+”^SyntaxError:无效syntaxwhat's expected output here?@AshwiniChaudhary我想要与第一次相同的输出,但是添加了HTML代码,为什么在函数完成之前返回?@ErikaSawajiri第一个和第二个之间有区别,第一个中没有打印任何内容。发布预期的html输出,以澄清问题。