0

Is there way to split or chunk the dynamic string into fixed size? let me explain:

Suppose:

name = Natalie
Family = David12 

length = len(name)  #7 bit
length = len(Family) # 7 bit

i want to split the name and family into and merging as :

result=nadatavilid1e2

and again split and extract the the 2 string as

x= Natalie
y= david

another Example:

Name = john
Family= mark

split and merging:

result= jomahnrk

and again split and extract the the 2 string as

x=john 
y= mark

.

Remember variable name and family have different size length every time not static! . i hope my question is clear. i have seen some related solution about it like here and here and here and here and here and here and here but none of these work with what im looking for. Any suggestion ?? Thanks

i'm using spyder python 3.6.4

I have try this code split data into two parts:

def split(data):
    indices = list(int(x) for x in data[-1:])
    data = data[:-1]
    rv = []
    for i in indices[::-1]:
        rv.append(data[-i:])
        data=data[:-i]
    rv.append(data)
    return rv[::-1]

data='Natalie'
x,c=split(str(data))
print (x)
print (c)
16
  • where is the code you have trie so far and what didnt work with it Commented Feb 28, 2020 at 16:25
  • i have try with this code split into 2 parts but its not working what im looking for code is there : def split(data): indices = list(int(x) for x in data[-1:]) data = data[:-1] rv = [] for i in indices[::-1]: rv.append(data[-i:]) data=data[:-i] rv.append(data) return rv[::-1] data='natalie' x,c=split(str(data)) print x print c Commented Feb 28, 2020 at 16:29
  • @natalie could you edit your post to include that in a code block please - comments really aren't any good for code... Commented Feb 28, 2020 at 16:29
  • what i want is data it self every time split into 2 bit as long as the length of the string and merging with other string in between as shown in the example Commented Feb 28, 2020 at 16:30
  • 1
    you first example uses Natalie(7chars) and David(5 chars) if both are equal length then this becomes a trivially easy pproblem to solve Commented Feb 28, 2020 at 16:58

2 Answers 2

0

Given you have stated names will always be of equal length you could use wrap to split in to 2 char pairs and the zip and chain to join them up. In the split part you can again use wwrap to split in 2 char pairs but if the number of pairs is odd then you need to split the last pair into 2 single entries. something like.

from textwrap import wrap
from itertools import chain


def merge_names(name, family):
    name_split = wrap(name, 2)
    family_split = wrap(family, 2)
    return "".join(chain(*zip(name_split, family_split)))


def split_names(merged_name):
    names = ["", ""]
    char_pairs = wrap(merged_name, 2)
    if len(char_pairs) % 2:
        char_pairs.append(char_pairs[-1][1])
        char_pairs[-2] = char_pairs[-2][0]
    for index, chars in enumerate(char_pairs):
        pos = 1 if index % 2 else 0
        names[pos] += chars
    return names

print(merge_names("john", "mark"))
print(split_names("jomahnrk"))
print(merge_names("stephen", "natalie"))
print(split_names("stnaeptaheline"))
print(merge_names("Natalie", "David12"))
print(split_names("NaDatavilid1e2"))

OUTPUT

jomahnrk
['john', 'mark']
stnaeptaheline
['stephen', 'natalie']
NaDatavilid1e2
['Natalie', 'David12']
Sign up to request clarification or add additional context in comments.

1 Comment

prefect and supper answer, god bless you always. this is what i'm looking for. thank you.
0

Something like:

a = "Eleonora"
b = "James"

l = max(len(a), len(b))
a = a.lower() + " " * (l-len(a)) 
b = b.lower() + " " * (l-len(b))

n = 2
a = [a[i:i+n] for i in range(0, len(a), n)]
b = [b[i:i+n] for i in range(0, len(b), n)]

ans = "".join(map(lambda xy: "".join(xy), zip(a, b))).replace(" ", "")

Giving for this example:

eljaeomenosra

9 Comments

From the examples (even though one is slightly off - might be a typo...) the OP has given... it looks like pairs of letters are needed from each, so given your example, it seems they'd want it to be ElJaeome...
@CedricDruck good answer thx, but u are split the strings into one bit look to the anser ejlaemoensora
The second part seems impossible. When merging the two names, by definition you lose the individual lengths. If you allow using the original lengths then it may be possible
but your answer for first part was wrong sir, as i seen u split only one bit and merging another bit compare both answer of mine and yours in your code
You could also do something like: ans = ''.join(''.join(names) for names in zip_longest(*[re.findall('.{,2}', name) for name in (a, b)], fillvalue='')) which'll scale to more than one name and you can specify the order... can't see how you'd get back to 2 names, let alone more, but seems to work...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.