1

This is what I have done when there is no HTML codes

from collections import defaultdict

hello = ["hello","hi","hello","hello"]
def test(string):
    bye = defaultdict(int)
    for i in hello:
        bye[i]+=1
    return bye

And i want to change this to html table and This is what I have try so far, but it still doesn't work

 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

Erika Sawajiri
  • 1,136
  • 3
  • 13
  • 18

4 Answers4

2
from collections import defaultdict

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

def test2(strList):
  d = defaultdict(int)
  for k in strList:
    d[k] += 1
  print('<table>')
  for i in d.items():
    print('<tr><td>{0[0]}</td><td>{0[1]}</td></tr>'.format(i))
  print('</table>')

test2(hello)

Output

<table>
  <tr><td>hi</td><td>1</td></tr>
  <tr><td>hello</td><td>3</td></tr>
</table>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

You can use collections.Counter to count occurrences in a list, then use this information to create the html table. Try this:

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>"

The output of this code will be (you can change the format if you want):

>>> <table>
>>> <tr>
>>> <td>hi:1</td>
>>> </tr>
>>> <tr>
>>> <td>hello:3</td>
>>> </tr>
>>> </table>

Hope this help you!

jvallver
  • 2,230
  • 2
  • 11
  • 20
1

You can't assign a variable in the middle of a print statement. You also can't concatenate string types and integer types in a print statement.

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

Should be

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

And your return statement comes before the final print, so it will never print.

Full function

def test2(string):
    bye= defaultdict(int)
    print"<table>"
    for i in hello:
        print "<tr>"
        bye[i] = bye[i] + 1
        print "<td>"
        print bye[i]
        print '</td>'
        print "</tr>"
        print"</table>"
        return bye

That would be an exact, working translation of your code, but I'm not sure why you are going about it that way. bye is pointless here, as you are just printing 1 everytime

Shane
  • 2,315
  • 3
  • 21
  • 33
1

Python collections module contains Counter function, that do exactly, what is needed:

>>> from collections import Counter
>>> hello = ["hello", "hi", "hello", "hello"]
>>> print Counter(hello)
Counter({'hello': 3, 'hi': 1})

Now, you want to generate html. Better way is to use existing libraries for this. For example Jinja2. You just need to install it, for example using pip:

pip install Jinja2

Now, the code will be:

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))
stalk
  • 11,934
  • 4
  • 36
  • 58