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:
Now I want this string to converted into a list of individual digits
I've tried both
and
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:
Strings are Iterable too For python 3x:
answered Apr 2, 2015 at 9:20
HackaholicHackaholic 18k3 gold badges52 silver badges70 bronze badges 1 Use list_comprehension.
answered Apr 2, 2015 at 9:20
Avinash RajAvinash Raj 168k25 gold badges214 silver badges261 bronze badges 1 The answers are brilliant. Here is an approach using regex
Ref - Docs on Regex Why use regex for everything? answered Apr 2, 2015 at 9:23
Bhargav RaoBhargav Rao 47.2k27 gold badges121 silver badges137 bronze badges You also can use
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:
The first
example uses the The list class takes an iterable and returns a list object. When passed a string, the If you need to split a string without spaces into a list of integers, use a list comprehension.
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
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable. We passed the Now the Note that the |