de-vraag
  • Otázky
  • Značky
  • Používatelia
Oznámenia
Odmeny
Registrácia
Po registrácii budete informovaní o odpovediach a komentároch na vaše otázky.
Prihlásiť sa
Ak už máte konto, prihláste sa a skontrolujte nové oznámenia.
Za pridané otázky, odpovede a komentáre budú udelené odmeny.
Viac na
Zdroj
Upraviť
Lawrence Johnston
Lawrence Johnston
Question

Najlepší spôsob odstránenia interpunkcie z reťazca

Zdá sa, že by mal existovať jednoduchší spôsob ako:

import string
s = "string. With. Punctuation?" # Sample string 
out = s.translate(string.maketrans("",""), string.punctuation)

Existuje?

570 2008-11-05T17:30:32+00:00 3
 Georgy
Georgy
Edited question 26 jún 2019 в 1:36
Programovanie
python
string
punctuation
This question has 1 odpoveď in English, to read them log in to your account.
Solution / Answer
 Brian
Brian
5 november 2008 в 6:36
2008-11-05T18:36:11+00:00
Viac na
Zdroj
Upraviť
#8614227

Z hľadiska efektívnosti sa vám nepodarí poraziť

s.translate(None, string.punctuation)

Pre vyššie verzie Pythonu použite nasledujúci kód:

s.translate(str.maketrans('', '', string.punctuation))

Vykonáva operácie so surovými reťazcami v jazyku C pomocou vyhľadávacej tabuľky - nie je toho veľa, čo by to prekonalo, ale napíšte si vlastný kód v jazyku C.

Ak vás netrápi rýchlosť, je tu iná možnosť:

exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)

Je to rýchlejšie ako s.replace s každým znakom, ale nebude to fungovať tak dobre ako iné ako pythonovské prístupy, ako sú regexy alebo string.translate, ako môžete vidieť z nižšie uvedených časov. Pri tomto type problému sa oplatí robiť to na čo najnižšej úrovni.

Časovanie kódu:

import re, string, timeit

s = "string. With. Punctuation"
exclude = set(string.punctuation)
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))

def test_set(s):
    return ''.join(ch for ch in s if ch not in exclude)

def test_re(s):  # From Vinko's solution, with fix.
    return regex.sub('', s)

def test_trans(s):
    return s.translate(table, string.punctuation)

def test_repl(s):  # From S.Lott's solution
    for c in string.punctuation:
        s=s.replace(c,"")
    return s

print "sets      :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000)
print "regex     :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000)
print "translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000)
print "replace   :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000)

Výsledkom sú nasledujúce výsledky:

sets      : 19.8566138744
regex     : 6.86155414581
translate : 2.12455511093
replace   : 28.4436721802
Ashish Cherian
Ashish Cherian
Edited answer 5 marec 2019 в 9:07
818
0
 S.Lott
S.Lott
5 november 2008 в 5:41
2008-11-05T17:41:27+00:00
Viac na
Zdroj
Upraviť
#8614226

Zvyčajne používam niečo podobné:

>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
...     s= s.replace(c,"")
...
>>> s
'string With Punctuation'
27
0
Vinko Vrsalovic
Vinko Vrsalovic
5 november 2008 в 5:39
2008-11-05T17:39:55+00:00
Viac na
Zdroj
Upraviť
#8614225

Nie nevyhnutne jednoduchšie, ale iným spôsobom, ak ste lepšie oboznámení s rodinou re.

import re, string
s = "string. With. Punctuation?" # Sample string 
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
Vinko Vrsalovic
Vinko Vrsalovic
Edited answer 5 november 2008 в 11:20
21
0
Pridať otázku
Kategórie
Všetky
Technológia
Kultúra / Rekreácia
Život / Umenie
Veda
Profesionálne
Obchod
Používatelia
Všetky
New
Popular
1
365
Registered pred dňom
2
True Image
Registered pred dňom
3
archana agarwal
Registered pred 3 dňami
4
Maxim Zhilyaev
Registered pred 6 dňami
5
adambotsfford adambotsfford
Registered pred týždňom
BG
DE
EL
ES
FR
ID
IT
JA
KO
NL
PT
RU
SK
ZH
© de-vraag 2022
Zdroj
stackoverflow.com
na základe licencie cc by-sa 3.0 s uvedením autora