我创建了一个sqlite数据库,它有一个存储温度值的表.温度值首次以升序写入数据库.然后我将数据库中的温度值读入列表,然后将该列表添加到组合框中以选择温度 - 工作正常.
结果列表是:
templist = ['25', '50', '100', '150', '200', '250', '300'].
Run Code Online (Sandbox Code Playgroud)
然后我向数据库添加一个新的温度值,比如'33'.
它被附加到表的末尾.如果我现在读取温度,列表将变为:
['25', '50', '100', '150', '200', '250', '300', '33'].
Run Code Online (Sandbox Code Playgroud)
如果我做templist.sort()或sorted(templist),最终的结果是
['150', '200', '25', '250', '300', '33', '50']
Run Code Online (Sandbox Code Playgroud)
是否有任何简单的方法按升序对列表进行排序,以便我得到:
['25', '33', '50', '100', '150', '200', '250', '300']
Run Code Online (Sandbox Code Playgroud) 我正在尝试对包含数字的字符串列表进行排序
a = ["1099.0","9049.0"]
a.sort()
a
['1099.0', '9049.0']
b = ["949.0","1099.0"]
b.sort()
b
['1099.0', '949.0']
a
['1099.0', '9049.0']
Run Code Online (Sandbox Code Playgroud)
但是列表b是排序而不是列表a
如何对包含浮点值的python列表进行排序,
list1 = [1, 1.10, 1.11, 1.1, 1.2]
Run Code Online (Sandbox Code Playgroud)
要么
list1 = ['1', '1.10', '1.11', '1.1', '1.2']
Run Code Online (Sandbox Code Playgroud)
预期的结果是
list_val = ['1', **'1.1', '1.2'**, '1.10', '1.11']
Run Code Online (Sandbox Code Playgroud)
但返回的结果使用sort()方法返回
[1, 1.1000000000000001, 1.1000000000000001, 1.1100000000000001, 1.2]
Run Code Online (Sandbox Code Playgroud)
要么
['1', '1.1', '1.10', '1.11', '1.2'].
Run Code Online (Sandbox Code Playgroud)
但是,在这里1.2应该进来之间1.1和1.10.
我制作了一个程序来计算列表中的第二大数字。输入是一个被切成列表的字符串。这是代码
score1 = input()
score = score1.split()
score.sort()
maximum = max(score)
count = score.count(maximum)
for i in range(0,count):
score.pop()
print(max(score))
Run Code Online (Sandbox Code Playgroud)
它对正数工作正常,但如果列表包含负数,我的程序无法产生正确的答案。
对于输入
-7 -7 -7 -7 -6
输出是
-6而不是-7
有什么办法可以改善吗?
我是一名初学者程序员,正在尝试练习。我想对整数列表进行排序,但是每次我运行代码时,列表都不会排序。我用sorted()或.sort()以几种不同的方式尝试过,但是似乎没有任何帮助。
def main():
_list1_ = []
_list2_ = []
print("Enter random numbers and enter Q to quit: ")
userInput1 = input("")
while userInput1.upper() != "Q":
_list1_.append(int(userInput1))
userInput1 = input("")
print("Enter random numbers and enter Q to quit:")
userInput2 = input("")
while userInput2.upper() != "Q":
_list2_.append(int(userInput2))
userInput2 = input("")
sorted(_list1_)
sorted(_list2_)
print(_list1_)
main()
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有以下已排序的元组列表,在python中使用"sorted":
L = [("1","blaabal"),
("1.2","bbalab"),
("10","ejej"),
("11.1","aaua"),
("12.1","ehjej"),
("12.2 (c)", "ekeke"),
("12.2 (d)", "qwerty"),
("2.1","baala"),
("3","yuio"),
("4","poku"),
("5.2","qsdfg")]
Run Code Online (Sandbox Code Playgroud)
我的问题是你可以注意到,起初它很好,虽然在"12.2(d)"之后列表重启"2.1",我不知道如何解决这个问题.
谢谢
我的代码是用python 3编写的,它的目的是打印出回文.它应该遍历2个3位数字的所有回文产品,如下所示:
mylist=[]
for i in range(999,99,-1):
for x in range(999, i-1, -1,):
number=i*x
number=str(number)
if number==number[::-1]:
#print(number)
mylist.append(number)
mylist=mylist.sort(reverse=True)
print(mylist)
Run Code Online (Sandbox Code Playgroud)
请注意已注释的打印件.当它仍然存在时,应该打印出的所有回文都出来了.当我运行我的代码而没有print语句时,控制台只打印出"无".
据我所知,我的逻辑是有序的,为什么会发生这种情况呢?编辑:此外,当我按相反顺序对列表进行排序时,首先是99999.我认为这是因为python看着连续的9并且认为它是最大的.但是,有没有一种简单的方法来获得实际最大的数字?
我试图对实际上是整数的字符串列表进行排序,但我没有得到正确的排序值。我如何以根据字符串的整数值对其进行排序的方式对其进行排序?
a = ['10', '1', '3', '2', '5', '4']
print(sorted(a))
Run Code Online (Sandbox Code Playgroud)
输出:
['1', '10', '2', '3', '4', '5']
Run Code Online (Sandbox Code Playgroud)
想要的输出:
['1', '2', '3', '4', '5', '10']
Run Code Online (Sandbox Code Playgroud)