当列表长度不同时迭代 Python 中的多个列表
Iterating over Multiple Lists in Python When the Lists are Different Lengths
我有从 MySQL 数据库中提取的 table 数据并试图匹配行,但我无法弄清楚最后一部分。我正在使用 Flask 将使用 'izip_longest' 的三个字典列表传递给 Jinja 模板,然后在 Jinja 模板内使用 for 循环遍历每一行的变量。问题是我正在遍历的三个列表具有可变长度并且并不总是相同的长度。
我已经做到这一点,如下所示,但我需要日期在整行中匹配。在这种情况下,S-Date/07-11 应该在整行中为 07-11,或者在其他列中为空白。
S-Date | S-Place | G-Date | G-Place | F-Date | F-Place
------------------------------------------------------
07-11 7 07-12 7 07-11 7
07-12 7 07-13 7
07-13 7 07-14 7
07-14 7
我觉得我快要搞清楚了,但我一直盯着这个看,但还是不太明白。
这是 SQL table 我正在从中提取数据:
location | date | status
------------------------
001 07-10 success
002 07-10 success
123 07-11 fail
222 07-11 fail
333 07-11 fail
232 07-11 fail
444 07-12 pending
555 07-13 pending
这是获取每一天的失败次数的查询:
SELECT 'date', COUNT(`location`) as 'location' FROM mytable
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
AND `status` LIKE '%fail%' GROUP BY DATE(`date`)
这是每天的成功次数:
SELECT `date`, COUNT(`location`) as 'location' FROM mytable
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
AND `status` NOT LIKE '%fail%' GROUP BY DATE(`date`)
我需要将失败和成功与日期分组相匹配,这样我就可以生成一个堆积条形图来显示单个 date/bar 的每个数字。
不太确定这些 S-Date
、F-Date
业务是什么,但我认为您想知道的是每个唯一日期的总成功和失败次数。
我在 Python 中为您提供了一个非常简单的解决方案:
from collections import defaultdict
from collections import Counter
data = defaultdict(list)
for row in database_table:
data[row[1]].append(row[2])
results = {}
for date, attempts in data.iteritems():
stats = Counter(attempts)
results[date] = {'total': len(attempts)}
for stat, count in stats.most_common():
results[date][stat] = count
现在 results 是一个字典,键是日期,值是另一个包含你所有统计数据的字典。在您的模板中,您只需:
<table>
<thead>
<tr>
<th>Date</th>
<th>Total Places</th>
<th>Success</th>
<th>Failed</th>
<th>Pending</th>
</tr>
</thead>
<tbody>
{% for date, stats in results.iteritems() %}
<tr>
<td>{{ date }}</td>
<td>{{ stats['total'] }}</td>
<td>{{ stats['success'] }}</td>
<td>{{ stats['fail'] }}</td>
<td>{{ stats['pending'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
One thing I'm having trouble with is the dates aren't showing in the
correct order. They go from 07-14 to 07-11, rather than 07-11 to
07-14. I know Python dictionaries are not ordered so now I need to
investigate how to get the dates ordered correctly.
要对日期进行排序,需要先将它们转换为datetime对象;以便排序正常工作。
import datetime
# rest of the code here
fmt = '%m-%d'
results = [] # a list, so its sortable
for date, attempts in data.iteritems():
stats = Counter(attempts)
record = {'total': len(attempts),
'date': datetime.datetime.strptime(date, fmt)}
for stat, count in stats.most_common():
record[stat] = count
results.append(record)
results = sorted(results, key=lamdba x: x['date'])
然后,只需调整您的模板即可:
{% for record in results %}
<tr>
<td>{{ record['date'] }}</td>
<td>{{ record['total'] }}</td>
<td>{{ record['success'] }}</td>
<td>{{ record['fail'] }}</td>
<td>{{ record['pending'] }}</td>
</tr>
{% endfor %}
我有从 MySQL 数据库中提取的 table 数据并试图匹配行,但我无法弄清楚最后一部分。我正在使用 Flask 将使用 'izip_longest' 的三个字典列表传递给 Jinja 模板,然后在 Jinja 模板内使用 for 循环遍历每一行的变量。问题是我正在遍历的三个列表具有可变长度并且并不总是相同的长度。
我已经做到这一点,如下所示,但我需要日期在整行中匹配。在这种情况下,S-Date/07-11 应该在整行中为 07-11,或者在其他列中为空白。
S-Date | S-Place | G-Date | G-Place | F-Date | F-Place
------------------------------------------------------
07-11 7 07-12 7 07-11 7
07-12 7 07-13 7
07-13 7 07-14 7
07-14 7
我觉得我快要搞清楚了,但我一直盯着这个看,但还是不太明白。
这是 SQL table 我正在从中提取数据:
location | date | status
------------------------
001 07-10 success
002 07-10 success
123 07-11 fail
222 07-11 fail
333 07-11 fail
232 07-11 fail
444 07-12 pending
555 07-13 pending
这是获取每一天的失败次数的查询:
SELECT 'date', COUNT(`location`) as 'location' FROM mytable
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
AND `status` LIKE '%fail%' GROUP BY DATE(`date`)
这是每天的成功次数:
SELECT `date`, COUNT(`location`) as 'location' FROM mytable
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
AND `status` NOT LIKE '%fail%' GROUP BY DATE(`date`)
我需要将失败和成功与日期分组相匹配,这样我就可以生成一个堆积条形图来显示单个 date/bar 的每个数字。
不太确定这些 S-Date
、F-Date
业务是什么,但我认为您想知道的是每个唯一日期的总成功和失败次数。
我在 Python 中为您提供了一个非常简单的解决方案:
from collections import defaultdict
from collections import Counter
data = defaultdict(list)
for row in database_table:
data[row[1]].append(row[2])
results = {}
for date, attempts in data.iteritems():
stats = Counter(attempts)
results[date] = {'total': len(attempts)}
for stat, count in stats.most_common():
results[date][stat] = count
现在 results 是一个字典,键是日期,值是另一个包含你所有统计数据的字典。在您的模板中,您只需:
<table>
<thead>
<tr>
<th>Date</th>
<th>Total Places</th>
<th>Success</th>
<th>Failed</th>
<th>Pending</th>
</tr>
</thead>
<tbody>
{% for date, stats in results.iteritems() %}
<tr>
<td>{{ date }}</td>
<td>{{ stats['total'] }}</td>
<td>{{ stats['success'] }}</td>
<td>{{ stats['fail'] }}</td>
<td>{{ stats['pending'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
One thing I'm having trouble with is the dates aren't showing in the correct order. They go from 07-14 to 07-11, rather than 07-11 to 07-14. I know Python dictionaries are not ordered so now I need to investigate how to get the dates ordered correctly.
要对日期进行排序,需要先将它们转换为datetime对象;以便排序正常工作。
import datetime
# rest of the code here
fmt = '%m-%d'
results = [] # a list, so its sortable
for date, attempts in data.iteritems():
stats = Counter(attempts)
record = {'total': len(attempts),
'date': datetime.datetime.strptime(date, fmt)}
for stat, count in stats.most_common():
record[stat] = count
results.append(record)
results = sorted(results, key=lamdba x: x['date'])
然后,只需调整您的模板即可:
{% for record in results %}
<tr>
<td>{{ record['date'] }}</td>
<td>{{ record['total'] }}</td>
<td>{{ record['success'] }}</td>
<td>{{ record['fail'] }}</td>
<td>{{ record['pending'] }}</td>
</tr>
{% endfor %}