0

我有一项评估要求我在列表中交换单词。我给出了一个 txt 文件,其中包含一列“旧词”和一列“新词”。我需要定义一个函数来检查字符串以查看“旧单词”列表/列中的单词是否在字符串中,然后我需要将该单词与“新单词”列中的相应单词交换。

例如:

两列单词中的第一行:['We, 'You']。

字符串: “我们和我们的猫一起参加了婚礼” 输出: “你和你的猫一起参加了婚礼”

给定的 txt 文件包含两列,因此在编写了一些代码后,我设法将单词拆分为某些列表,有一个名为 'old_word_list' 的列表包含所有单词的单个字符串,这些单词将/可以在字符串和一个名为“new_word_list”的列表,其中包含用于替换旧单词的单词。

伪代码概念: 如果字符串包含 old_word_list 中的任何单词,则将 word/s 替换为 new_word_list 中相同(对应)索引的单词。

这是我唯一坚持的评估部分,如果有人可以帮助我,我将不胜感激,如果我遗漏了任何需要的信息,请发表评论。谢谢你。

完整代码:

# Declaring a global variables for the file, so it can be used in the code.
filename = "reflection.txt"
the_file = open(filename)

# Declaring any other reqiured variables.
word_list = []
old_word_list = []
new_word_list = []

# Creating loop to add all words to a list. 
for line in the_file:

    # Appends each line to the end of the list declared above. In appending
    # the lines, the code also removes the last character on each line (/n).
    word_list.append(line[:-1])

# Creating a loop to split each individual word, then appends the
# old/original words to a declared list, and appends the new words
# to a declared list.
for index in range(len(word_list)):
    temp_word = word_list[index]
    split_words = temp_word.split()
    old_word_list.append(split_words[0])
    new_word_list.append(split_words[1])

# Defining a function to produce altered statement.
def reflect_statement(statement):

    # Swaps the old word with the new word.
    for i in range(len(old_word_list)):
        if old_word_list[i] in statement:
             statement.replace(old_word_list[i], new_word_list[i])

    # Replaces '.' and '!' with '?'    
    for index in range(list_length):
        if old_word_list[index] in statement:
            statment = statement.replace(old_word_list[index], \
                                         new_word_list[index])

    statement = statement.replace(".", "?")
    statement = statement.replace("!", "?")

    # Returns result statement.
    return statement.
4

5 回答 5

3

您可以使用以下代码:

# the words to be replaced
old_word_list=['We','cat']

# the new words
new_word_list=['You','dog']

my_string="We went to a wedding with our cat"

# it prints "We went to a wedding with our cat"
print my_string

# iterate over the list of old words
for word in old_word_list:
  # get the index of the current word
  index = old_word_list.index(word)

  # use this index to do the replacement with the words of the two lists
  my_string = my_string.replace(old_word_list[index], new_word_list[index])

# it prints "You went to a wedding with our dog"
print my_string

根据作者的评论更新答案:

# the words to be replaced
old_word_list=['We','cat']

# the new words
new_word_list=['You','dog']

def reflect_statement(statement):
  # iterate over the list of old words
  for word in old_word_list:
    # get the index of the current word
    index = old_word_list.index(word)

    # use this index to do the replacement with the words of the two lists
    statement = statement.replace(old_word_list[index], new_word_list[index])

  return statement

mystring="We went to a wedding with our cat"
mystring = reflect_statement(mystring)
print mystring

作者最后添加源代码后更新的答案:

将您的功能更改为此:

def reflect_statement(statement, old_word_list, new_word_list):
  mystr=statement
  # iterate over the list of old words
  for word in old_word_list:
    # get the index of the current word
    index = old_word_list.index(word)

    # use this index to do the replacement with the words of the two lists
    mystr = mystr.replace(old_word_list[index], new_word_list[index])

  return mystr
于 2012-04-11T11:33:38.253 回答
1

提示:

  1. "string string".split()# 将空格上的字符串拆分为列表
  2. 您可以使用 for 循环遍历列表
  3. 列表有一个myList.index(element)告诉你一个元素是否在列表中并返回它的索引。
  4. 索引将在您的两个列表之间匹配(根据您的描述)
  5. 如果你想走那条路,字符串也有一个“myString.replace(old, new)”方法。
于 2012-04-11T11:12:39.213 回答
0
# map old to new words (assuming there are no duplicates)
replacement_mapping = dict(zip(old_word_list, new_word_list))

# go through each word of the string, if it's in the mapping, use replacement, otherwise the word from the string
new_string = ' '.join([replacement_mapping.get(word, word) for word in given_string.split(' ')])
于 2012-04-11T11:17:52.610 回答
0

在发布我的答案之前,我将陈述我所做的一些假设,所以如果其中任何一个错误,请纠正我。

假设1:每列有相同数量的单词假设2:每个单词只有一个“伙伴”,形成一个单词对

在这种情况下,我会将每个单词对单独设为一个列表。然后,为了继续你的伪代码,对于每一行文本,检查每个单词对并将 word[0] 替换为 word[1]。

编辑抱歉刚刚发现我的措辞模棱两可。在制作单词对列表时,我也会将它们存储在一个列表中,例如[["old1", "new1"], ["old2", "new2"]]然后运行你的循环来替换它们(我现在没有给出示例代码,因为我知道你正在做一个作业,所以可能想要解决问题,但如果您遇到困难,请告诉我)。

于 2012-04-11T11:06:31.620 回答
0

如果我理解你的话,可能是这样,但它并不漂亮:

for i in range( len(old_word_list) )
    if old_word_list[i] in "some string":
         "some string".replace(old_word_list[i], new_word_list[i])
于 2012-04-11T11:09:29.697 回答