Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split integer and add to list?

I want to know how can we program in Python please.

So,
D = read().value

And D value is updated two times
First time D = [50, 11, 18, 21, 34]
Second time D = 1346 (second time D value is always 4 digit integer)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to get the final result like this (First time D array with the second time D with 4 digit integer has to be split into two elements and added to the end of the array)

D = [50, 11, 18, 21, 34, 13, 46]

>Solution :

You can achieve this result with by using a temporary variable for storing the initial list of numbers contained in D when D is updated to contain an integer. Then you can cast the integer to a string and use substrings to get the first two characters of the string and then cast them back to and integer. Then you append the integer to the list of integers contained in the temporary variable. Repeat for the last two characters in of the string representation of the four digit number contained in D. Finally we assign the value of the temporary variable back to D and print it out. This code should behave as I outlined:

D = [50, 11, 8, 21, 4]
print(D)
tmp = D
D = 1346
D = str(D)
tmp.append(int(D[:2]))
tmp.append(int(D[2:]))
D=tmp
print(D)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading