我试过这个,但问题是我不知道如何让程序将每个元素与列表进行比较以确定其顺序
a= [1,4,8,2,210,3]
def sorting(L):
for i in L:
if i[0]<:
print i[0]
sorting(a)
答案 0 :(得分:0)
尝试创建一个新变量来存储临时数据并检查if
List[ Map(term -> check), Map(term -> base) ]
可能第二次循环再次检查是否全部按顺序
答案 1 :(得分:0)
这是你的技术:
a = [1,4,8,2,210,3]
newlist = []
while a:
maximum = a[0]
for x in a:
if x > maximum:
maximum = x
newlist.append(maximum)
a.remove(maximum)
print (newlist)
#[210, 8, 4, 3, 2, 1]
如果您希望它降序,代码会继续从a中移除最小值并将其放入新列表中:
a = [1,4,8,2,210,3]
def ascending(data):
newlist = []
while data:
minimum = data[0]
for x in data:
if x < minimum:
minimum = x
newlist.append(minimum)
data.remove(minimum)
return newlist
def descending(data):
newlist = []
while data:
maximum = data[0]
for x in data:
if x > maximum:
maximum = x
newlist.append(maximum)
data.remove(maximum)
return newlist
a = ascending(a)
print ('Sorted in ascending order: ',a)
a = descending(a)
print ('Sorted in descending order: ',a)
如果你想要它在不同的功能中:
def _sort(data,t):
newlist = []
if t == ('a'):
while data:
minimum = min(data)
newlist.append(minimum)
data.remove(minimum)
elif t == ('d'):
while data:
maximum = max(data)
newlist.append(maximum)
data.remove(maximum)
return newlist
def program():
a=[]
while True:
print ('enter values to sort, type done when done')
v=input(' >').lower().strip()
if v == ('done'):
break
try:
v = float(v)
a.append(v)
print ('numbers so far: ',', '.join(str(x) for x in a))
except:
print ('enter numerical value')
continue
while True:
print ('enter "a" for sort ascending and "d" for sort descending')
print ('enter "start" to go back to start')
t=input(' >').lower().strip()
if t == ('start'):
program()
return
if not t == ('a') and not t == ('d'):
continue
a = _sort(a,t)
print ('my sorted numbers: ',', '.join(str(x) for x in a))
program()
简单程序使用min()和max()而不是逐个浏览列表
<Begin>
<tag1>a</tag1>
<tag2>b</tag2>
</Begin>