같이 일하면서 여러번 파이썬 문자열 , 문자열에서 특정 문자를 제거해야 하는 문제가 있습니다. 이는 데이터 전처리에 응용될 수 있습니다. 파이썬 .
예
Input: 'Gfg, is best: for ! Geeks ;' Output: Gfg is best for Geeks Explanation: Here we can observe the difference between input and output we removed all the punctuation from the input and the ways to this is listed below to do that.>
문자열에서 구두점을 제거하는 방법
문자열에서 구두점을 제거하는 방법에는 여러 가지가 있을 수 있지만 주요 방법은 아래에 나열되어 있습니다. 그럼 하나씩 살펴보겠습니다. 이 기사에서 다룰 방법은 다음과 같습니다.
자바 널 체크
- 번역을 사용하여 문자열에서 구두점 제거
- Python 루프를 사용하여 문자열에서 구두점 제거
- Python 루프를 사용하여 문자열에서 쉼표 제거
- 정규식을 사용하여 문자열에서 구두점 제거
- for 루프, 구두점 문자열 및 not 연산자 사용
- filter()를 사용하여 문자열에서 구두점 제거
- 교체() 메소드 사용
번역을 사용하여 문자열에서 구두점 제거
에 대한 처음 두 인수 문자열.번역 메소드는 빈 문자열이고 세 번째 입력은 파이썬 목록 제거해야 할 구두점. 이는 Python 메서드에 문자열에서 구두점을 제거하도록 지시합니다. 이것은 다음 중 하나입니다. 문자열에서 구두점을 제거하는 가장 좋은 방법 .
파이썬3
import> string> test_str> => 'Gfg, is best: for ! Geeks ;'> test_str> => test_str.translate> > (> str> .maketrans('> ', '> ', string.punctuation))> print> (test_str)> |
>
>
산출:
Gfg is best for Geeks>
Python 루프를 사용하여 문자열에서 구두점 제거
이는 이 작업을 수행할 수 있는 무차별 방식입니다. 여기서는 구두점이 포함된 원시 문자열을 사용하여 구두점을 확인한 다음 해당 구두점을 제거하는 문자열을 구성합니다.
파이썬3
# initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # initializing punctuations string> punc> => '''!()-[]{};:'',./?@#$%^&*_~'''> # Removing punctuations in string> # Using loop + punctuation string> for> ele> in> test_str:> > if> ele> in> punc:> > test_str> => test_str.replace(ele, '')> # printing result> print> (> 'The string after punctuation filter : '> +> test_str)> |
>
>
산출:
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks>
시간 복잡도: 에)
보조 공간: O(n), 여기서 n은 문자열의 문자 수입니다.
Python 루프를 사용하여 문자열에서 쉼표를 제거합니다.
이것이 이 작업을 수행할 수 있는 무차별적인 방법입니다. 여기서는 쉼표가 포함된 원시 문자열을 사용하여 쉼표를 확인한 다음 해당 쉼표를 제거하는 문자열을 구성합니다.
파이썬3
마지막 커밋 실행 취소
def> remove_commas(string):> > result> => ''> > for> char> in> string:> > if> char !> => ','> :> > result> +> => char> > return> result> > input_string> => 'GFG, is, the, best.'> output_string> => remove_commas(input_string)> print> (output_string)> |
>
>
산출:
GFG is the best>
정규식을 사용하여 문자열에서 구두점 제거
구두점을 바꾸는 부분은 다음을 사용하여 수행할 수도 있습니다. 정규식 . 여기서는 특정 정규식을 사용하여 모든 구두점을 빈 문자열로 바꿉니다.
파이썬3
strsep
import> re> # initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # Removing punctuations in string> # Using regex> res> => re.sub(r> '[^ws]'> , '', test_str)> # printing result> print> (> 'The string after punctuation filter : '> +> res)> |
>
>
출력 :
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks>
for 루프, 구두점 문자열 및 not 연산자 사용
여기서는 루프 + 구두점 문자열을 사용하여 문자열에서 구두점 제거를 볼 수 있습니다.
파이썬3
# initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # initializing punctuations string> punc> => '''!()-[]{};:'',./?@#$%^&*_~'''> res> => ' '> for> ele> in> test_str:> > if> ele> not> in> punc:> > res> +> => ele> > # printing result> print> (> 'The string after punctuation filter : '> +> res)> |
자바 스위치
>
>산출
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks>
모든 방법의 시간 및 공간 복잡도는 동일합니다.
시간 복잡도: 에)
보조 공간: 에)
filter()를 사용하여 문자열에서 구두점 제거
filter() 메서드는 주어진 조건에 따라 시퀀스의 요소를 필터링합니다.
이 경우 filter() 메서드와 람다 함수를 사용하여 구두점 문자를 필터링할 수 있습니다.
파이썬3
def> remove_punctuation(test_str):> # Using filter() and lambda function to filter out punctuation characters> > result> => ''.join(> filter> (> lambda> x: x.isalpha()> or> x.isdigit()> or> x.isspace(), test_str))> > return> result> test_str> => 'Gfg, is best : for ! Geeks ;'> print> (> 'The original string is : '> +> test_str)> result> => remove_punctuation(test_str)> print> (> 'The string after punctuation filter : '> +> result)> #This code is contributed by Edula Vinay Kumar Reddy> |
>
>산출
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks>
시간 복잡도: 에)
보조 공간: 에)
replacement() 메소드를 사용하여 문자열에서 구두점 제거
문자열 모듈을 가져온 다음 입력 문자열을 초기화하고 원래 문자열을 인쇄합니다. insert() 메서드를 사용하여 입력 문자열에서 각 구두점 문자를 제거한 후 문자열 구두점 상수의 각 구두점 문자를 반복합니다. 구두점을 제거한 후 결과 문자열을 인쇄합니다.
파이썬3
기가바이트 대 메가바이트
import> string> # initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # Removing punctuations using replace() method> for> punctuation> in> string.punctuation:> > test_str> => test_str.replace(punctuation, '')> # printing result> print> (> 'The string after punctuation filter : '> +> test_str)> |
>
>산출
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks>
시간 복잡도 분석: O(len(string.tempoation) * len(test_str)) for 루프는 string.puntation 상수의 모든 문장 부호 문자를 반복하므로 O(len(string.tempoation)) 시간이 걸립니다.
보조 공간 분석: O(1) . 입력 문자열이 제자리에서 수정되므로 결과를 저장하는 데 추가 공간이 필요하지 않습니다.