Home > other >  The Python string set all the substring
The Python string set all the substring

Time:09-22

For all the substring of string sets, such as "ABC" substring 'a', 'b', 'c', 'ab', "BC", "ABC", now have the idea is to traverse all string length according to the position control, to write again after heavy list, but if the string through long, number too much, many cycles, the program runs, hope everybody can big door to give some advice, thank you

CodePudding user response:

Is there a man

CodePudding user response:

 
#! The/usr/bin/env python
# - * - coding: utf-8 - * -
# Python 3.6
The from itertools import the accumulate

Def all_sub_string (a_string) :
If len (a_string)==1:
Return [a_string]
The else:
Return the list (the accumulate (a_string)) + all_sub_string (a_string [1])


Print (all_sub_string (" abcde "))

CodePudding user response:

refer to the second floor xpresslink response:
 
#! The/usr/bin/env python
# - * - coding: utf-8 - * -
# Python 3.6
The from itertools import the accumulate

Def all_sub_string (a_string) :
If len (a_string)==1:
Return [a_string]
The else:
Return the list (the accumulate (a_string)) + all_sub_string (a_string [1])


Print (all_sub_string (" abcde "))



Results:
[' a ', 'ab', 'ABC' and 'abcd', 'abcde', 'b', 'the BC', 'BCD', 'bcde', 'c', 'CD', 'cde', 'd', 'DE', 'e']

CodePudding user response:

Blogger, how are you, I just started to learn python, learning python is promising

CodePudding user response:

reference 4 floor pythonner_ljx response:
blogger, hello, I just started to learn python, learn to python promising

No, go to learn Java or c + +, such as learning master, came back to see 30 minutes python tutorial you will,

CodePudding user response:

Perfect implementation

CodePudding user response:

Is the length of traverse, and combination, ready-made functions are available

CodePudding user response:

Str1="ABC"
For num in range (1, len (str1) + 1) :
The import itertools
For I in itertools.com binations (str1, num) :
Print ('. Join (I))
print()

CodePudding user response:

 import itertools 
Str1="ABC"
For num in range (1, len (str1) + 1) :
For I in itertools.com binations (str1, num) :
Print ('. Join (I))
Print ()


A
B
C
AB
AC
BC
ABC
  • Related