English 中文(简体)
I m 撰写笔记检查方案,我如何用笔记取代ch?
原标题:I m writing a spellchecking program, how do I replace ch in a string?

我做了什么错误/什么?

import sys
import string

def remove(file):
    punctuation = string.punctuation
    for ch in file:
        if len(ch) > 1:
            print( error - ch is larger than 1 --| {0} |-- .format(ch))
        if ch in punctuation:
            ch =    
            return ch
        else:
            return ch

ref = (open("ref.txt","r"))
test_file = (open("test.txt", "r"))

dictionary = ref.read().split()
file = test_file.read().lower()
file = remove(file)
print(file)

页: 1

问题回答

该法典:

for ch in file:
        if len(ch) > 1:

斜体表示的<代码>>>>>>(除打破不藏有你自己的识别特征的封面名称的最佳做法外)不是档案,而是显示——这是指三条code,但与下述事实并无任何区别:该 lo体是返回单一特性(单编码特性,不是由tes星,而是由Sharma 3)这样<编码>len(ch)= 1,是绝对受“灰色语言”规则保障的。 我不肯定你在尝试完成这一试验时会做些什么(排除了单一编码特性的某些子集?),但是,不管怎样,is>?

除此之外,你还回来——因此立即撤出——从而退出职能,只恢复一种功能(档案中的第一个功能,如果第一个空间是图形特征的话)。

关于使用<代码>translate方法的建议,我在另一项答复中认为是正确的,但回答使用了“translate的错误版本。 (其中一项适用于星座,not,适用于你需要的单条形体。) 适当的单编码版本比较简单,把你整个职能变成仅两份声明:

trans = dict.fromkeys(map(ord, string.punctuation),    )
return file.translate(trans)

简言之,扼杀是不可调和的,因此,你需要为你们的变革创造新的动力。

这样做有几个途径:

其中一人正在使用核对清单,以检查特征,只返回不进行校对。

def remove(file):
  return   .join(ch for ch in file if ch not in string.punctuation)

您还可以要求履行功能,测试你可能放弃“超级特性”例外的性质,或发挥某种其他功能:

def remove(file):
  return   .join(TranslateCh(ch) for ch in file if CheckCh(ch))

另一个替代办法是string , 提供replacetranslate。 翻译提供了一种 n(比制定清单更为有效)的机制,见Alex的答复。

或... 您可在forloop上收集一份名单,并在结尾处加入该名单,但该名单只字不提。

检查再版(经常表述)模块。 它具有“子”功能,可以取代与定期表述相匹配的插图。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签