简体   繁体   English

关于 python 列表中错误的快速提问 * 常量,例如 [a, b, c, d, e ] * x

[英]Quick question on error in python list * a constant e.g. [a , b, c, d , e ] * x

Quick question on a problem was doing previously.关于一个问题的快速提问是以前做过的。 This question gives you a list of days from Mon - Sun, and you are supposed to create a list given 'n' (no of days ahead).这个问题给你一个从周一到周日的天数列表,你应该创建一个给定“n”(提前几天)的列表。 So if n = 2, Monday will be Wednesday etc.因此,如果 n = 2,则星期一将是星期三,依此类推。

I tried this in python, and came up with this:我在 python 中尝试了这个,并得出了这个:

def after_n_days(days, n):
    day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] * (int(n/7)+1)
    return [day[day.index(i) + n] for i in days]

after_n_days(["Monday","Friday","Wednesday"], 15)

However, it seems i get an error when i multiply the list 'day' by (int(n/7) + 1 when n= 15 and int(n/7) + 1 =3. It works when I simply multiply the list by 3 though, so I'm wondering what's the issue here?但是,当我将列表“day”乘以 (int(n/7) + 1 when n= 15 and int(n/7) + 1 =3.虽然是 3 点,所以我想知道这里有什么问题?

Many thanks everyone!!非常感谢大家!!

Here is the error message这是错误信息

IndexError                                Traceback (most recent call last)
<ipython-input-124-481771160081> in <module>
      6 
      7 
----> 8 after_n_days(["Thursday", "Monday"], 4)
      9 after_n_days(["Monday","Friday","Wednesday"], 15)

<ipython-input-124-481771160081> in after_n_days(days, n)
      3 def after_n_days(days, n):
      4     day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] * (int(n/7)+1)
----> 5     return [day[day.index(i) + n] for i in days]
      6 
      7 

<ipython-input-124-481771160081> in <listcomp>(.0)
      3 def after_n_days(days, n):
      4     day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] * (int(n/7)+1)
----> 5     return [day[day.index(i) + n] for i in days]
      6 
      7 

IndexError: list index out of range IndexError:列表索引超出范围

The problem is that you don't expand the list far enough.问题是您没有将列表扩展得足够远。 Multiplying a list by 1 is just the list.列表乘以 1 只是列表。 Any day, n combination that goes off the end of the list fails.任何一天,超出列表末尾的 n 个组合都会失败。 For example,例如,

after_n_days(["Thursday", "Monday"], 4)
after_n_days(["Thursday", "Monday"], 7 + 4)
etc...

You could just make the expanded list bigger你可以让扩展列表更大

day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
        'Saturday', 'Sunday'] * (int(n/7)+2)

Or better, use the modulo operator to wrap the index instead.或者更好的是,使用模运算符来包装索引。

def after_n_days(days, n):
    print(int(n/7)+1)
    day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
            'Saturday', 'Sunday']
    return [day[(day.index(i) + n) % len(day)] for i in days]

after_n_days(["Thursday", "Monday"], 4)
after_n_days(["Monday","Friday","Wednesday"], 15)
after_n_days(["Monday","Friday","Wednesday"], 18)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串(例如'A')引用到更大列表的索引(例如['A','B','C','D',...])? - How can I reference a string (e.g. 'A') to the index of a larger list (e.g. ['A', 'B', 'C', 'D', ...])? 在Python中创建不带变量“ x in”(例如,范围)的生成器表达式或列表推导 - Creating a generator expression or list comprehension without variable “x in” (e.g. for range) in Python Python中的3D几何拓扑(例如,交集) - 3D geometric topology (e.g. intersection) in Python Python:如何创建一个函数? 例如 f(x) = ax^2 - Python: How to create a function? e.g. f(x) = ax^2 在 Python 中将国际象棋网格(A1、B2、D5 等)转换为元组坐标(例如 (0,0)、(1,1)) - Convert chess grid (A1, B2, D5, etc.) to tuple coordinates (e.g. (0,0), (1,1) in Python 为什么分配给空列表(例如 [] = &quot;&quot;)不是错误? - Why isn't assigning to an empty list (e.g. [] = "") an error? 找到一维数组/列表元素之间差异的函数,例如x [n] -x [n-1],x [n-2] -x [n-1]其中n是数组/列表的大小 - A function that finds the difference between elements of a 1D array/list e.g. x[n]-x[n-1], x[n-2]-x[n-1] where n is the size of the array/list 索引操作失败时如何使用指定的值(例如b [x] [y])? - How to use specified value when indexing operation fails (e.g. b[x][y])? 用python进行窗口式写入,例如写入NetCDF - Windowed writes in python, e.g. to NetCDF Abaqus python 脚本,如何为具有多个集成点的元素(例如 C3D20R)“添加数据” - Abaqus python script, how to 'addData' for elements with multiple integration points (e.g. C3D20R)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM