【问题标题】:Shifting String characters移位字符串字符
【发布时间】:2020-04-18 09:19:58
【问题描述】:

最近我有一个任务来移动字符串的字符,如下示例所示

s = 'abcd'
right shift by 2
output: 'cdab'

我使用下面的代码,它工作正常。

l1=[]
l2=[]
s='abcs'
l1=list(s)
loop = 0
rightshift = 2
while loop < rightshift:
     l2=[]
     l2=l1[1:]
     l2.append(l1[0])
     l1=[]
     l1=l2
     loop += 1
 print('After shift ' , l1)

但是,如果我们有一个像 544645655 这样的大数字,则需要很多时间。我们还有其他替代方法吗?

Some more examples(where s='abcd')
bcda 1st shift
cdab 2nd shift
dabc 3rd shift
abcd 4th shift
bcda 5th shift

【问题讨论】:

  • 这能回答你的问题吗? Rotating strings in Python
  • abcd右移3,不应该是bcda吗?
  • 已在问题中添加了一些示例,之前的问题存在一些问题
  • 一个简单的优化是意识到按字符串长度移动是无操作的,所以你只需要移动N / len(s)的其余部分。您可以使用模运算符简单地做到这一点

标签: python


【解决方案1】:

移动字符最简单的方法是:

N = 3
s = s[N:] + s[:N]

或者如果您更喜欢在道路的对面行驶:

s = s[-N:] + s[:-N]

与那些一起玩,并使用让你快乐的那个。

【讨论】:

  • 对于 'abcd' 和 N = 3,它给出了 'dabc',而不是 OP 在他的示例中给出的想要的
  • @ZainArshad 有了这个想法,你可能会自己弄清楚这些小细节
  • 它明白了,但答案应该可以解决问题。为了给出这个想法,我们在问题下使用评论部分
  • @ZainArshad 你不能得到 OP 发布的结果:'abcd' 右移 3 永远不会是 'cdab'
  • 我知道但你是左移,你可以用 N 的负数来右移
【解决方案2】:

您可以使用模运算来减少大数的移位次数:

s = "abcd"
leftshift = 544645655             # using left shift as per sample output in question
                                  # for rightshift, use negative number

N = leftshift % len(s)            # reduce shifts using modulo, explained below
print((s+s)[N:N+len(s)])

您也可以使用:print(s[N:] + s[:N]) 进行打印,如其他答案所示。

输出:

dabc

说明:

对于大于 4(字符串长度)的数字,模式只是重复。您可以使用模运算将其转换为 [0,3] 范围内的数字。

下面的a % b表示取模运算(相当于正数的remainder除法a / b

abcd   # leftshift = 0   (0 % 4 = 0)     [leftshift % len(s) = N]
bcda   # leftshift = 1   (1 % 4 = 1)
cdab   # leftshift = 2   (2 % 4 = 2)
dabc   # leftshift = 3   (3 % 4 = 3)

abcd   # leftshift = 4   (4 % 4 = 0)
bcda   # leftshift = 5   (5 % 4 = 1)
cdab   # leftshift = 6   (6 % 4 = 2)
dabc   # leftshift = 7   (7 % 4 = 3)

abcd   # leftshift = 8   (8 % 4 = 0)
  ^ repeating pattern              ^ you only have to shift by this number

所以要移动544645655,我们只需要移动544645655 % len(s) = 3

>>> s = "abcd"
>>> (s+s)
'abcdabcd'

我们在位置 0 处获取长度为 len(s) 的切片,如下所示:

'abcdabcd'
 ^^^^
>>> (s+s)[:len(s)]
'abcd'

现在我们只需要像这样移动这个切片:

abcdabcd
   ^^^^                 
>>> (s+s)[3:3+len(s)]   # 3 is the leftshift count
'dabc'

放入模表达式N = leftshift % len(s)而不是3:

>>> leftshift = 5
>>> N = leftshift % len(s)   # N = 1
>>> (s+s)[N:N+len(s)]
'bcda'

【讨论】:

  • 对不起,我已经更正了问题,样品 = bcda 1st shift, cdab 2nd shift, dabc 3rd shift, abcd 4th shift, bcda 5th shift
  • @PradeepShanbhag 新示例显示左移。没关系,你可以对rightshift变量使用负值来获得左移。
【解决方案3】:

如果你的字符串是abcd,那么执行右移3会将字符串转换为bcda而不是cdab(它是右移2而不是3)。

正如@lenik 的回答中所述,您可以使用字符串拼接来完成此操作,使用这种简单的方法可以进行右移和左移:

def shift(s, op, n):
    if op == 'left':
        return s[n:] + s[:n]
    elif op == 'right':
        return s[-n:] + s[:-n]
    else:
        print('Invalid operation')


print(shift('abcd', 'right', 3))

输出: bcda

【讨论】:

  • 使用stringtype 作为变量的坏主意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2013-12-13
  • 2019-04-26
  • 2014-12-08
  • 2020-06-18
  • 2018-12-15
  • 2013-09-11
相关资源
最近更新 更多