Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Python 中,我有一个列表:[11,42,122,1919, 17, 4]并且想要计算它的模数(例如 10)以将结果作为列表给出:[1,2,2,9,7,4]
[11,42,122,1919, 17, 4]
[1,2,2,9,7,4]
即使这是一个家庭作业问题,您也可以这样做:
[a%10 for a in l]
您也可以在没有列表理解的情况下执行此操作,如下所示:
num_list = [11,42,122,1919, 17, 4] result = [] for num in num_list: result.append(num%10) print(result) # Output [1, 2, 2, 9, 7, 4]
如果您是 python 新手,那么这种带有代码的结构非常常用。