Put a character by according to a list of its index number to a string [Python 2.7]

Htet

The sample string and the given range of index numbers of a character can be seen below:

a_str = 'Iamgoingtoschool'
num_index = ['1','3','8','10','16',...]

As the position of a character ('|') should be assigned as index number of the list(num_index), the outcome should be like this:

get_result = 'I|am|going|to|school|'

The attempt is in below:

for i in num_index:
    get_res = a_str[:i]+'|'+a_str[i:]
Arya McCarthy

First of all, your values in num_index should not be strings. They should be integers.

num_index = [int(n) for n in num_index]

After that, you're completely overwriting get_result on each iteration of your loop. Instead, strive for something more like this:

from itertools import tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

get_result = a_str[:num_index[0]]  # Start with the first chunk of `a_str`
for a, b in pairwise(num_index):
    get_result += '|' + a_str[a:b]
get_result += '|' + a_str[num_index[-1]:]  # Attach the last part.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Put number in list python

How to Order these list of string according to its version

Return value from list according to index number

Python - Check if part of a string exists in a list and give the index of its match

Angular 2 put space between number according to number length

Python string index and character comparing

Multiply char in string by its index number

Python: how to generate a list of words from a string and save it in a text file according to their index?

Get the index of xml element according to its attribute / Python

Sorting a list of words according its position in a given string in dart

Python - Select string from a list based on index number - index number entered via input

Replacing each character in a string using an index into a list

Accessing each character in a string in an index in a list

I want to split a string by a character on its first occurence, which belongs to a list of characters. How to do this in python?

Python string to list per character

transform list(character) to string in python

how to count the number of a certain character in a list of list of string in python version 3?

Index of a variable in a number list in Python

how to compare and merge two list according to its sequence in python?

Python - Build a tuple list according to the character frequencies in the input

return index of item in list according to another list, python

Replacing index numbers of a list according to another list in Python

replacing first character in a string with multiple characters using its index

Find number from string character matching its pattern

Ansible remove last character from string if its a number

Python read file and write lines according to their index number

finding the index of the elements appended to new list from its old list - python 2

How to put a string in a specific index position in python

How can I get the index of a string in Python list, which contains a certain character?

TOP Ranking

HotTag

Archive