我正在尝试根据列Points(PT)
列的最高内容值排列表格行这是我的代码:
<table class="standings-datatable table table-sm">
<thead class="thead-inverse">
<tr>
<th class="bg-dark text-lg-left">TEAM</th>
<th class="bg-dark text-lg-center">M</th>
<th class="bg-dark text-lg-left">W</th>
<th class="bg-dark text-lg-left">L</th>
<th class="bg-dark text-lg-left">T</th>
<th class="bg-dark text-lg-left">N/R</th>
<th class="bg-dark text-lg-left">PT</th>
<th class="bg-dark text-lg-left">NRR</th>
<th class="bg-dark text-lg-left">FOR</th>
<th class="bg-dark text-lg-left">AGAINST</th>
</tr>
</thead>
<tbody>
{% for key in tabledata %}
<tr class="table table-bordered">
<tr>
<td><img class="team-logo" src="/static/{{ key["Sydney image"] }}">{{ key["Sydney Thunders name"]}}</td>
<td>{{ key['Sydney THunder M'] }}</td>
<td>{{ key['Sydney THunder W'] }}</td>
<td>{{ key['Sydney THunder L'] }}</td>
<td>{{ key['Sydney THunder T'] }}</td>
<td>{{ key['Sydney THunder N/R'] }}</td>
<td>{{ key['Sydney THunder PT'] }}</td>
<td>{{ key['Sydney THunder NRR'] }}</td>
<td>{{ key['Sydney THunder FOR'] }}</td>
<td>{{ key['Sydney THunder AGAINST'] }}</td>
</tr>
<tr>
<td><img class="team-logo" src="/static/{{ key["Brisbane image"] }}">{{ key["Brisbane name"]}}</td>
<td>{{ key['Brisbane Heat M'] }}</td>
<td>{{ key['Brisbane Heat W'] }}</td>
<td>{{ key['Brisbane Heat L'] }}</td>
<td>{{ key['Brisbane Heat T'] }}</td>
<td>{{ key['Brisbane Heat N/R'] }}</td>
<td>{{ key['Brisbane Heat PT'] }}</td>
<td>{{ key['Brisbane Heat NRR'] }}</td>
<td>{{ key['Brisbane Heat FOR'] }}</td>
<td>{{ key['Brisbane Heat AGAINST'] }}</td>
</tr>
{%endfor%}
</tbody>
</table>
如果两行具有相同的点(PT)值,那么下一列优先是(NRR),我该如何实现?,如果您有任何其他问题,请问我,谢谢
修复:我在渲染到模板之前按顺序排列数据,我改变了我的代码,以便根据其中一个答案建议计算最高点和NRR基础。
答案 0 :(得分:1)
执行此操作的方法是在将数据呈现在模板中之前将数据排序为所需的顺序。在您的情况下,您可能希望通过删除团队名称和键名称中的统计信息之间的链接来重新组织数据结构,因为这样就无法轻松地对数据进行排序。
以list
dicts
dict
整理您的数据,每个list
都有以下密钥:
&#39;名称&#39; (队名), &#39;图像&#39 ;, &#39; M&#39 ;, &#39; W&#39 ;, &#39; L&#39 ;, &#39; T&#39 ;, &#39; N / R&#39 ;, &#39; PT&#39 ;, &#39; NRR&#39 ;, &#39; FOR&#39 ;, &#39; AGAINST&#39;
现在,您可以使用sort
对象PT
方法对您的团队列表进行排序;您需要为sort方法提供一个函数,告诉它如何对数据进行排序。您可以使用lambda函数返回团队的NRR
和sort
值; sort函数将首先使用第一个值(PT),如果PT相等则使用NRR。 reverse
方法的>>> teams = [{'name': 'Sydney Thunder', 'PT': 5, 'NRR': 0.5}, {'name': 'Brisbane Heat', 'PT': 5, 'NRR': 0.75}, {'name': 'Someone Else', 'PT': 6, 'NRR': 0.2}]
>>> teams.sort(key=lambda x: (x['PT'], x['NRR']), reverse=True)
>>> teams
[{'name': 'Someone Else', 'PT': 6, 'NRR': 0.2}, {'name': 'Brisbane Heat', 'PT': 5, 'NRR': 0.75}, {'name': 'Sydney Thunder', 'PT': 5, 'NRR': 0.5}]
参数按降序对数据进行排序,即点数最多的团队将在列表中排在第一位。
{% for team in teams %}
<tr>
<td><img class="team-logo" src="/static/{{ team["image"] }}">{{ team["name"]}}</td>
<td>{{ team['M'] }}</td>
<td>{{ team['W'] }}</td>
<td>{{ team['L'] }}</td>
<td>{{ team['T'] }}</td>
<td>{{ team['N/R'] }}</td>
<td>{{ team['PT'] }}</td>
<td>{{ team['NRR'] }}</td>
<td>{{ team['FOR'] }}</td>
<td>{{ team['AGAINST'] }}</td>
</tr>
{% endfor %}
将已排序的列表传递给您的模板并循环遍历它:
(select distinct col1 from table)
UNION DISTINCT (select distinct col2 from table)
UNION DISTINCT (select distinct col3 from table)