- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力解决如何循环遍历列表,将缺少的月份添加到原始字典中的 0。我正在考虑从 calendar
创建一个月份列表,循环遍历每个月,然后循环遍历我的数据中的每个月......但不太清楚如何更新字典缺少一个按正确顺序排列的月份。
import calendar
my_dict = {'Green Car':
[('January', 340),
('February', 2589),
('March', 12750),
('April', 114470),
('July', 4935),
('August', 1632),
('September', 61),
('December', 3409)],
'Red Truck':
[('January', 2325185),
('February', 209794),
('March', 201874),
('April', 19291),
('May', 18705),
('July', 22697),
('August', 22796)],
'Police Car':
[('January', 2037),
('February', 2620),
('March', 1480),
('April', 15630),
('July', 40693),
('August', 2329)],
'Zamboni':
[('January', 256),
('February', 426690),
('March', 589),
('April', 4740),
('May', 880),
('July', 1016),
('August', 106),
('September', 539),
('October', 598),
('November', 539),
('December', 470)],
'Witch Broom':
[('February', 350),
('March', 3520),
('October', 2703),
('November', 2221),
('December', 664)]
}
def fill_months(reported_months):
const_months = list(calendar.month_name)
x = 0
print("Looking for months in", reported_months)
# print(const_months)
for const_month in const_months:
for month in reported_months:
if const_month != month[0] and len(const_month) > 0:
print(const_month, month[0])
print("You don't have", const_month, "in the months group:", reported_months)
def main():
for commod, months in my_dict.items():
# print(commod)
# print(commod, months)
fill_months(months)
if __name__ == '__main__':
main()
对于每个键(“绿色汽车”、“红色卡车”等),我想循环遍历并添加值为 0
的缺失飞蛾。所以“绿色汽车”最终会是:
my_dict = {'Green Car':
[('January', 340),
('February', 2589),
('March', 12750),
('April', 114470),
('May', 0),
('June', 0),
('July', 4935),
('August', 1632),
('September', 61),
('October', 0),
('November', 0),
('December', 3409)],
<小时/>
我对此有所了解 - 但逻辑感觉有点困惑:
def fill_months(reported_months):
const_months = list(calendar.month_name)
x = 0
temp_months = []
for i in reported_months:
temp_months.append(i[0])
print("Looking for months in", reported_months)
# print(const_months)
for const_month in const_months:
if len(const_month) > 0:
if const_month not in temp_months:
reported_months.insert(x-1, (const_month, 0))
x += 1
print(reported_months)
最佳答案
添加缺失月份的逻辑必须检查的就是月份是否与您拥有的某些月份名称列表(months
)的索引不匹配。如果匹配则递增,并循环直到覆盖了 months
的每个成员。
months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
def add_missing_months(vehicle_d):
for vehicle in vehicle_d:
ind = 0
month_l = vehicle_d[vehicle]
while ind < len(months):
if ind >= len(month_l) or month_l[ind][0] != months[ind]:
month_l.insert(ind, (months[ind], 0))
else:
ind += 1
return vehicle_d
关于python - 循环字典的内部列表,添加缺失的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399102/
我编写了一个 Android 应用程序,它使用 Azure 来执行用户通过 Google、Twitter 和 Facebook 的登录;它使用 Microsoft.WindowsAzure.Mobil
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我正在学习“绘图应用程序”的教程。当我在 Firefox 上启动我的应用程序时,Firebug 告诉我“在语句之前缺少 ;” 我在第 9 行调用函数的位置。我只是不明白应该将这些“;”放在哪里. va
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我在 Firebug 中不断收到关于 onClick 事件的错误。 我已经尝试了 "和 ' 的各种不同组合,但无济于事。在添加 onClick 事件之前,这工作正常。 有人能发现我可能做错了什么吗?
Visual Studio 2015 告诉我找不到 WSASetSocketSecurity。 该 dll 存在并且还包括似乎没问题。 我的包括: windows.h stdio.h Wincrypt
我需要访问 eloquent 的 whereHasNot方法(此处添加: https://github.com/laravel/framework/commit/8f0cb08d8ebd157cbfe
跟随宠物物体检测的 TF 教程:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/run
构建路径 > 添加库 > JUnit 无法添加 JUnit3 或 JUnit4 组件。 我在.log 中看到这样的消息 !MESSAGE No property tester contributes
我正在运行此脚本来查看网络上的摄像机: gst-launch udpsrc port=1234 ! "application/x-rtp, payload=127" ! rtph264depay !
我正在使用http://java.sun.com/jsp/jstl/fmt用于从 Spring 配置中设置的 Message Resource Bundle 输出消息的标签库。消息解析也可以放在 Co
我正在将 Ninject 与 MVC4 连接起来,并让它工作到尝试实际解决依赖关系的程度。但是,我收到以下异常: Method not found: 'System.Web.Http.Services
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我在启动 ASP.NET MVC5 应用程序时遇到问题。到目前为止一切正常。启动应用程序时出现以下错误: Could not load file or assembly 'Microsoft.Appl
我已经使用以下方法创建了一个环境: conda create --prefix C:\Users\Dell\Dropbox\DjangoProjects\webenv python=3.6 执行后:c
我们有一个遗留的 Web 窗体应用程序,我们最近将其从网站项目转换为 Web 应用程序项目。 Web 窗体项目是解决方案的“启动”项目。 有一个 MVC 项目是对 Web 窗体项目的引用。 在 MVC
使用某种字体,我使用Java的FontLayout来确定它的上升、下降和行距。 (参见 Java 的 FontLayout 教程 here) 在我的具体案例中,我使用的是 Arial Unicode
我正在尝试在 linux 下编译 qt ffmpeg 包装器简单编码/解码示例 QTFFmpegWrapper source # Set list of required FFmpeg librari
我正在使用来自开发人员 android 页面的 SlidingTabLayout.java。在我使用 slidingTabLayout.setDistributeEvenly(true); 使 sli
我正在尝试使用 v360 filter 将 180° 鱼眼视频转换为普通/常规视频的 FFmpeg . 这是我尝试过的命令:ffmpeg -i in.mp4 -vf "v360=input=fishe
我是一名优秀的程序员,十分优秀!