2

I have a string such as '0111111011111100' and I want to separate every four characters so here that would be:

0111,1110,1100

Then I want to replace those with another values.

Here is my code so far, but it doesn't work properly:

Beyza
  • 53
  • 8
  • to split string every nth character https://stackoverflow.com/questions/9475241/split-string-every-nth-character – You're awesome Nov 18 '17 at 11:52
  • Possible duplicate of [Split string every nth character?](https://stackoverflow.com/questions/9475241/split-string-every-nth-character) – Foxan Ng Nov 18 '17 at 11:56
  • Do you want your `line` variable to be modified so that in your case to become `line = 'abcd'`? – Vasilis G. Nov 18 '17 at 11:58
  • Please edit the question to include your expected output. – Martin Evans Nov 18 '17 at 12:15
  • Possible duplicate of [What is the most "pythonic" way to iterate over a list in chunks?](https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) – Uyghur Lives Matter Nov 18 '17 at 17:00

4 Answers4

4

You can use a list-comprehension with string indexing:

s = "0111111011111100"
[s[i:i+4] for i in range(0,len(s),4)]

gives:

['0111', '1110', '1111', '1100']

and then define a dictionary for what you want each nibble to translate to:

d = {'0111': 'a', '1110': 'b', '1111': 'c', '1100': 'd'}

and then you can shove the translating into the list-comp:

[d[s[i:i+4]] for i in range(0,len(s),4)]

which would give:

['a', 'b', 'c', 'd']

and finally use str.join to put this back into a string, making the entire conversion one-line:

''.join(d[s[i:i+4]] for i in range(0,len(s),4))

which gives:

'abcd'

as a matter of fact, a generator expression is being used here as they are more efficient than list-comprehensions

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

If you want to replace each group of four digits in string with some value you could use dict

line='0111111011111100'
lookup = {'0111': 'a', '1110': 'b', '1100': 'c'} # add all combination here
"".join(lookup.get(line[i:i+4], 'D') for i in range(0, len(line), 4)) # 'D' is default value
Out[18]: 'abDc'
kvorobiev
  • 5,012
  • 4
  • 29
  • 35
0
line='0111111011111100'
# define a dictionary with nibbles as values
dict = { '0111': 'a', '1110': 'b', '1111': 'c', '1100': 'd'}

# define chunk size
n = 4

# use a list comprehension to split the original string into chunks
key_list = [line[i:i+n] for i in range(0, len(line), n)]

# use a generator expression to replace keys in the list with
# their dictionary values and join together
key_list = ' '.join(str(dict.get(value, value)) for value in key_list)
print(key_list)
Steven_VdB
  • 108
  • 1
  • 7
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – mx0 Nov 18 '17 at 20:15
  • added some explanatory notes – Steven_VdB Nov 18 '17 at 20:27
0

Here's something that might help you:

str = "0111111011111100"

n = 4

# Create a list from you string with 4 characters in one element of list.
tmp_list = [str[i:i + n] for i in range(0, len(str), n)]
# tmp_list : ['0111', '1110', '1111', '1100']

for n, i in enumerate(tmp_list):
    if tmp_list[n] == "0111":
        tmp_list[n] = "A"
    # elif ....
    # Todo:
    # Populate your if-elif requirements here.   

result_str = ''.join(tmp_list)
Amit
  • 109
  • 2
  • 2
  • 11