Python 如何用字典的元素替换列表的元素

Python 如何用字典的元素替换列表的元素,python,Python,我正在尝试使用python创建一个程序,用全名替换一周中几天的简写名称 这是我所做的代码: # Write your format_days function here. def format_days(days): REPLACEMENTS = { 'Mon': 'Monday', 'Tue': 'Tuesday', 'Wed': 'Wednesday', 'Thu': 'Thursday', 'Fri':

我正在尝试使用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'
回溯(最近一次呼叫最后一次):
文件“program.py”,第24行,在
答案=格式(周一、周三、周五)
文件“program.py”,第14行,格式为\u天
天=天。替换(i,替换[i])
AttributeError:“列表”对象没有属性“替换”
我猜这是因为我试图用字典的元素替换列表的元素?我不确定

我还需要从输出中删除所有不是一周中几天(“乐趣”)的元素


先谢谢你!:)

所以你混淆了你谈论的是哪一天、哪一天、哪一天。 同样,通过在列表循环中使用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
编辑以从列表中删除非天数。 最简单的方法是在运行循环并生成答案时实际使用一个新变量。因此,我将引入您的答案变量,但在我们现在运行循环之前

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
def格式\u天(天):
天数={
“星期一”:“星期一”,
“星期二”:“星期二”,
“星期三”:“星期三”,
"周四",,
“星期五”:“星期五”,
“星期六”:“星期六”,
“太阳”:“星期日”,
}

答案=[]#您混淆了
变量。尽量用更清楚的名字来防止这种情况发生!为什么有两行
days=days.replace(day,days[day])
?只是打字错误?第二,与其将答案设置为天,然后返回天,为什么不返回天呢?谢谢!但是你知道如何从输出中删除“Fun”吗?@coder1234编辑了我的答案,删除了《星期字典》中找不到的任何单词。@coder1234我刚刚注意到我有一个复制和粘贴错误。我更新了答案,但就在一秒钟前,在编辑中,我返回了几天,当时我打算返回答案
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
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