字符串和列表的问题(搜索和替换) [英] Problems with string and lists (searching and replaceing)

查看:67
本文介绍了字符串和列表的问题(搜索和替换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串中搜索某些字符,当它们找到
时,我想替换其他字符串中的其他字符,这些字符位于

相同的位置(对于一个非常简单的策划游戏)我的学生。


这个非常简单的事情看起来并不简单。


如果我使用字符串,我不能替换他们的部分(虽然我可以使用

string.find进行搜索)。我认为字符串不是可变的是一个坏主意,但我怀疑这已经讨论了很久了。


我可以使用序列代替,但首先我必须''拆分''和''加入''。

此外,没有''找到'序列(谁知道为什么不),所以

我可以选择使用''index''引发异常(我们还有

未涵盖的异常)或者我可以询问该角色是否在
中在使用''index'之前
字符串,这对于我的学生来说是有点人为的。 (对我来说没问题。)


我有什么监督吗?


TIA,

JB


---- ==通过Newsfeed.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeed.com 世界排名第一的新闻组服务! > 100,000新闻组

--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---

I should like to search certain characters in a string and when they are
found, I want to replace other characters in other strings that are at
the same position (for a very simply mastermind game) for my pupils.

This very simple thing does not seem simple at all.

If I use strings, I cannot replace their parts (though I can use
string.find for the searching). I think it is a bad idea that strings are
not mutable, but I suspect that this has been discussed here for ages.

I can use sequences instead, but then first I have to ''split'' and ''join''.
Additionally, there is no ''find'' for sequences (who knows why not) and so
I can choose between using ''index'' that raises an exception (and we have
not covered exceptions yet) or I can ask whether the character is in the
string before using ''index'' which is a bit artificial from the point of
view of my pupils. (It is all right with me.)

Do I oversee something?

TIA,
JB

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

推荐答案

jblazi< jb **** @ hotmail.com>写道:
jblazi <jb****@hotmail.com> wrote:
我想搜索字符串中的某些字符,当找到它们时,我想替换其他字符串中的其他字符
对于我的学生来说,这个位置(对于一个非常简单的策划游戏)。

这个非常简单的事情看起来并不简单。

如果我使用字符串,我无法取代他们的部分(虽然我可以使用
string.find进行搜索)。我认为字符串不可变是一个坏主意,但我怀疑这已经讨论了很多年了。

我可以使用序列代替,但首先我必须' 分裂和加入。
另外,序列没有找到(谁知道为什么不这样做)所以
我可以选择使用''index''引发异常(我们还没有覆盖异常)或者我可以在使用''index'之前询问该字符是否在
字符串中,这有点人为的从
我的学生的看法。 (对我来说没关系。)

我可以监督吗?
I should like to search certain characters in a string and when they are
found, I want to replace other characters in other strings that are at
the same position (for a very simply mastermind game) for my pupils.

This very simple thing does not seem simple at all.

If I use strings, I cannot replace their parts (though I can use
string.find for the searching). I think it is a bad idea that strings are
not mutable, but I suspect that this has been discussed here for ages.

I can use sequences instead, but then first I have to ''split'' and ''join''.
Additionally, there is no ''find'' for sequences (who knows why not) and so
I can choose between using ''index'' that raises an exception (and we have
not covered exceptions yet) or I can ask whether the character is in the
string before using ''index'' which is a bit artificial from the point of
view of my pupils. (It is all right with me.)

Do I oversee something?




是的,UserString。文档有点稀疏,但阅读

模块本身提供了额外的信息。下面是一个快速测试

脚本。


HTH,


Anton

来自UserString导入MutableString

def test():

s = MutableString(" helo world")

print s

x =''helo''

y ='''你好''

i = s.find(x)

s [ i:i + len(x)] = y

print s


如果__name __ ==''__ main__'':

测试()


#output:

#helo world

#hello world



Yes, UserString. The documentation is a bit sparse, but reading the
module itself provides additional information. Below is a quick test
script.

HTH,

Anton
from UserString import MutableString

def test():
s = MutableString("helo world")
print s
x = ''helo''
y = ''hello''
i = s.find(x)
s[i:i+len(x)] = y
print s

if __name__==''__main__'':
test()

#output:
#helo world
#hello world


我不确定这是不是你想要的,但似乎你可以通过作业解决它:


& gt; &安培; GT;&安培; GT; x =''test''

& gt;& gt;& gt; y =''yummy''

& gt;& gt;& gt; a = x.index(''e'')

& gt;& gt;& gt; y = y [:a] +''a''+ y [a + 1:]

& gt;& gt;& gt;你需要为索引方法添加try / except ValueError。$ / b
-shuhsien


我想搜索字符串中的某些字符,当它们被找到时,我想替换其他字符串中的其他字符位于同一位置(对于一个非常简单的策划游戏)我的学生。这个非常简单的事情看起来并不简单。如果我使用字符串,我不能替换他们的部分(虽然我可以使用string.find进行搜索)。我认为字符串不可变是一个坏主意,但我怀疑这已经讨论了很久了。我可以使用序列,但首先我必须''拆分''和''加入''。另外,序列没有找到(谁知道为什么不这样),所以我可以选择使用引起异常的索引(我们还没有涵盖异常)或者我可以问这个角色在使用''index''之前是在字符串中,从我的学生的角度来看这有点人为。 (对我来说没问题。)我是否监督某事?



I am not sure if this is what you want, but it seems that you can get around with it by assignments:

&gt;&gt;&gt; x = ''test''
&gt;&gt;&gt; y = ''yummy''
&gt;&gt;&gt; a = x.index(''e'')
&gt;&gt;&gt; y = y[:a] + ''a''+ y[a+1:]
&gt;&gt;&gt; y
''yammy''

you can add try/except ValueError for the index method.

-shuhsien


I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game) for my pupils. This very simple thing does not seem simple at all. If I use strings, I cannot replace their parts (though I can use string.find for the searching). I think it is a bad idea that strings are not mutable, but I suspect that this has been discussed here for ages. I can use sequences instead, but then first I have to ''split'' and ''join''. Additionally, there is no ''find'' for sequences (who knows why not) and so I can choose between using ''index'' that raises an exception (and we have not covered exceptions yet) or I can ask whether the character is in the string before using ''index'' which is a bit artificial from the point of view of my pupils. (It is all right with me.) Do I oversee something?




" Anton Vredegoor" <一个*** @ vredegoor.doge.nl> schrieb im Newsbeitrag

news:bk ********** @ news.hccnet.nl ...
"Anton Vredegoor" <an***@vredegoor.doge.nl> schrieb im Newsbeitrag
news:bk**********@news.hccnet.nl...
Do我监督什么?



是的,UserString。文档有点稀疏,但阅读
模块本身提供了额外的信息。下面是一个快速测试的脚本。

来自UserString import MutableString

def test():
s = MutableString(" helo world")
打印s
x =''helo''
y =''你好''
我= s.find(x)
s [i:i + len(x)] = y
打印s
如果__name __ ==''__ main__'':
test()

#output:
#helo world
#hello world



Yes, UserString. The documentation is a bit sparse, but reading the
module itself provides additional information. Below is a quick test
script.

from UserString import MutableString

def test():
s = MutableString("helo world")
print s
x = ''helo''
y = ''hello''
i = s.find(x)
s[i:i+len(x)] = y
print s

if __name__==''__main__'':
test()

#output:
#helo world
#hello world




Thx。

解释这个akward程序是非常困难的初学者。

特别是当他们看到这可以在C中完成时这样做。

似乎我偶然发现Python中的少数情况之一

既麻烦又难以使用。


JB



Thx.
It would be quite diffcult to explain this akward procedure to beginners.
especially when they see that this can be done so simply in C.
It seems that I have stumbled upon one of the few cases when Python is
cumbersome and difficult to use.

JB


这篇关于字符串和列表的问题(搜索和替换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆