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)
natalie
  • 13
  • 4
  • where is the code you have trie so far and what didnt work with it – Chris Doyle Feb 28 '20 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 – natalie Feb 28 '20 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... – Jon Clements Feb 28 '20 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 – natalie Feb 28 '20 at 16:30
  • ok sorry, i will edit it – natalie Feb 28 '20 at 16:31
  • @natalie how do you expect to go back from `result=NaDatavilid` to `x= Natalie y= david`? The `e` is missing from `result`. Do you want to store it from the previous operation? – Dan Feb 28 '20 at 16:34
  • Also how come when davids as assigned to `y` it now has a `.` at the end – Chris Doyle Feb 28 '20 at 16:36
  • yes sorry i miss write the e yes its should be there – natalie Feb 28 '20 at 16:37
  • What would be the expected output when one name is significantly longer then the other, how would you expect to split them back into two names? – Chris Doyle Feb 28 '20 at 16:40
  • Is there some constraint here that names will be equal length or differ by no more than 1? otherwise how can a function know how to split back in 2 names – Chris Doyle Feb 28 '20 at 16:48
  • @Chris Doyle yes both string are equal length, – natalie Feb 28 '20 at 16:55
  • @Chris Doyle the both variable name and family are equal in length size for sure.what you said its absolutely right, that's why i mention both string equal length size – natalie Feb 28 '20 at 16:56
  • 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 – Chris Doyle Feb 28 '20 at 16:58
  • @ChrisDoyle, sir , i give two example with different string length size, but if name is 7 bit the family will be 7 bit also and so one , i hope you got my point sir – natalie Feb 28 '20 at 17:01
  • but you have given exmaple name = Natalie Family = David here name and family are not the same length – Chris Doyle Feb 28 '20 at 17:02
  • @ChrisDoyle my god, i'm sorry its my mistake , i will edit the code, the both variable should be same length , sorry sir – natalie Feb 28 '20 at 17:03

2 Answers2

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
Cedric Druck
  • 1,032
  • 7
  • 20
  • 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...` – Jon Clements Feb 28 '20 at 16:37
  • @CedricDruck good answer thx, but u are split the strings into one bit look to the anser ejlaemoensora – natalie Feb 28 '20 at 16:39
  • 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 – Cedric Druck Feb 28 '20 at 16:41
  • 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 – natalie Feb 28 '20 at 16:43
  • 1
    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... – Jon Clements Feb 28 '20 at 16:44
  • colude u modify your code plz sir, im getting NameError: name 'zip_longest' is not defined – natalie Feb 28 '20 at 16:47
  • @natalie make sure you `import re` and `from itertools import zip_longest` at the top of your file – Jon Clements Feb 28 '20 at 16:51
  • @CedricDruck thanks for your code sir its help my first part, hopefully i solve second part. – natalie Feb 28 '20 at 16:52
  • @CedricDruck if we try split the result into two parts as the both a and b are equal length size? what you suggest in this case? – natalie Feb 28 '20 at 16:59
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']
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42