【问题标题】:Circular shift of a string element字符串元素的循环移位
【发布时间】:2018-12-15 18:38:42
【问题描述】:

基本上,我已将整数转换为二进制表示,然后以字符串格式存储。

我想循环旋转数字。

我应该如何进行?

我用过np.roll(),但它不起作用。

【问题讨论】:

  • 我需要更多信息。您可以编辑您的问题以使用您的代码发布吗?阅读this怎么样?

标签: python numpy


【解决方案1】:

你可以创建一个新的字符串如下循环移动它

bin_str = bin_str[-1] + bin_str[:-1]

如果不行,可以使用collections.deque(有rotate方法)来实现循环移位效果

from collections import deque
bin_str = "{0:b}".format(10)
print (bin_str)
1010

d = deque(bin_str, maxlen=len(bin_str))
print (d)
# deque(['1', '0', '1', '0'], maxlen=4)

d.rotate()
print (d)
# deque(['0', '1', '0', '1'], maxlen=4)

【讨论】:

    【解决方案2】:

    尝试使用负移位 例如像这样 打印(np.roll(a, -3))

    【讨论】:

    • "我使用了 np.roll() 但它不起作用。"我不确定这个答案会增加什么。
    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2015-12-23
    • 2020-09-19
    相关资源
    最近更新 更多