Hướng dẫn split without space python

I take a string of integers as input and there are no spaces or any kind of separator:

12345

Now I want this string to converted into a list of individual digits

[1,2,3,4,5]

I've tried both

numlist = map(int,input().split(""))

and

numlist = map(int,input().split(""))

Both of them give me Empty Separator error. Is there any other function to perform this task?

asked Apr 2, 2015 at 9:18

0

You don't need to use split here:

>>> a = "12345"    
>>> map(int, a)
[1, 2, 3, 4, 5]

Strings are Iterable too

For python 3x:

list(map(int, a))

answered Apr 2, 2015 at 9:20

Hướng dẫn split without space python

HackaholicHackaholic

18k3 gold badges52 silver badges70 bronze badges

1

Use list_comprehension.

>>> s = "12345"
>>> [int(i) for i in s]
[1, 2, 3, 4, 5]

answered Apr 2, 2015 at 9:20

Hướng dẫn split without space python

Avinash RajAvinash Raj

168k25 gold badges214 silver badges261 bronze badges

1

The answers are brilliant. Here is an approach using regex

>>> import re
>>> s = '12345'
>>> re.findall(r'\d',s)
['1', '2', '3', '4', '5']

Ref - Docs on Regex

Why use regex for everything?

answered Apr 2, 2015 at 9:23

Hướng dẫn split without space python

Bhargav RaoBhargav Rao

47.2k27 gold badges121 silver badges137 bronze badges

You also can use

>>> range(1,6)
[1, 2, 3, 4, 5]

answered Apr 2, 2015 at 9:21

Ishikawa YoshiIshikawa Yoshi

1,7598 gold badges21 silver badges43 bronze badges

4

How to split a string without spaces in Python #

To split a string without spaces:

  1. Use the list() class to split a string into a list of strings.
  2. Use a list comprehension to split a string into a list of integers.

Copied!

my_str = 'abcde' # ✅ split string without spaces into list of characters list_of_strings = list(my_str) print(list_of_strings) # 👉️ ['a', 'b', 'c', 'd', 'e'] # --------------- # ✅ split string without spaces into list of integers my_str_2 = '123456' list_of_ints = [int(x) for x in my_str_2] print(list_of_ints) # 👉️ [1, 2, 3, 4, 5, 6]

The first example uses the list() class to split a string without spaces.

The list class takes an iterable and returns a list object.

When passed a string, the list class splits the string into a list of characters.

If you need to split a string without spaces into a list of integers, use a list comprehension.

Copied!

my_str_2 = '123456' list_of_ints = [int(x) for x in my_str_2] print(list_of_ints) # 👉️ [1, 2, 3, 4, 5, 6]

We iterate directly over the string and on each iteration, we convert the digit that is wrapped in a string to an integer.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

An alternative approach is to use the map() function.

Copied!

my_str_2 = '123456' list_of_ints = list(map(int, my_str_2)) print(list_of_ints) # 👉️ [1, 2, 3, 4, 5, 6]

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We passed the int() class for the function to map and passed it the string for the iterable.

Now the map() function will pass each digit that is wrapped in a string to the int() class.

Note that the map() function returns a map object, so we have to use the list() class to covert the map object to a list.