我有这个
import random
a = random.randint(1,100)
b = random.randint(1,100)
c = random.randint(1,100)
d = random.randint(1,100)
e = random.randint(1,100)
print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e
print "The numbers in ascending order:"
list1=[a,b,c,d,e]
list1.sort(key=int)
print a
print b
print c
print d
print e`
但是我无法理解我想要打印新值而不是旧值...
答案 0 :(得分:3)
遍历list1
并打印每个值:
for number in list1:
print number
答案 1 :(得分:2)
对列表进行排序时,您分配的原始变量不会更改,只会更改列表的内容。
您可以从排序列表中重新分配变量:
list1=[a,b,c,d,e]
list1.sort(key=int)
a, b, c, d, e = list1
print a
print b
print c
print d
print e
答案 2 :(得分:1)
您将变量的数量放入列表list1
。如果您对列表进行排序,则不会将a
的值更改为e
。
如果你想打印它们,在列表上排序并打印所有元素,然后对它们进行排序。
for current in list1:
print current
答案 3 :(得分:1)
这有效
import random
a = random.randint(1,100)
b = random.randint(1,100)
c = random.randint(1,100)
d = random.randint(1,100)
e = random.randint(1,100)
print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e
print "The numbers in ascending order:"
list1=[a,b,c,d,e]
list1.sort(key=int)
for item in list1:
print item
答案 4 :(得分:1)
import random
a = random.randint(1,100)
b = random.randint(1,100)
c = random.randint(1,100)
d = random.randint(1,100)
e = random.randint(1,100)
print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e
print "The numbers in ascending order:"
list1=[a,b,c,d,e]
list1.sort(key=int)
for item in list1:
print(str(item) + '\n')
你的观念有问题。是的,a,b,c,d,e是由random.randint()创建的。对list1进行排序时,list1的更改不会更改a,b,c,d,e
。因为在list1中,只有从a,b,c,d,e
内部复制的值,而不是a,b,c,d,e
本身。因此,您无法通过更改list1来更改a,b,c,d,e的值。
答案 5 :(得分:1)
通过创建和排序list1,您可以更改列表的内容,而不是实际变量a,b,c,d和e。如果你想让python打印“新值”,你必须打印list1。
import random
a = random.randint(1,100)
b = random.randint(1,100)
c = random.randint(1,100)
d = random.randint(1,100)
e = random.randint(1,100)
print "The numbers are a:",a,"b:",b,"c:",c,"d:",d,"and e:",e
print "The numbers in ascending order:"
list1=[a,b,c,d,e]
list1.sort(key=int)
for num in list1:
print num