相关疑难解决方法(0)

如何以数字方式对列表进行排序?

我知道这听起来微不足道,但我没有意识到sort()Python 的功能很奇怪.我有一个实际上是字符串形式的"数字"列表,所以我首先将它们转换为int,然后尝试排序.

list1=["1","10","3","22","23","4","2","200"]
for item in list1:
    item=int(item)

list1.sort()
print list1
Run Code Online (Sandbox Code Playgroud)

给我:

['1', '10', '2', '200', '22', '23', '3', '4']
Run Code Online (Sandbox Code Playgroud)

我想要的是

['1','2','3','4','10','22','23','200']
Run Code Online (Sandbox Code Playgroud)

我查看了一些与排序数字集相关的算法,但我发现的算法都涉及排序字母数字集.

我知道这可能是一个毫无疑问的问题,但谷歌和我的教科书没有提供比.sort()功能更多或更少有用的东西.

python sorting

114
推荐指数
7
解决办法
26万
查看次数

如何按python降序排序整数列表

我试图以不同的方式解决这个问题,但没有成功.我一直按升序排序,而不是在打印时按降序排序.

ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
sorted(ListB, key=int, reverse=True)
print sorted(ListB)
Run Code Online (Sandbox Code Playgroud)

python list

7
推荐指数
2
解决办法
3万
查看次数

在Python中对整数列表进行排序

我是一名初学者程序员,正在尝试练习。我想对整数列表进行排序,但是每次我运行代码时,列表都不会排序。我用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 sorting

0
推荐指数
1
解决办法
143
查看次数

当100应该是我的列表中最高时,max()返回99

def get_highs():
    atlTemps = open("tempsAtlanta2015.txt")
    highs = []
    highs = split_data(atlTemps, highs, 2)
    atlTemps.close()
    return highs

def split_data(lst, lst2, num):
    for i in lst:
        data = i.split(",")
        lst2.append(data[num])
    return lst2

def main():
    highs = get_highs()
    print(max(highs))
main()
Run Code Online (Sandbox Code Playgroud)

我将这些功能从我的主程序中取出来进行故障排除,我似乎遇到了使用max()的问题.在文本文档中,我从max()返回的最大数字中提取信息应该是100但是它返回99.我可以索引100,所以我确定100列在包含中.任何帮助将非常感激!

这是文本文件的一部分,信息存储为 [month, day, high, low]

7,29,99,76
7,30,98,76
7,31,96,73
8,1,93,71
8,2,96,68
8,3,98,71
8,4,99,69
8,5,100,71
8,6,90,72
…
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
439
查看次数

标签 统计

python ×4

sorting ×2

list ×1