Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python代码中的数字移位_Python_Python 2.7 - Fatal编程技术网

python代码中的数字移位

python代码中的数字移位,python,python-2.7,Python,Python 2.7,请帮助我,我不知道如何写这个函数。我尝试了一个Ceser-cypher函数,但没有成功。有什么想法吗 编写一个函数循环S,n,它接受一个“0”和“1”的字符串S和一个整数n,并返回一个字符串,其中S已将其最后一个字符移动到初始位置n次。例如,循环'111011000',2将返回'0011101100'。您要查找的函数是: def cycle(s, n): return s[-n:] + s[:-n] 您可以使用Python的deque数据类型,如下所示: import collecti

请帮助我,我不知道如何写这个函数。我尝试了一个Ceser-cypher函数,但没有成功。有什么想法吗


编写一个函数循环S,n,它接受一个“0”和“1”的字符串S和一个整数n,并返回一个字符串,其中S已将其最后一个字符移动到初始位置n次。例如,循环'111011000',2将返回'0011101100'。

您要查找的函数是:

def cycle(s, n):
    return s[-n:] + s[:-n]
您可以使用Python的deque数据类型,如下所示:

import collections

def cycle(s, n):
    d = collections.deque(s)
    d.rotate(n)
    return "".join(d)

print cycle('1110110000', 2)
这将显示:

0011101100

提示:如果s='111011000',什么是s[:-2]和什么是s[-2:]?你要找的谷歌搜索词是字符串切分。听起来像家庭作业。