如何有效地左移元组? [英] How to efficiently left-shift a tuple?

查看:40
本文介绍了如何有效地左移元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

到目前为止我所做的:

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.

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

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

告诉我,是不是...

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

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

?

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

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

推荐答案

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

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.

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

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) 需要恒定的时间.

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

Maybe you want a deque which has a rotate method?

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))

随机时间结果

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 秒的惊人时间.
  • 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.

这篇关于如何有效地左移元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆