简体   繁体   中英

Python: Replacing chars in an array

I've got a question about arrays in Python. So I have to replace chars which are neighbors to each other INSIDE of array. For example (let's say input will be 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'.

The same thing with odd number of chars but last number is staying without replacing, so for 'Lover': 'L' with 'o' and so on; then 'r' is staying at the end: '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:

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:

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:

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)

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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