• 首页
• Python
• Python：for循环只运行一次，Project Euler

# Python：for循环只运行一次，Project Euler [英] Python: &#39;For&#39;loop only runs once, Project Euler

### 问题描述

1. 列出~3k个素数。
2. 返回由原始列表中每个项目的所有旋转列表组成的新列表。

``````def rotations(lst):
newlist = []
for i in lst:                                              # Take each int item in list.
s = [int(j) for j in str(i)]                           # Turn each item into a list of digit strings
num = ([s[k:]+s[:-len(s)+k] for k in range(len(s))])   # Generate list of rotations of strings
tmplst = []
for l in num:                                          # For each string rotation
tmplst.append(int(''.join(map(str,l))))            # Turn it into an integer, add that int to tmplst
newlist.append(tmplst)                                 # Add each tmplist to 'newlist'
return newlist
``````

``````[[123, 231, 312]]
``````

``````[[123, 231, 312],[456,564,645]]
``````

### 推荐答案

``````'    def rotations(lst):'
'            newlist = []'
'            for i in lst:                                              # Take each int item in list. '
'            	    s = [int(j) for j in str(i)]                           # Turn each item into a list of digit strings'
'            	    num = ([s[k:]+s[:-len(s)+k] for k in range(len(s))])   # Generate list of rotations of strings '
'            	    tmplst = [] '
'            	    for l in num:                                          # For each string rotation'
"                	        tmplst.append(int(''.join(map(str,l))))            # Turn it into an integer, add that int to tmplst"
"                	    newlist.append(tmplst)                                 # Add each tmplist to 'newlist'"
'                    return newlist'
'        '
``````

``````python -tt your_program_name.py
``````