简体   繁体   English

python中的这个旋转程序怎么了?

[英]what is wrong with this rotation program in python?

context: (enigma machine) What I am trying to do is get this bit of code to run so that the first wheel will rotate each time the sequences goes through the first wheel. 上下文:(谜题机器)我想做的是让这段代码运行,以便每次序列经过第一个轮子时第一个轮子都会旋转。

The problem is that i worked out how to do the rotation using a this sequences below from the internet yet it is not working when I change the variables around so that it can be auto-mated more. 问题是,我从互联网上找到了如何使用下面的this序列进行旋转的操作,但是当我更改变量周围的位置以便自动进行更多操作时,旋转不起作用。 It comes up with error messages after the if function. if函数后会显示错误消息。 I have checked through and changed the names of the variables to make them simpler and the spacing. 我已经检查并更改了变量的名称,以使它们更简单且更间距。 And can't find out whats not working in the code. 并且无法找出代码中不起作用的内容。 For is it that the rotate function can't use variables or what? 是因为rotate函数不能使用变量还是什么?

import collections

theinput=raw_input('enter letter')
x=0



w=collections.ww=([1,2,3,4,5])


if theinput == 'a':
    w.rotate(x)
    a = w[0]
    x= x+1
    w.rotate(x)
 print a

Thanks 谢谢

The container you probably want to use is deque , as far as I know there is no such variable as ww in collections module. 据我所知,您可能要使用的容器是deque ,据我所知,collections模块中没有ww这样的变量。

To put a bit of context, a deque is very similar to a list, but it is optimized in such a way that you can easily (efficiently) add and remove items at both ends, which is slightly more efficient that for built-in lists. 为了说明背景,双端队列与列表非常相似,但是对双端队列进行了优化,您可以轻松地(高效地)在两端添加和删除项目,这比内置列表的效率略高。 Also deques provides some additional methods not found in lists, like rotate. 此外,双端队列还提供了列表中找不到的一些其他方法,例如旋转。 While it's really easy to do the same things with lists combining basic operations, they are not optimized for these kind of things, while deques are. 使用结合了基本操作的列表来完成相同的事情确实很容易,但对于双端队列,并没有针对此类事情进行优化。 But for something as simple as simulation of an Enigma machine, sticking with lists wouldn't change performance much anyway. 但是对于像Enigma机器模拟这样的简单操作,坚持使用列表无论如何都不会改变性能。

I guess you are trying to do something like: 我想您正在尝试执行以下操作:

import collections
w = collections.deque([1, 2, 3, 4, 5])
print "Deque state is ", w
print "First item in deque is", w[0]
w.rotate(1)
print "Deque state after rotation is ", w
print "First item in deque is", w[0]

This should print 这应该打印

Deque state is  deque([1, 2, 3, 4, 5])
First item in deque is 1 Deque
state after rotation is  deque([5, 1, 2, 3, 4])
First item in deque is 5

Use negative numbers as arguments for rotate to turn the other way 使用负数作为旋转的参数以反过来

Below is an alternative implementation using only built-in lists 以下是仅使用内置列表的替代实现

w = [1, 2, 3, 4, 5]
print "List state is ", w
print "First item in list is", w[0]
x = 1 # x is rotation
w0 = w[:-x]
w = w[-x:]
w.extend(w0)
print "List state after rotation is ", w
print "First item in list is", w[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM