如何创建整数数组?

时间:2017-04-30 07:31:27

标签: python python-3.x

我试过了列表

new=[]
new=input()
print(new)

默认给我一个字符串。如何找到第二大整数?我尝试过在本网站上找到的其他答案,但对我没有任何帮助。

3 个答案:

答案 0 :(得分:2)

当你想比较整数时不要比较字符串!你需要将这些字符串转换为整数:

jQuery(function() {
  jQuery("#itemDetails").hide();
  jQuery('#regcode').on('keydown', function() {
    if (jQuery('#regCodeTarget').text() == "Correct Key") {
      jQuery("#itemDetails").show();
    }
  });
});

(比较字符串时div#itemDetailsin_str = '243 3443 6543 43 546' ints = [int(i) for i in in_str.split()] ints.sort(reverse=True) print(ints[1]) # 3443

答案 1 :(得分:1)

你可以这样做:

new = []
new = list(map(int, input().split(' ')))
new.sort()
print(new[-2])

它假定您的输入值由''分隔。例如:

6 5 4 3 7 8

map()将您的字符串映射为整数,这将返回map-object。然后,您可以在其上调用list()来获取列表。排序后,您可以通过访问[-2]获得倒数第二个元素。

答案 2 :(得分:1)

你可以使用heapq模块和map,先改为int,然后得到第二大;

new = heapq.nlargest(2, map(int, new))