not able to add extra values in a list using for loop in python

时间:2018-02-03 07:55:05

标签: python list for-loop split

I need to write a function takes one argument, a list of strings, and returns a single string which is an HTML list. For example, if the function should produce the following string when provided the list ['first string]', 'second string'], the result should be like this

<ul>
  <li>first string</li>
  <li>second string</li>
</ul>

Code that i am using is

def new_func(value):
    capitalized_names = [] 
    capitalized_names.append("<ul>") 
    for values in value:
        capitalized_names.append("<li>" + values + "</li>") 
    capitalized_names.append("</ul>") 
    return capitalized_names

print(new_func(['first string', 'second string']))

It is throwing the error

code raised an exception, "'list' object has no attribute 'strip'".

Can anyone please tell the correct way to achieve result

3 个答案:

答案 0 :(得分:0)

Your code cannot throw that error.Please check again... You have not used strip() in the code anywhere.For more neatness to get every list item in new line, modify your code as:

def new_func(value):
capitalized_names = [] 
capitalized_names.append("<ul>") 
for values in value:
    capitalized_names.append("    <li>" + values + "</li>") 
capitalized_names.append("</ul>") 
return '\n'.join(capitalized_names)

print(new_func(['first string', 'second string']))

答案 1 :(得分:0)

Your code doesn't run with any errors when I try it. It outputs this:

['<ul>', '<li>first string</li>', '<li>second string</li>', '</ul>']

Have you tried isolating just this function from everything else in its environment and testing it all by itself? The error is probably coming from somewhere else besides this script.

If you want a html list, then using a python list won't work, instead use python strings.

def new_func(value):
capitalized_names = ""
capitalized_names+="<ul>" 
for values in value:
    capitalized_names+="<li>" + values + "</li>"
capitalized_names+="</ul>"
return capitalized_names

This should output

<ul><li>first string</li><li>second string</li></ul>

If you want your whitespace (new lines, indents) to look nice, then you can add "\n" for a new line, and spaces to indent:

for values in value:
    capitalized_names+="\n  <li>" + values + "</li>"
capitalized_names+="\n</ul>"

答案 2 :(得分:0)

Your code does not throw an error (you aren't even calling strip), however it does return a list of the lines, rather than one string as you asked for.

To get the string, you can do it in one line:

def html_list(l):
    return '<ul>\n\t' + '\n\t'.join('<li>'+s+'</li>' for s in l) + '\n</ul>'

which you should be able to see, works:

>>> html_list(['first string', 'second string'])
'<ul>\n\t<li>first string</li>\n\t<li>second string</li>\n</ul>'
>>> print(html_list(['first string', 'second string']))
<ul>
    <li>first string</li>
    <li>second string</li>
</ul>

why?

The first step is to create a generator that will yield each string in l - surrounded with <li> tags. This can be done really simply using a comprehension:

>>> ['<li>'+s+'</li>' for s in l]
['<li>first string</li>', '<li>second string</li>']

Note that I have used a list-comprehension for this example, but the solution uses a generator as they are more efficient in terms of memory.

We then want to join each of these modified strings together with one new-line (\n) and a tab (\t) between each element. This can be done really easily with str.join which works in the following way:

>>> 'sep'.join(map(str, range(8)))
'0sep1sep2sep3sep4sep5sep6sep7'

The final step is to add the <ul> tags around this string which is formed and that gives us the final result of something like:

'<ul>\n\t<li>first string</li>\n\t<li>second string</li>\n</ul>'

which, when passed into print(), will give you the right output.

相关问题