0

これは私が持っている文字列のリストです:

 [
  ['It', 'was', 'the', 'besst', 'of', 'times,'], 
  ['it', 'was', 'teh', 'worst', 'of', 'times']
 ]

times,句読点を, to be'times',','
または別の例で分割する必要がWhy?!?ある場合は、 be が必要です'Why','?!?'

import string

def punctuation(string):

for word in string:
    if word contains (string.punctuation):
        word.split()

私はそれがPython言語ではないことを知っています! しかし、それは私がやりたいことです。

4

4 に答える 4

3

finditer文字列がより複雑な場合でも使用できます。

    >>> r = re.compile(r"(\w+)(["+string.punctuation+"]*)")
    >>> s = 'Why?!?Why?*Why'
    >>> [x.groups() for x in r.finditer(s)]
    [('Why', '?!?'), ('Why', '?*'), ('Why', '')]
    >>> 
于 2013-07-16T16:45:06.440 に答える
0

正規表現を使用しないジェネレーター ソリューション:

import string
from itertools import takewhile, dropwhile

def splitp(s):
    not_punc = lambda c: c in string.ascii_letters+"'"  # won't split "don't"
    for w in s:
        punc = ''.join(dropwhile(not_punc, w))
        if punc:
            yield ''.join(takewhile(not_punc, w))
            yield punc
        else:
            yield w

list(splitp(s))
于 2013-07-16T17:48:36.210 に答える