从另一个列表中创建一个新列表,并通过重复填充缺失值

发布于 2025-01-24 04:26:47 字数 283 浏览 0 评论 0原文

我有两个列表,一个包含工作日,另一个包含与工作日相对应的费用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

马蹄踏│碎落叶 2025-01-31 04:26:48

我会将这些值放在字典中,然后循环循环到一周的日子(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)

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

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)
情魔剑神 2025-01-31 04:26:48

feemap地图 费用必须付款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)]

feeMap maps days to fee that has to be paid prev is used to store the previous value(fee).

#!/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)

output:

$ ./working_days.py 
[(1, 1.44), (2, 1.44), (3, 1.44), (4, 1.17), (5, 1.21), (6, 1.26)]
脱离于你 2025-01-31 04:26:48

我什么都没做,我看到上面的好人,我试图为您更加清楚。

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}

I didn't do anything I saw the good people above and I try to make it more clear for you.

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])

I just add this line to make the output in dictionary

result = dict(zip(correspDay, result))

output:

{1: 1.44, 2: 1.44, 3: 1.44, 4: 1.17, 5: 1.21, 6: 1.26, 7: 1.26}
心碎无痕… 2025-01-31 04:26:47

我首先要构建一个字典,以便每天查找费用:

>>> wd = [1,4,5,6]
>>> fees = [1.44, 1.17, 1.21, 1.26]
>>> fee_dict = dict(zip(wd, fees))

然后用简单的range> range构建correspday

>>> correspDay = list(range(1, 7))

并构建result通过迭代conerespday,使用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]

I'd start by building a dictionary to be able to look up fees by day:

>>> wd = [1,4,5,6]
>>> fees = [1.44, 1.17, 1.21, 1.26]
>>> fee_dict = dict(zip(wd, fees))

Then build correspDay with a simple range:

>>> correspDay = list(range(1, 7))

and build result by iterating over correspDay, using fee_dict to look up the fees and using the last entry when fee_dict comes up empty:

>>> 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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文