从另一个列表创建一个新列表并通过重复填充缺失值
Creating a new list from another one and filling missing values by repeating
我有两个列表,一个是工作日,一个是工作日对应的费用:
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]
我该如何编码?
我首先要构建一个字典,以便能够按天查找费用:
>>> wd = [1,4,5,6]
>>> fees = [1.44, 1.17, 1.21, 1.26]
>>> fee_dict = dict(zip(wd, fees))
然后用简单的 range
构建 correspDay
:
>>> correspDay = list(range(1, 7))
并通过遍历 correspDay
构建 result
,使用 fee_dict
查找费用并在 fee_dict
为空时使用最后一个条目:
>>> result = []
>>> for i in correspDay:
... result.append(fee_dict.get(i) or result[-1])
...
>>> result
[1.44, 1.44, 1.44, 1.17, 1.21, 1.26]
我会把这些值放在字典中,然后遍历一周中的几天 (1-7),检查我们是否已经有了那一天的值。如果您那天确实有值,请将其存储起来,以防第二天没有值。如果没有值,则使用当天的键和最后一笔费用的值在字典中创建一个项目。
最后我对列表进行了排序,但除了使输出更易于阅读之外没有真正的理由这样做
fees_dict = {1: 1.44, 4: 1.17, 5: 1.21, 6: 1.26}
last_fee = 0
for i in range(1, 8):
if i in fees_dict:
last_fee = fees_dict[i]
else:
fees_dict[i] = last_fee
sorted_fees_dict = dict(sorted(fees_dict.items()))
print(sorted_fees_dict)
feeMap
映射days
到fee
需要支付的prev
用于存储之前的值(费用)
#!/usr/bin/env python3.10
wd = [1,4,5,6]
fees = [1.44, 1.17, 1.21, 1.26]
corresDay = list(range(1, 6))
feeMap = dict()
index = 0
for index, day in enumerate(wd):
feeMap[day] = fees[index]
prev = None
for day in range(1, 6):
if day not in feeMap.keys():
feeMap[day] = prev
else:
prev = feeMap[day]
feeMap = sorted(feeMap.items())
print(feeMap)
输出:
$ ./working_days.py
[(1, 1.44), (2, 1.44), (3, 1.44), (4, 1.17), (5, 1.21), (6, 1.26)]
我什么都没做,看到上面的好人,我尽量给你讲清楚。
wd = [1,4,5,6]
fees = [1.44, 1.17, 1.21, 1.26]
fee_dict = dict(zip(wd, fees))
correspDay = list(range(1,8))
result = []
for i in correspDay:
result.append(fee_dict.get(i) or result[-1])
我只是添加这一行以在字典中输出
result = dict(zip(correspDay, result))
输出:
{1: 1.44, 2: 1.44, 3: 1.44, 4: 1.17, 5: 1.21, 6: 1.26, 7: 1.26}
我有两个列表,一个是工作日,一个是工作日对应的费用:
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]
我该如何编码?
我首先要构建一个字典,以便能够按天查找费用:
>>> wd = [1,4,5,6]
>>> fees = [1.44, 1.17, 1.21, 1.26]
>>> fee_dict = dict(zip(wd, fees))
然后用简单的 range
构建 correspDay
:
>>> correspDay = list(range(1, 7))
并通过遍历 correspDay
构建 result
,使用 fee_dict
查找费用并在 fee_dict
为空时使用最后一个条目:
>>> result = []
>>> for i in correspDay:
... result.append(fee_dict.get(i) or result[-1])
...
>>> result
[1.44, 1.44, 1.44, 1.17, 1.21, 1.26]
我会把这些值放在字典中,然后遍历一周中的几天 (1-7),检查我们是否已经有了那一天的值。如果您那天确实有值,请将其存储起来,以防第二天没有值。如果没有值,则使用当天的键和最后一笔费用的值在字典中创建一个项目。
最后我对列表进行了排序,但除了使输出更易于阅读之外没有真正的理由这样做
fees_dict = {1: 1.44, 4: 1.17, 5: 1.21, 6: 1.26}
last_fee = 0
for i in range(1, 8):
if i in fees_dict:
last_fee = fees_dict[i]
else:
fees_dict[i] = last_fee
sorted_fees_dict = dict(sorted(fees_dict.items()))
print(sorted_fees_dict)
feeMap
映射days
到fee
需要支付的prev
用于存储之前的值(费用)
#!/usr/bin/env python3.10
wd = [1,4,5,6]
fees = [1.44, 1.17, 1.21, 1.26]
corresDay = list(range(1, 6))
feeMap = dict()
index = 0
for index, day in enumerate(wd):
feeMap[day] = fees[index]
prev = None
for day in range(1, 6):
if day not in feeMap.keys():
feeMap[day] = prev
else:
prev = feeMap[day]
feeMap = sorted(feeMap.items())
print(feeMap)
输出:
$ ./working_days.py
[(1, 1.44), (2, 1.44), (3, 1.44), (4, 1.17), (5, 1.21), (6, 1.26)]
我什么都没做,看到上面的好人,我尽量给你讲清楚。
wd = [1,4,5,6]
fees = [1.44, 1.17, 1.21, 1.26]
fee_dict = dict(zip(wd, fees))
correspDay = list(range(1,8))
result = []
for i in correspDay:
result.append(fee_dict.get(i) or result[-1])
我只是添加这一行以在字典中输出
result = dict(zip(correspDay, result))
输出:
{1: 1.44, 2: 1.44, 3: 1.44, 4: 1.17, 5: 1.21, 6: 1.26, 7: 1.26}