Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何从字符串中删除标点符号并记住标点符号在字符串中的位置_String_Python 3.x_If Statement_For Loop_Dictionary - Fatal编程技术网

String 如何从字符串中删除标点符号并记住标点符号在字符串中的位置

String 如何从字符串中删除标点符号并记住标点符号在字符串中的位置,string,python-3.x,if-statement,for-loop,dictionary,String,Python 3.x,If Statement,For Loop,Dictionary,您好,我一直在尝试创建一个程序,该程序获取一个字符串并删除所有标点符号和大写字母,然后该程序应将所有标点符号和大写字母插入到句子的起始位置 这是我到目前为止得到的 sentence = 'I am called bob. What is your name?' punc = ('!', '"', '£', '$', '%', '^', '&', '*', '(', ')', '¬', '`', '{', '}', '~', '@', ':', '?', '>', '<', '

您好,我一直在尝试创建一个程序,该程序获取一个字符串并删除所有标点符号和大写字母,然后该程序应将所有标点符号和大写字母插入到句子的起始位置

这是我到目前为止得到的

sentence = 'I am called bob. What is your name?'
punc = ('!', '"', '£', '$', '%', '^', '&', '*', '(', ')', '¬', '`', '{', '}', '~', '@', ':', '?', '>', '<', ',', '.', '/', ';', '#', ']', '[', '/', '*')

Dictionary = {}
count = 0

for i in sentence:
    count = count + 1
    if i == punc:
        Dictionary[i] = count 

print(Dictionary)
我叫鲍勃。你叫什么名字?”
punc=(“!”、“”、“*”、“、”、“*”、“(“、”)、“、”、“、”、“、”、“、”、“{”、”}“,”、“~”、“@'、“:”、“?”、“>”、“字符串是不可变的,因此没有插入或删除方法。但是,您可以将其更改为肯定是可变的列表。我可能会有一个以标点符号为键的字典和每个索引的列表。您可能遇到的问题是,如果您有多个标点符号,则没有索引确保它们将以正确的顺序插入。例如:

sentence = 'I am called bob. What is your name?'
punc = ('!', '"', '£', '$', '%', '^', '&', '*', '(', ')', '¬', '`', '{', '}', '~', '@', ':', '?', '>', '<', ',', '.', '/', ';', '#', ']', '[', '/', '*')

sentence = list(sentence)
Dictionary = {}

for i, p in enumerate(sentence): # enumerate() returns an iterable in (index, value) format
    if p in punc:
        if p in Dictionary:
            Dictionary[p].append(i)
        else:
            Dictionary[p] = [i]

print(Dictionary) # => {'?': [34], '.': [15]}
这显然是不正确的。唯一可靠的方法是从最低的元素到最高的元素遍历dict,并以这种方式添加它们

最终代码:

original = sentence = 'I? am. cal?led ,bob. Wh,at. is your .name?.'
punc = ('!', '"', '£', '$', '%', '^', '&', '*', '(', ')', '¬', '`', '{', '}', '~', '@', ':', '?', '>', '<', ',', '.', '/', ';', '#', ']', '[', '/', '*')

sentence = list(sentence)
Dictionary = {}
seq = [] # list of all indices with any punctuation 

for i, p in enumerate(sentence):
    if p in punc:
        seq.append(i)
        if p in Dictionary:
            Dictionary[p].append(i)
        else:
            Dictionary[p] = [i]

sentence = list(filter(lambda x: x not in punc, sentence))
for i in seq:
    for key, indices in Dictionary.items():
        if i in indices:
            sentence.insert(i, key)
            indices.remove(i)
assert(''.join(sentence) == original)
original=句子='I?am.cal?led,bob.Wh,at.是你的.name?'

punc=(“!”、“”、“*”、“、”、“*”、“(“、”)、“、”、“、”、“、”、“、”、“{”、”}“,”、“~”、“@”、“:”、“?”、“>”,“拿着字符串,随便用它,然后扔掉那根字符串,再使用原来的那根?你为什么要在去掉它后重新插入这些东西?这是学校的一项愚蠢的任务谢谢你,这正是我需要的。我如何去掉标点符号和大写字母,然后重新插入它们呢
original = sentence = 'I? am. cal?led ,bob. Wh,at. is your .name?.'
punc = ('!', '"', '£', '$', '%', '^', '&', '*', '(', ')', '¬', '`', '{', '}', '~', '@', ':', '?', '>', '<', ',', '.', '/', ';', '#', ']', '[', '/', '*')

sentence = list(sentence)
Dictionary = {}
seq = [] # list of all indices with any punctuation 

for i, p in enumerate(sentence):
    if p in punc:
        seq.append(i)
        if p in Dictionary:
            Dictionary[p].append(i)
        else:
            Dictionary[p] = [i]

sentence = list(filter(lambda x: x not in punc, sentence))
for i in seq:
    for key, indices in Dictionary.items():
        if i in indices:
            sentence.insert(i, key)
            indices.remove(i)
assert(''.join(sentence) == original)