The decimal number received from the user is converted into a decimal number and divided into two.
I want to store it in a list and represent it in a different decimal number.
For example, when 424896 is converted to binary, it becomes as follows.
If 1 or 0 is 2 consecutive times, this number
Decode them to 1 or 0 (eg 11 → 1, 00 → 0).
Continue it until a single 0 (separator role) comes, and if you hit it (eg 000, 11011),
Decode until the end again.
If i store the above two antilogarithms in this way, you get [1011, 11000].
From here, if you convert it to a decimal number, the output will be
It will be [11, 24]. Also, numbers ending in a single 1 or 0, such as 7 (111) and 24 (11000), are
Does not decode.
What I tried
def get_list (bit): l =  bit = str (bit) bit_sliced = bit [:: 2] # 11 → 1, 00 → 0 while bit == bit [len (bit) // 2]: # Determining the center l.append (int (bit_sliced)) break l  = "," l.append (int (bit_sliced)) return l n = 1100111101111000000 def get_list (bit): l =  l2 =  bit = str (bit) bit_s = bit [: len (bit) ///2] bit2_s = bit [:: 2] l.append (int (bit_s)) l2.append (int (bit2_s)) return l2 + l print (get_list (n)) out: out: None [1011011000, 110011110]
First, I changed the type from Int to str and tried various things. The while one will be None.
I also found 000 in .split (",") and regular expression re.match, and tried replace, but it didn't work.
I'm still new to python, but thank you.
Answer # 1
I'm sorry, the code that was written was difficult, so I wrote it in a different form. for your information.
I added it because I overlooked that it is ignored if the end ends with a single 0 or 1 (= an odd number of 0s or 1s are lined up at the end).
By the way, what if there is a single 1 other than the end ...? For example, "10011".
def sep0 (n): ns = str (n) .replace ("101", "1,1"). split (",") result =  for i, t in enumerate (ns): if judge_odd (t): continue result.append (t) return result def judge_odd (txt): if len (txt.replace ("10", ", 0"). split (",") [-1])% 2! = 0: return True elif len (txt.replace ("01", ", 1"). split (",") [-1])% 2! = 0: return True else: else: return False def doublet_to_single (txt): result = "" i = 0 while i<len (txt)-1: if txt [i] == txt [i + 1]: result + = txt [i] i + = 1 i + = 1 return result n = "11101100111101111000000" sep = sep0 (n) print (sep) # ['11001111', '1111000000'] single = [doublet_to_single (i) for i in sep] print (single) # ['1011', '11000'] dec = [int (r, 2) for r in single] print (dec) # [11, 24]
Answer # 2
It seems that you should look at each of them as shown below and process them separately.
def conv (s): ret =  one ='' while True: c = s [: 2] st = 2 if c == '01': # Break in the middle ret.append (one) one ='' st = 1 elif c == '00': # 0 one + = '0' elif c == '11': # 1 one + = '1' elif c =='': # Exactly at the end if len (one)>0: ret.append (one) break else: # unintended tail break s = s [st:] return ret s = bin (424896) [2:] print (conv (s)) # ['1011', '11000'] for s in ['', '0', '1', '00', '11', '111', '11000']: ret = conv (s) print (s, ret)
- python - i want to divide the contents displayed from the database by user
- python - i want to store a 3d array in an array using numpy
- where is the minor version number of python that starts by default specified?
- python 3x - i want to divide the process when it is err as a result of command execution in python3 subprocess
- python - when you want to judge by the number of characters from the back with a regular expression
- python - how to get the number of searches for a specific word within the period
- python - sumy by specifying the maximum number of characters with sumy
- python - i want to add a line number to the data frame
- number of python combinations
- python 3x - i want to output the name corresponding to the number entered in the excel file
- python - how to accept number input even if keypressevent is defined for qlineedit in pyside
- i want to invert the number 12345 in python like 54321 and display it
- get the number of characters from the python text box
- get the total number and name of python grouping
- python - i want to create a binary mask by comparing two elements and setting the smaller one to 0
- how to align the number of elements with overlaplengt of python etc
- set python excel serial number to date and time
- how to store in dict using python for loop
- python - valueerror: field'id' expected a number but got'suzukitadashi'