【问题标题】:My output format is not correct can someone help me?我的输出格式不正确有人可以帮助我吗?
【发布时间】:2016-07-07 21:34:51
【问题描述】:

给定一个未排序的整数列表,按顺序输出整数。

示例输入

100 63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33

样本输出

1 1 3 3 6 8 9 9 10 12 13 16 16 18 20 21 21 22 23 24 25 25 25 27 27 30 30 32 32 32 33 33 33 34 39 39 40 40 41 42 43 44 44 46 46 48 50 53 56 56 57 59 60 61 63 65 67 67 68 69 69 69 70 70 73 73 74 75 75 76 78 78 79 79 80 81 81 82 83 83 84 85 86 86 87 87 89 89 89 90 90 91 92 94 95 96 98 98 99 99

我的输出

11 33 6 8 99 10 12 13 1616 18 20 2121 22 23 24 252525 2727 3030 323232 333333 34 3939 4040 41 42 43 4444 4646 48 50 53 5656 57 59 60 61 63 65 6767 68 696969 7070 7373 74 7575 76 7878 7979 80 8181 82 8383 84 85 8686 8787 898989 9090 91 92 94 95 96 9898 9999

我的代码:

a=int(input())
b=input()
b1=b.split(" ")
arr=list(map(int,b1))
ans=[]
for i in range(0,100,1):
    #print(arr.count(i),end=' ')
    ans.append(arr.count(i))
for i in range(0,len(ans)):
    if(i==0):
        continue
    else:
        print(str(i)*ans[i],end=' ')

【问题讨论】:

  • 为什么不使用sort()?
  • @Idos 我想这是一个家庭作业问题
  • 不,这是挑战问题,但我失去了挑战,所以我想知道如何解决这个问题。

标签: python arrays algorithm list data-structures


【解决方案1】:

如果您必须这样做,请尝试:

print((str(i) + ' ') * ans[i], end=' ')

【讨论】:

    【解决方案2】:

    使用 sorted with key:

    string = "100 63 25 73 1 98 73 56 84 ... "
    
    sorted_string = " ".join(sorted(string.split(), key=lambda x: int(x)))
    

    【讨论】:

    • 我是正则表达式的新手,你能告诉我一个可以阅读python中的lambda表达式的地方吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2021-03-15
    • 2018-04-16
    • 2015-03-06
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多