python 中的列表列表?

发布于 2024-08-05 20:47:01 字数 231 浏览 3 评论 0原文

我需要一个好的函数来在 python 中执行此操作。

def foo(n):
    # do somthing
    return list_of_lists

>> foo(6)
   [[1],
    [2,3],
    [4,5,6]]
>> foot(10)
    [[1],
    [2,3],
    [4,5,6]
    [7,8,9,10]]

I need a good function to do this in python.

def foo(n):
    # do somthing
    return list_of_lists

>> foo(6)
   [[1],
    [2,3],
    [4,5,6]]
>> foot(10)
    [[1],
    [2,3],
    [4,5,6]
    [7,8,9,10]]

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

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

发布评论

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

评论(6

许久 2024-08-12 20:47:01
def foo(n):
  lol = [ [] ]
  i = 1
  for x in range(n):
    if len(lol[-1]) >= i:
      i += 1
      lol.append([])
    lol[-1].append(x)
  return lol
def foo(n):
  lol = [ [] ]
  i = 1
  for x in range(n):
    if len(lol[-1]) >= i:
      i += 1
      lol.append([])
    lol[-1].append(x)
  return lol
沙沙粒小 2024-08-12 20:47:01
def foo(n):
    i = 1
    while i <= n:
        last = int(i * 1.5 + 1)
        yield range(i, last)
        i = last

list(foo(3))

当您使用无效的数字(例如 9)作为 n 时,您期望什么行为?

def foo(n):
    i = 1
    while i <= n:
        last = int(i * 1.5 + 1)
        yield range(i, last)
        i = last

list(foo(3))

What behavior do you expect when you use a number for n that doesn't work, like 9?

九八野马 2024-08-12 20:47:01

改编自gs的答案,但没有神秘的“1.5”。

def foo(n):
    i = c = 1
    while i <= n:
        yield range(i, i + c)
        i += c
        c += 1

list(foo(10))

Adapted from gs's answer but without the mysterious "1.5".

def foo(n):
    i = c = 1
    while i <= n:
        yield range(i, i + c)
        i += c
        c += 1

list(foo(10))
天邊彩虹 2024-08-12 20:47:01

这可能不适合列表理解,但我不在乎!

from math import ceil, sqrt, max

def tri(n):
    return n*(n+1) // 2

def irt(x):
    return int(ceil((-1 + sqrt(1 + 8*x)) / 2))

def foo(n):
    return [list(range(tri(i)+1, min(tri(i+1)+1, n+1))) for i in range(irt(n))]

This is probably not a case where list comprehensions are appropriate, but I don't care!

from math import ceil, sqrt, max

def tri(n):
    return n*(n+1) // 2

def irt(x):
    return int(ceil((-1 + sqrt(1 + 8*x)) / 2))

def foo(n):
    return [list(range(tri(i)+1, min(tri(i+1)+1, n+1))) for i in range(irt(n))]
梦里南柯 2024-08-12 20:47:01

这是我的 python 高尔夫条目:

>>> def foo(n):
...     def lower(i): return 1 + (i*(i-1)) // 2
...     def upper(i): return i + lower(i)
...     import math
...     x = (math.sqrt(1 + 8*n) - 1) // 2
...     return [list(range(lower(i), upper(i))) for i in  range(1, x+1)]
...
>>>
>>> for i in [1,3,6,10,15]:
...     print i, foo(i)
...
1 [[1]]
3 [[1], [2, 3]]
6 [[1], [2, 3], [4, 5, 6]]
10 [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]
15 [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15]]
>>>

x 的计算依赖于具有正根的二次方程的解

0 = y*y + y - 2*n

Here's my python golf entry:

>>> def foo(n):
...     def lower(i): return 1 + (i*(i-1)) // 2
...     def upper(i): return i + lower(i)
...     import math
...     x = (math.sqrt(1 + 8*n) - 1) // 2
...     return [list(range(lower(i), upper(i))) for i in  range(1, x+1)]
...
>>>
>>> for i in [1,3,6,10,15]:
...     print i, foo(i)
...
1 [[1]]
3 [[1], [2, 3]]
6 [[1], [2, 3], [4, 5, 6]]
10 [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]
15 [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15]]
>>>

The calculation of x relies on solution of the quadratic equation with positive roots for

0 = y*y + y - 2*n
霞映澄塘 2024-08-12 20:47:01

还有一个,只是为了好玩:(

def lol(n):
    entries = range(1,n+1)
    i, out = 1, []
    while len(entries) > i:
        out.append( [entries.pop(0) for x in xrange(i)] )
        i += 1
    return out + [entries]

这不依赖于具有数字 1..n 的基础列表)

One more, just for fun:

def lol(n):
    entries = range(1,n+1)
    i, out = 1, []
    while len(entries) > i:
        out.append( [entries.pop(0) for x in xrange(i)] )
        i += 1
    return out + [entries]

(This doesn't rely on the underlying list having the numbers 1..n)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文