从另一个列表中创建一个新列表,并通过重复填充缺失值
我有两个列表,一个包含工作日,另一个包含与工作日相对应的费用:
wd = [1, 4, 5, 6]
fees = [1.44, 1.17, 1.21, 1.26]
我需要所有工作日的第三个列表,在工作日中填写费用,这些费用没有前一天的费用,而没有费用:
result = [1.44, 1.44, 1.44, 1.17, 1.21, 1.26]
correspDay = [1, 2, 3, 4, 5, 6]
我该如何编码?
I have two lists, one contains the working days, the other one contains the fees corresponding to the working days:
wd = [1, 4, 5, 6]
fees = [1.44, 1.17, 1.21, 1.26]
I need a third list with all workdays, filling up fees in the workdays that do not have fees with fees from the previous day:
result = [1.44, 1.44, 1.44, 1.17, 1.21, 1.26]
correspDay = [1, 2, 3, 4, 5, 6]
How can I code this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会将这些值放在字典中,然后循环循环到一周的日子(1-7)检查我们当天是否已经有一个值。如果您确实有那天商店的价值,那么第二天就没有价值。如果没有值在字典中创建一个用钥匙的键和最后一项费用的价值创建项目。
最后,我已经对列表进行了排序,但是没有其他理由除了使输出更易于阅读之外
I would put the values in a dictionary, then loop through the days of the week (1-7) checking if we already have a value for that day. If you do have a value for that day store it incase the next day doesn't have a value. If there isn't a value create an item in the dictionary with a key for that day and a value of the last fee.
At the end I have sorted the list but there is no real reason to do this other than to make the output easier to read
feemap
地图天
费用必须付款prev
用于存储上一个值(费用)。输出:
feeMap
mapsdays
tofee
that has to be paidprev
is used to store the previous value(fee).output:
我什么都没做,我看到上面的好人,我试图为您更加清楚。
我只是添加此行以在字典中输出输出
:
I didn't do anything I saw the good people above and I try to make it more clear for you.
I just add this line to make the output in dictionary
output:
我首先要构建一个字典,以便每天查找费用:
然后用简单的
range> range
构建correspday
:并构建
result
通过迭代conerespday
,使用fee_dict
查找费用,并在fee_dict
空白时使用最后一个条目:I'd start by building a dictionary to be able to look up fees by day:
Then build
correspDay
with a simplerange
:and build
result
by iterating overcorrespDay
, usingfee_dict
to look up the fees and using the last entry whenfee_dict
comes up empty: