如何有效地左移元组?

发布于 2024-10-21 13:46:45 字数 787 浏览 2 评论 0原文

我正在寻找一种有效的方法来左移元组。

到目前为止我所做的:

def leftShift(tup, n):
    length = len(tup)
    if length != 0:
        n = n % length
    else:
        return tuple()
    return tup[n:] + tup[0:n]

sample = (1,2,3,4)
sample2 = ()

print(leftShift(sample, 5)) #prints (2, 3, 4, 1)
print(leftShift(sample, 1)) #prints (2, 3, 4, 1)
print(leftShift(sample, 15)) #prints (4, 1, 2, 3)
print(leftShift(sample, 3)) #prints (4, 1, 2, 3)
print(leftShift(sample2, 4)) #prints ()

要移动的位置数作为第二个参数给出。

有效率吗?可以用更 Pythonic 的方式编码吗?

告诉我,它……

length = len(tup)
if length != 0:
    n = n % length

比……更有效率吗

if len(tup) != 0:
    n = n % len(tup)

我的意思是, len(tup) 是 O(1) 还是我应该记住它以供以后使用?

I'm looking for an efficient way to left-shift a tuple.

What I've done so far:

def leftShift(tup, n):
    length = len(tup)
    if length != 0:
        n = n % length
    else:
        return tuple()
    return tup[n:] + tup[0:n]

sample = (1,2,3,4)
sample2 = ()

print(leftShift(sample, 5)) #prints (2, 3, 4, 1)
print(leftShift(sample, 1)) #prints (2, 3, 4, 1)
print(leftShift(sample, 15)) #prints (4, 1, 2, 3)
print(leftShift(sample, 3)) #prints (4, 1, 2, 3)
print(leftShift(sample2, 4)) #prints ()

The number of places to shift is given as the second argument.

Is it efficient? Can it be coded in more Pythonic way?

And tell me, is it...

length = len(tup)
if length != 0:
    n = n % length

more efficient than

if len(tup) != 0:
    n = n % len(tup)

?

I mean, is len(tup) O(1) or should I remember it for later usage?

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

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

发布评论

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

评论(2

我最亲爱的 2024-10-28 13:46:45

您所做的绝对是微观优化,如果您不确切知道自己的目标是什么,那完全是浪费时间。

代码的第一个版本可能更快,因为它使用了更少的函数调用,但两者都很好。如果您真的关心速度,您应该首先弄清楚如何使用分析器和 timeit 模块。

len(tup) 需要常数时间。

也许您想要一个具有旋转方法的 deque

这里有一些替代方案:

def leftShift1(tup, n):
    try:
        n = n % len(tup)
    except ZeroDivisionError:
        return tuple()
    return tup[n:] + tup[0:n]

def leftShift2(tup, n):
    length = len(tup)
    if length != 0:
        n = n % length
    else:
        return tuple()
    return tup[n:] + tup[0:n]

def leftShift3(tup, n):
    if len(tup) != 0:
        n = n % len(tup)
    else:
        return tuple()
    return tup[n:] + tup[0:n] 

def leftShift4(tup, n):
    if tup:
        n = n % len(tup)
    else:
        return tuple()
    return tup[n:] + tup[0:n]

sample= tuple(range(10))

随机 timeit 结果

D:\downloads>python -m timeit -s"from asd import *" "leftShift1(sample, 20)"
1000000 loops, best of 3: 0.472 usec per loop

D:\downloads>python -m timeit -s"from asd import *" "leftShift2(sample, 20)"
1000000 loops, best of 3: 0.533 usec per loop

D:\downloads>python -m timeit -s"from asd import *" "leftShift3(sample, 20)"
1000000 loops, best of 3: 0.582 usec per loop

D:\downloads>python -m timeit -s"from asd import *" "leftShift4(sample, 20)"
1000000 loops, best of 3: 0.474 usec per loop

所以:

  • 最 Pythonic 的代码(try .. exceptif tup:)是最快的。为此我一定会喜欢Python。
  • 您可以节省令人难以置信的 0.0000001 秒。

What you're doing is absolute micro-optimization, which is a total waste of time if you don't know exactly what you're aiming for.

The first version of you code is probably faster because it uses one less function call, but both are fine. If you really care about speed you should figure out how to use a profiler and the timeit module first.

len(tup) takes constant time.

Maybe you want a deque which has a rotate method?

Here are some alternatives:

def leftShift1(tup, n):
    try:
        n = n % len(tup)
    except ZeroDivisionError:
        return tuple()
    return tup[n:] + tup[0:n]

def leftShift2(tup, n):
    length = len(tup)
    if length != 0:
        n = n % length
    else:
        return tuple()
    return tup[n:] + tup[0:n]

def leftShift3(tup, n):
    if len(tup) != 0:
        n = n % len(tup)
    else:
        return tuple()
    return tup[n:] + tup[0:n] 

def leftShift4(tup, n):
    if tup:
        n = n % len(tup)
    else:
        return tuple()
    return tup[n:] + tup[0:n]

sample= tuple(range(10))

random timeit results

D:\downloads>python -m timeit -s"from asd import *" "leftShift1(sample, 20)"
1000000 loops, best of 3: 0.472 usec per loop

D:\downloads>python -m timeit -s"from asd import *" "leftShift2(sample, 20)"
1000000 loops, best of 3: 0.533 usec per loop

D:\downloads>python -m timeit -s"from asd import *" "leftShift3(sample, 20)"
1000000 loops, best of 3: 0.582 usec per loop

D:\downloads>python -m timeit -s"from asd import *" "leftShift4(sample, 20)"
1000000 loops, best of 3: 0.474 usec per loop

So:

  • The most Pythonic code (try .. except and if tup:) is the fastest. Gotta love Python for that.
  • You can save the incredible amount of 0.0000001 seconds.
伊面 2024-10-28 13:46:45

更简洁一点

def leftShift(tup, n):
    if not tup or not n:
        return tup
    n %= len(tup)
    return tup[n:] + tup[:n]

A little more tersely

def leftShift(tup, n):
    if not tup or not n:
        return tup
    n %= len(tup)
    return tup[n:] + tup[:n]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文