首页 > 解决方案 > 如何用字典元素替换列表元素

问题描述

我正在尝试使用 python 创建一个程序,用它们的全名替换一周中几天的简写名称。

这是我所做的代码:

# Write your format_days function here.
def format_days(days):
    REPLACEMENTS = {
        'Mon': 'Monday',
        'Tue': 'Tuesday',
        'Wed': 'Wednesday',
        'Thu': 'Thursday',
        'Fri': 'Friday',
        'Sat': 'Saturday',
        'Sun': 'Sunday'
   }

   for i in REPLACEMENTS:
       if i in days:
          days = days.replace(i,REPLACEMENTS[i])

  answer = days

  return days

if __name__ == '__main__':
    # You can use this to test your function.
    # Any code inside this `if` statement will be ignored by the automarker.

    # Run your `format_days` function with the first example in the question.
    answer = format_days(['Mon', 'Wed', 'Fri'])
    print(answer)

    # Run your `format_days` function with the second example in the question.
    answer = format_days(['Sat', 'Fun', 'Tue', 'Thu'])
    print(answer)

这就是我希望代码执行的操作:

['Monday', 'Wednesday', 'Friday']
['Saturday', 'Tuesday', 'Thursday']

这就是我的代码所说的:

Traceback (most recent call last):
  File "program.py", line 24, in <module>
    answer = format_days(['Mon', 'Wed', 'Fri'])
  File "program.py", line 14, in format_days
    days = days.replace(i,REPLACEMENTS[i])
AttributeError: 'list' object has no attribute 'replace'

我猜这是因为我试图用字典的元素替换列表的元素?我不确定。

我还需要从输出中删除所有不是星期几('Fun')的元素。

先感谢您!:)

标签: python

解决方案


因此,您正在混淆您正在谈论的 DAYS、days 和 day。此外,通过在列表循环中使用 for 变量,您正在创建一个变量并丢失您尝试更新的索引。

像这样更新你的循环

for index in range(len(days)): # <-- loop through range to get the index
   if days[index] in DAYS: # <--- the index is accessed to get the key in the dictionary
      days[index] = DAYS[days[index]] # <-- the reason we need the index is to assign the answer

The other problem I see is that you assign days to answer and then immediately return answer. This means you can skip that step and just return days instead.

# answer = days
# return answer
return days # <-- no need for an extra variable.

Final Code of the function.

def format_days(days):
    DAYS = {
        'Mon': 'Monday',
        'Tue': 'Tuesday',
        'Wed': 'Wednesday',
        'Thu': 'Thursday',
        'Fri': 'Friday',
        'Sat': 'Saturday',
        'Sun': 'Sunday',
    }

    for index in range(len(days)):
       if days[index] in DAYS:
          days[index] = DAYS[days[index]]

    return days

Edit to remove non days from list.

The easiest way to do this would be to actually use a new variable when running the loop and building the answer. So I will introduce your answer variable back in, but before we run the loop now.

def format_days(days):
    DAYS = {
        'Mon': 'Monday',
        'Tue': 'Tuesday',
        'Wed': 'Wednesday',
        'Thu': 'Thursday',
        'Fri': 'Friday',
        'Sat': 'Saturday',
        'Sun': 'Sunday',
    }

    answer = [] # <-- new variable

    for index in range(len(days)):
       if days[index] in DAYS:
          answer.append(DAYS[days[index]]) # <-- appending only the found items

    return answer

Now if something in the list is not in the dictionary, it will just be skipped.


推荐阅读