Lua中如何获取表中的最大整数?

发布于 2024-10-20 11:54:57 字数 23 浏览 2 评论 0原文

Lua中如何获取表中的最大整数?

How do I get the highest integer in a table in Lua?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

疯了 2024-10-27 11:54:58
loltable = {1, 2, 3, 4, 1, 2, 37, 1, 0}
table.sort(loltable)
print(loltable[#loltable])
loltable = {1, 2, 3, 4, 1, 2, 37, 1, 0}
table.sort(loltable)
print(loltable[#loltable])
薄暮涼年 2024-10-27 11:54:58

ponzao 的另一个答案很好,但是为了更具体地回答你的问题,如果你只想获得最大的数字(而不是索引),我通常会这样做:

function max(a)
  local values = {}

  for k,v in pairs(a) do
    values[#values+1] = v
  end
  table.sort(values) -- automatically sorts lowest to highest

  return values[#values]
end

print(max({1, 2, 3, 4, 1, 2, 37, 1, 0})) --> 37

更进一步,只包含数组部分表并过滤掉仅数字值(以防止错误),您可以添加一些类型检查:

function max(a)
  local values = {}

  for k,v in pairs(a) do
    if type(k) == "number" and type(v) == "number" then
      values[#values+1] = v
    end
  end
  table.sort(values) -- automatically sorts lowest to highest

  return values[#values]
end

print(max({1, 2, 3, 4, 1, 2, 37, 1, 0})) --> 37

逻辑如下:

  1. 创建一个空表(数组)
  2. 通过对迭代所有键(ipairs() 首先停止nil,使用带有 # 的 for 循环也是如此)
  3. 将每个值添加到数组中(在验证第二个代码块中的类型之后)
  4. 将数组从最高到最低排序
  5. 返回最后一个元素的值(排序后,它将位于结尾)。

我知道这是一个老问题,所以OP可能不再需要这个了,但是这个页面目前在Google上排名很高,所以希望这可以帮助其他偶然发现这个页面的人。

The other answer by ponzao is good, but to answer your question more specifically, if you just want to get the highest number (and not the index as well), I usually do this:

function max(a)
  local values = {}

  for k,v in pairs(a) do
    values[#values+1] = v
  end
  table.sort(values) -- automatically sorts lowest to highest

  return values[#values]
end

print(max({1, 2, 3, 4, 1, 2, 37, 1, 0})) --> 37

To take it a step further and include only the array part of the table and filter out for only number values (to prevent errors), you can add some type checks:

function max(a)
  local values = {}

  for k,v in pairs(a) do
    if type(k) == "number" and type(v) == "number" then
      values[#values+1] = v
    end
  end
  table.sort(values) -- automatically sorts lowest to highest

  return values[#values]
end

print(max({1, 2, 3, 4, 1, 2, 37, 1, 0})) --> 37

The logic is as follows:

  1. Create an empty table (array)
  2. Iterate over all keys via pairs (ipairs() stops at first nil, and so does using a for loop with #)
  3. Add each value to the array (after verifying type in second code block)
  4. Sort the array from highest to lowest
  5. Return the value of the last element (after sort, it will be at the end).

I know this is an old question so the OP probably doesn't need this anymore, but this page currently ranks high on Google so hopefully this can help someone else who stumbles upon this page.

痴情换悲伤 2024-10-27 11:54:58

如果您的表是一个数组(仅数字索引 >0),则使用 table.sort 并采用 t[#t] (但是,这会更改表)。

其他方法就像这样

m={0,0}
for k,v in pairs(t) do
    if m[1]<v then
         m[1]=v
         m[2]=k
    end
end
print("Maximum of "..m[1].." at index "..m[2])

If your table is an array (only numeric indices >0) then use table.sort and take t[#t] (however, this changes the table).

Other approach would be like this

m={0,0}
for k,v in pairs(t) do
    if m[1]<v then
         m[1]=v
         m[2]=k
    end
end
print("Maximum of "..m[1].." at index "..m[2])
Hello爱情风 2024-10-27 11:54:58

Lua 附带了一个函数来获取最大的整数键,如果这就是你想要的......

table.maxn

Lua comes with a function to get the highest integer key if thats what you wanted...

table.maxn
臻嫒无言 2024-10-27 11:54:58

您忘记在解压之前添加表格,

 local save = math.max(table.unpack({1, 2, 3, 4, 5}))
    print(save)

如果您想找到最大或最小数量,这应该可以工作

you are forgetting to add table before unpack like this

 local save = math.max(table.unpack({1, 2, 3, 4, 5}))
    print(save)

this should work if you want to find the max or min number

未蓝澄海的烟 2024-10-27 11:54:57

在Lua 5.1及更早版本中,您可以使用

math.max(unpack({1, 2, 3, 4, 5}))

这受到Lua堆栈大小的限制;在 PUC Lua 5.1 上,该值的最大值可达 ca。 8000 个数字(如果堆栈空闲,即尚未进行函数调用)。

从 Lua 5.2 开始,您必须使用 table.unpack 而不是 unpack (它已被移动)。堆栈大小已显着增加,因此该方法可用于查找数十万个数字的最大值。

In Lua 5.1 and earlier, you can use

math.max(unpack({1, 2, 3, 4, 5}))

This is limited by the size of the Lua stack; on PUC Lua 5.1, this gets the maximum of up to ca. 8 thousand numbers (if the stack is free, i.e. no function calls have been made yet).

Since Lua 5.2, you must use table.unpack instead of unpack (it has been moved). The stack size has been dramatically increased such that this method can be used to find the maximum of hundreds of thousands of numbers.

余生一个溪 2024-10-27 11:54:57

用于实现此目的的通用函数:

function max(t, fn)
    if #t == 0 then return nil, nil end
    local key, value = 1, t[1]
    for i = 2, #t do
        if fn(value, t[i]) then
            key, value = i, t[i]
        end
    end
    return key, value
end

使用方式如下:

print(max({1,2,3,4,1,2,37,1,0}, function(a,b) return a < b end)) --> 7 37

A generic function for achieving this:

function max(t, fn)
    if #t == 0 then return nil, nil end
    local key, value = 1, t[1]
    for i = 2, #t do
        if fn(value, t[i]) then
            key, value = i, t[i]
        end
    end
    return key, value
end

Which is used like this:

print(max({1,2,3,4,1,2,37,1,0}, function(a,b) return a < b end)) --> 7 37
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文