English 中文(简体)
如何在没有使用在功能中建造的设施的情况下,取代插图清单中的第一级事件
原标题:how can I replace the nth occurrences in a list of string without using the built in function
  • 时间:2010-12-05 13:57:19
  •  标签:
  • python

字典中,我有新鲜的字眼,我有问题要写一份文字,其中含有四个要素(图形、替换、替换和n)的字眼特征,并取代 occurrence。

例:

>>> replaeceit("Mississippi", "s", "l", 2)
 Mislissippi 
>>> replaeceit("Mississippi", "s", "l", 0)
 Mississippi 

第2条就是这样,法典将第2条改为第1条。

坦率地说,我不知道如何执行这一公式,这是我迄今为止没有一 n的法典。

def replaceit(str,replacefrom,replaceto):
    new=""
    for letter in str:
        if letter== replacefrom:
            new=new+replaceto
        else: 
            new=new+letter
    return new
问题回答

奥凯,现在也许我知道你正在寻求:

def replaceit(st, remove, put, pos):
outs = ""
count = 0
for letter in st:
    if letter == remove:
        count += 1
        if count == pos:
            outs += put
        else:
            outs += letter
    else:
        outs += letter
return outs

产出:

In [84]: replaceit("Mississipi", "s", "l", 2)
Out[84]:  Mislissipi 

当然,你可以核实,第2和第3号论点是用第1号的透镜进行的。

This is second first attempt at understanding your question:

def replaceit(s, replacefrom, replaceto, n):
  new_s, count =   , 0
  for letter in s:
    if letter == replacefrom:
      count += 1
      if count == n:
        new_s += replaceto
        continue
    new_s += letter
  return new_s

这符合你的例子:

>>> replaceit("Mississippi", "s", "l", 2)
 Mislissippi 
>>> replaceit("Mississippi", "s", "l", 0)
 Mississippi 

如果这不是你想要的话,请作更好的解释。

您也可以定期表达:

def replaceit(s, replacefrom, replaceto, n):
  import re
  if n <= 0:
    return s
  return re.sub( (.*?%s)%s  % (( %s.*?  % replacefrom) * (n-1), replacefrom), r 1%s  % replaceto, s)

每个人都总是热爱产生者的言论。

from itertools import count
def replaceit(str, replacefrom, replaceto, n):
    c = count(1)
    return   .join(replacefrom if l == replaceto and c.next() == n else l for l in str)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签