2

I'm trying to convert a user inputted string into a list of 2 digit numbers. When the user enters a string, say "1234" and I convert that into a list, it becomes [1,2,3,4]. What I need it to become is [12,34] and so on. How would one go about doing this?

print("Please insert string.")  # User enters "12345678"
string1 = input()
list1 = list(string1)  # "12345678" becomes [1,2,3,4,5,6,7,8]
if not any(char.isdigit() for char in string1):
    print("Submission must be numbers only.")
else:
    magically turn list1 into 2digitlist
print(2digitlist)
# Desired output: [12,34,56,78]

Hopefully there's a solution to this but I certainly have no clue what it is. That's what I get for trying to do something complex even though I have little experience I suppose. Thanks in advance.

idjaw
  • 25,487
  • 7
  • 64
  • 83
Riftie
  • 43
  • 3
  • 3
    What happens if user inputs an odd-length string? – DeepSpace Sep 07 '19 at 15:39
  • 2
    Do you want ``[12, 34]`` or ``["12", "34"]``? Is there a reason why you do not ask for ``12 34`` (or another delimiter) from the user? – MisterMiyagi Sep 07 '19 at 15:40
  • 1
    You've provided a good of information already, which is a great start. However, as already asked by the two previous comments, it would be helpful if you provided more details around the constraints and conditions of this code. Is it only to be run for even numbers? Is the output in fact supposed to provide integers in its outputs as well? If it is in fact supposed to take any length, then what should the output be for an odd number of values, like `1234567`? – idjaw Sep 07 '19 at 15:44
  • 1
    Any reasons why you use an abundance of brackets? `list1 = (list(string1))` should be `list1 = list(string1)` and `if not (any(char.isdigit() for char in string1)):` should be `if not any(char.isdigit() for char in string1):` Also, your check does not work, `any` should be `all` – Finomnis Sep 07 '19 at 15:44
  • I haven't written the code to check for odd length strings yet but yes the final product is only supposed to accept an even string. I was going to write that after I figured this out since it's my biggest road block. I could ask the user to separate their entries but it would be a lot more work for the user since the input they're entering is given to them to enter, like a password. Also thanks for pointing out the integer check problem I didn't realize. – Riftie Sep 07 '19 at 15:53
  • Okay thanks, I tried searching for a bit already on how to do this but I supposed I didn't think hard enough to find the right key words for a good result. Thanks for the link wjandrea, that solves my problem. Sorry for the duplicate. – Riftie Sep 07 '19 at 15:55
  • @Riftie You will notice that the question has been marked as a duplicate. The question that is provided as the duplicate to yours will give you exactly what you are looking for with a really good explanation. – idjaw Sep 07 '19 at 15:56
  • @Riftie on a side note: you also don't need to convert stuff to a list and then iterate over it to check whether they're all digits or not... `if not string1.isdigit()` will be enough to get into your submission must be numbers only... – Jon Clements Sep 07 '19 at 15:56
  • @JonClements Thanks for the tip. That works a lot better. Always more to learn. – Riftie Sep 07 '19 at 15:58
  • @Riftie So, for eg, to find a valid string (eg - it's not empty, has even length and all are digits) before attempting to break it into chunks, it'd be something like: `if string1 and len(string1) % 2 == 0 and string1.isdigit():` - then you can use the associated duplicate to do the splitting and int conversions... – Jon Clements Sep 07 '19 at 15:58

1 Answers1

0

My attempt with strided array access, combined with zip:

string1 = "123456"

list1 = list(string1) #"12345678" becomes [1,2,3,4,5,6,7,8]
if not all(char.isdigit() for char in string1):
    print("Submission must be numbers only.")
else:
    digitlist=[10*int(e1) + int(e2) for (e1,e2) in zip(list1[::2], list1[1::2])]
    print(digitlist)

[12, 34, 56]

Of course, this only works correctly if the input contains an even amount of digits, otherwise it will ignore the last digit. But that wasn't specified in the question.

Finomnis
  • 18,094
  • 1
  • 20
  • 27