【问题标题】:Is there any way to make this list sorter shorter? [duplicate]有没有办法让这个列表排序器更短? [复制]
【发布时间】:2019-07-08 06:57:31
【问题描述】:

我想输入 5 个名称,然后手动对它们进行排序。

还有比这更短的解决方案吗?

def swap(x,y):
  L[x],L[y]=L[y],L[x]
  L = input("Enter names separated by space: ")
  L = L.split(" ")
    for x in range(len(L)):
       for y in range(len(L)-1):
          if L[y] > L[y+1]:
            swap(y,y+1)
print(L)

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    在python中你可以这样做:

    L =  input("Enter names separated by space: ")
    L =   L.split(",")
    # sort the list
    L.sort()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-07
      • 2021-02-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      相关资源
      最近更新 更多