简体   繁体   English

Python:替换数组中的字符

[英]Python: Replacing chars in an array

I've got a question about arrays in Python.我在 Python 中有一个关于 arrays 的问题。 So I have to replace chars which are neighbors to each other INSIDE of array.所以我必须在数组内部替换彼此相邻的字符。 For example (let's say input will be Mike ):例如(假设输入将是Mike ):

stack = []
word = input("Type Your word here: ")
wordChars = list(word)

for i in range(len(wordChars)):
    stack.append(wordChars[i])

print(stack)

result: ['M', 'i', 'k', 'e']

So, when I have even number of chars - I need to replace neighbors, so:所以,当我有偶数个字符时 - 我需要替换邻居,所以:

'M' with 'i' and so on: 'iMek'. 'M' 和 'i' 等等:'iMek'。

The same thing with odd number of chars but last number is staying without replacing, so for 'Lover': 'L' with 'o' and so on;奇数个字符但最后一个数字保持不变,所以对于“情人”:“L”和“o”等等; then 'r' is staying at the end: 'oLevr'.然后'r'留在最后:'oLevr'。

I know how to find if word is even or odd with modulo but still can't figure out how to replace neighbors.我知道如何用模查找单词是偶数还是奇数,但仍然不知道如何替换邻居。

a = ['L', 'o', 'v', 'e', 'r']
for i in range(0,len(a)-1,2):
    a[i], a[i+1] = a[i+1], a[i]

print(a)

You can write a function that replaces 2 indexes in an array:您可以编写一个 function 替换数组中的 2 个索引:

def replace(arr, i, j):
    tmp = arr[i]
    arr[i] = arr[j]
    arr[j] = tmp

Now you can go over the list and replace every 2 neighbors:现在您可以在列表中使用 go 并替换每 2 个邻居:

for i in range(0, len(wordChars) - 1, 2):
    replace(wordChars, i, i + 1)

As mentioned by DarrylG, in python you can swap 2 elements by doing the following:如 DarrylG 所述,在 python 中,您可以通过执行以下操作交换 2 个元素:

arr[i], arr[j] = arr[j], arr[i]

Thus, making the code simpler:因此,使代码更简单:

for i in range(0, len(wordChars) - 1, 2):
    arr[i], arr[j] = arr[j], arr[i]

Alternative approach with list.pop(0) list.pop(0) 的替代方法

def flip(a):

    #convert input string to list
    lst = list(a)

    #initialise output string
    out = ''

    # if more than 1 element is left, remove the first two elements and invert
    while len(lst)>1:
        a = lst.pop(0)
        b = lst.pop(0)
        out = out + b + a

    # if list has at least one element, add this one to the output string
    if lst:
        out = out + lst.pop()

    return out

Without giving it too much thought, loop through (based on modulo as you say) and do something like this:不用想太多,循环(基于你所说的模数)并执行以下操作:

for x in range(0, len(wordChars) % 2, 2):
    char_a       = stack[i]
    char_b       = stack[i + 1]
    stack[i]     = char_b
    stack[i + 1] = char_a

This will basically advance through the list in increments of 2 where each increment therefore swaps items 0 and 1, 2 and 3, 4 and 5 etc etc.这基本上将以 2 为增量在列表中前进,因此每个增量都会交换项目 0 和 1、2 和 3、4 和 5 等。

You can take two letters at a time and iterate like the following:您可以一次取两个字母并进行如下迭代:

length = len(stack)
for i in range(0,length,2):
  if(i+1 >= length ):
      break
  temp = stack[i]
  stack[i] = stack[i+1]
  stack[i+1] = temp

Use this Code to do it -使用此代码来做到这一点 -

def is_even(stack):
        for i in range(0, len(stack), 2):
            temp = stack[i]
            stack[i] = stack[i+1]
            stack[i+1] = temp
        return stack

def is_odd(stack):
    for i in range(0, len(stack - 1), 2):
        temp = stack[i]
        stack[i] = stack[i+1]
        stack[i+1] = temp
    return stack

stack = []
word = input("Type Your word here: ")
wordChars = list(word)

for i in range(len(wordChars)):
    stack.append(wordChars[i])

print(stack)
if len(stack) % 2 == 0:
    new_stack = is_even(stack)
else:
    new_stack = is_odd(stack)

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

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