python range() 有重复项?

发布于 2024-12-01 02:18:04 字数 575 浏览 1 评论 0原文

每个人都知道可以使用 range 获取数字列表,如下所示;:

>>> list(range(5))
[0, 1, 2, 3, 4]

如果您想要每个数字的 3 个副本,则可以使用:

>>> list(range(5)) * 3
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]

但是有没有一种简单的方法使用 range< /code> 像这样重复复制?

[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

示例:

sorted(list(range(5)) * 3)   # has unnecessary n * log(n) complexity
[x//3 for x in range(3*5)]   # O(n), but division seems unnecessarily complicated

Everybody knows that a list of numbers can be obtained with range like this;:

>>> list(range(5))
[0, 1, 2, 3, 4]

If you want, say, 3 copies of each number you could use:

>>> list(range(5)) * 3
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]

But is there an easy way using range to repeat copies like this instead?

[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

Examples:

sorted(list(range(5)) * 3)   # has unnecessary n * log(n) complexity
[x//3 for x in range(3*5)]   # O(n), but division seems unnecessarily complicated

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

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

发布评论

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

评论(8

白鸥掠海 2024-12-08 02:18:04

您可以这样做:

>>> [i for i in range(5) for _ in range(3)]
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

range(3) 部分应替换为您的重复次数...

顺便说一句,您应该使用生成器


只是为了使其更清楚, _ 是您不关心的变量名称(允许任何名称)。

此列表理解使用嵌套的 for 循环,就像这样:

for i in range(5):
    for j in range(3):
        #your code here

You can do:

>>> [i for i in range(5) for _ in range(3)]
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

the range(3) part should be replaced with your number of repetitions...

BTW, you should use generators


Just to make it clearer, the _ is a variable name for something you don't care about (any name is allowed).

This list comprehension uses nested for loops and are just like that:

for i in range(5):
    for j in range(3):
        #your code here
沧桑㈠ 2024-12-08 02:18:04

试试这个:

itertools.chain.from_iterable(itertools.repeat(x, 3) for x in range(5))

Try this:

itertools.chain.from_iterable(itertools.repeat(x, 3) for x in range(5))
浅唱々樱花落 2024-12-08 02:18:04
from itertools import chain, izip
list(chain(*izip(*[xrange(5)]*3)))

离开

[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

列表,你就拥有了一台发电机。

编辑:甚至更好(省略对 izip 的函数调用):

list(chain(*([x]*3 for x in xrange(5))))
from itertools import chain, izip
list(chain(*izip(*[xrange(5)]*3)))

Gives

[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

Leave off the list and you have a generator.

EDIT: or even better (leaves out a function call to izip):

list(chain(*([x]*3 for x in xrange(5))))
梦境 2024-12-08 02:18:04

numpy 的帮助下,有一种非常简单的方法可以做到这一点。示例:

>>> import numpy as np
>>> np.arange(5*3) // 3
array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])

使用 range,您可以执行以下操作:

>>> list(map(lambda x: x // 3, range(5*3)))
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

记住 // 执行严格的整数除法。

There is a very simple way to do this with a help from numpy. Example:

>>> import numpy as np
>>> np.arange(5*3) // 3
array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])

With range you can do the following:

>>> list(map(lambda x: x // 3, range(5*3)))
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

Remembering that // performs a strict integer division.

涫野音 2024-12-08 02:18:04
>>> from itertools import chain, izip, tee
>>> list(chain.from_iterable(izip(*tee(range(5), 3))))
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
>>> from itertools import chain, izip, tee
>>> list(chain.from_iterable(izip(*tee(range(5), 3))))
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
停滞 2024-12-08 02:18:04

使用另一种方法的很酷的迭代器:

>>> from collections import Counter
>>> Counter(range(5) * 3).elements()

A cool iterator using another approach:

>>> from collections import Counter
>>> Counter(range(5) * 3).elements()
很酷又爱笑 2024-12-08 02:18:04

我喜欢保持简单:)

>>> sorted(list(range(5)) * 3)
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

I like to Keep It Simple :)

>>> sorted(list(range(5)) * 3)
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
就是爱搞怪 2024-12-08 02:18:04
import itertools
[x for tupl in itertools.izip(*itertools.tee(range(0,5),3)) for x in tupl]

或者:

[x for tupl in zip(range(0,5), range(0,5), range(0,5)) for x in tupl]
import itertools
[x for tupl in itertools.izip(*itertools.tee(range(0,5),3)) for x in tupl]

Or:

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