我正在开发一个程序,它接受一个列表并从最高到最低排序[是的,我知道我可以使用.sort()].该计划如下:
UnList = raw_input("Enter a list of numbers seperated by commas") #collects the users list
List = UnList.split(",") #Turns the users numbers into a list
for x in range(len(List)): #number of time to sort
Switcher = 0 #the switcher (also will reset it later)
for z in range(len(List)-1): #number of time the switcher runs
if List[Switcher] > List[(Switcher + 1)]: #if one number is bigger than the other
List[Switcher],List[(Switcher+1)] = List[(Switcher+1)],List[Switcher] #this switches those 2 numbers
Switcher += 1 #adds …Run Code Online (Sandbox Code Playgroud) 我有以下程序,它成功创建了一个充满零的 10x20 数组:
array2 = []
array2=[[0 for j in range(10)] for i in range(20)]
print array2
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用 for 循环和 while 循环执行相同的操作:
for j in range(20):
for i in range(10):
array1.append(0)
print array1
array3 = []
count = 0
while count <= 20:
count += 1
while count <= 10:
array3.append(0)
count += 1
print array3
Run Code Online (Sandbox Code Playgroud)
我觉得我走在正确的轨道上,但我似乎无法用这些循环创建同样的东西。我如何调整这些循环才能产生与第一个循环相同的效果?谢谢。