2017-10-10 169 views
0

我试图从python列表中创建订单列表项目(<ol></ol>)。 我的情况如下表所示:如何从python列表中生成订单列表项目

listItem = [  
      ['101','Dashboard'], 
      ['102','Potential Customer'], 
      ['102-01','Potential Customer Activity'], 
      ['102-02','Potential Customer Report'], 
      ['102-99','Potentail Customer Detail'], 
      ['102-99-01','Potential Customer Detail Listing'], 
      ['102-99-02','Potential Customer Follow Up'], 
      ['102-99-02-01','Follow Up PAR'] 
     ] 

从这个名单,我希望能按照使其为顺序列表项itemID101102102-01 ..等,将是这样的:

<ol> 
    <li data-itemID="101">Dashboard</li> 
    <li data-itemID="102">Potential Customer 
     <ol> 
      <li data-itemID="102-01">Potential Customer Activity</li> 
      <li data-itemID="102-99">Potential Customer Detail 
       <ol> 
        <li data-itemID="102-99-01">Potential Customer Detail Listing</li> 
        <li data-itemID="102-99-02">Potential Customer Follow Up 
         <ol> 
          <li data-itemID="102-99-02-01">Follow Up PAR</li> 
         </ol> 
        </li> 
       </ol> 
      </li> 
     </ol> 
    <li> 
<ol> 

我已经试过我的代码如下:

from flask import * 
import re 

app = Flask(__name__) 


@app.route('/', methods=['GET','POST']) 
def checkName(): 

    orderList = '' 
    listItem = [  
       ['101','Dashboard'], 
       ['102','Potential Customer'], 
       ['102-01','Potential Customer Activity'], 
       ['102-02','Potential Customer Report'], 
       ['102-99','Potentail Customer Detail'], 
       ['102-99-01','Potential Customer Detail Listing'], 
       ['102-99-02','Potential Customer Follow Up'], 
       ['102-99-02-01','Follow Up PAR'] 
      ] 
    print "List Item: ",listItem 
    orderList += '<ol>' 
    for item in listItem: 
     if item[0].find('-') > 0: 
      # list has sub-list item 
      orderList += '<ol>' 
      orderList += '<li data-itemID=%s>'%(item[0]) 
      orderList += item[1] 
      orderList += '</li>' 
      orderList += '</ol>' 
     else: 
      # list has no sub-list item 
      orderList += '<li data-itemID=%s>'%(item[0]) 
      orderList += item[1] 
      orderList += '</li>' 

    orderList += '</ol>' 

    return render_template('home.html', orderList=orderList) 

if __name__ == '__main__': 

    app.run(debug=True) 

家。 HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Home Page</title> 
</head> 
<body> 
    {% if orderList %} 
     {{ orderList | safe }} 
    {% else %} 
     <p>Please select a list menu</p> 
    {% endif %} 
</body> 
</html> 

但是,生成的顺序列表并不如我上面所料,它不会有一个正确的子次序列表可言。在代替,它的结果是这样的:

1. Dashboard 
2. Potential Customer 
    1. Potential Customer Activity 
    1. Potential Customer Report 
    1. Potentail Customer Detail 
    1. Potential Customer Detail Listing 
    1. Potential Customer Follow Up 
    1. Follow Up PAR 

我怎样才能更正我的代码来实现的结果正如我上面预期?谢谢。

回答

1

这里的代码假设,虽然它通过迭代,每次遇到数据ID中带有破折号的项目时,都应该启动新的OL。但这并不是你想要的。

对于这样的结构,我倾向于递归算法来处理子项目(这有助于避免如上所述的假设)。我不知道你的优势的情况下,但这样的事情可能会为你工作:

# somewhere else in your codebase, let's say utils.py 

import copy 


def structure_listitem(listitem): 
    items = copy.deepcopy(listitem) 

    if not items: 
     return items 

    if len(items) == 1: 
     items[0].append([]) 
     return items 

    first, rest = items[0], items[1:] 

    header = first[0] 
    sub_items = [] 
    while rest and rest[0][0].startswith(header): 
     sub_items.append(rest.pop(0)) 

    first.append(structure_listitem(sub_items)) 
    return [first] + structure_listitem(rest) 


def render_structured_listitem(listitem): 
    if not listitem: 
     return '' 

    out = ['<ol>'] 
    for item in listitem: 
     the_id, header, sublist = item 
     if sublist: 
      out.append('<li data-itemID="%s">%s\n%s\n</li>' 
         % (the_id, header, render_structured_listitem(sublist))) 
     else: 
      out.append('<li data-itemID="%s">%s</li>' 
         % (the_id, header)) 

    out.append('</ol>') 
    return '\n'.join(out) 


def render_listitem(listitem): 
    return render_structured_listitem(structure_listitem(listitem))  

,然后,在应用程序代码:

from utils import render_listitem # or from wherever 

# ... 

@app.route('/', methods=['GET','POST']) 
def check_name(): 
    listitem = [  
       ['101','Dashboard'], 
       ['102','Potential Customer'], 
       ['102-01','Potential Customer Activity'], 
       ['102-02','Potential Customer Report'], 
       ['102-99','Potentail Customer Detail'], 
       ['102-99-01','Potential Customer Detail Listing'], 
       ['102-99-02','Potential Customer Follow Up'], 
       ['102-99-02-01','Follow Up PAR'] 
    ]  

    ordered_list = render_listitem(listitem) 

    return render_template('home.html', ordered_list=ordered_list) 
+0

道歉 - 我最初的反应中使用Python 3中,但我看到你正在使用Python 2.我已经修改它在python 2中工作。 – ryanmrubin

+0

非常感谢你,我测试了你的答案,它真的与主项目,子列表和子级列表有很大关系!谢谢。 –

0

如果你不介意有没有很好地形成HTML去(我不是关闭<ol>,你可以有这样的解决方案。 要具有完全兼容的HTML在相同的方向一点点工作是必要的。

flask_file.py

from flask import * 
import re 

app = Flask(__name__) 


itid = lambda x: x[0] 
itval = lambda x: x[1] 
last = lambda x: x[-1] 

def wrapit(it_id): 
    return '<li item-id="%s">' % it_id 

def wrapval(id_val): 
    return '%s</li>' % id_val 

def put_ol(it_id): 
    return last(it_id.split('-')) == '01' 

@app.route('/', methods=['GET','POST']) 
def checkName(): 
    listItem = [ 
       ['101','Dashboard'], 
       ['102','Potential Customer'], 
       ['102-01','Potential Customer Activity'], 
       ['102-02','Potential Customer Report'], 
       ['102-99','Potentail Customer Detail'], 
       ['102-99-01','Potential Customer Detail Listing'], 
       ['102-99-02','Potential Customer Follow Up'], 
       ['102-99-02-01','Follow Up PAR'] 
      ] 
    orderItem = [] 

    for item in listItem: 
     if put_ol(itid(item)): 
      orderItem.append('<ol>') 
     orderItem.append(wrapit(itid(item))) 
     orderItem.append(wrapval(itval(item))) 

    print("List Item: %s " % listItem) 

    return render_template('home.html', orderItem=orderItem) 

if __name__ == '__main__': 

    app.run(debug=True) 

home.html的

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Home Page</title> 
</head> 
<body> 
<ol> 
{% for item in orderItem %} 
     {{ item | safe }} 
    {% endfor %} 
</ol> 
</body> 
</html>