Python 更换编号+;字母数字元素列表中的单词

Python 更换编号+;字母数字元素列表中的单词,python,python-3.x,string,list,Python,Python 3.x,String,List,我一直在努力解决这个问题,如果有人能帮助我,我会很高兴的。 从包含单词和数字的字符串开始,我要做的是查找组合“digit+单词“minutes”的实例,并将其替换为“only”+digit/2 “我在60分钟内跑完马拉松”“--期望输出-->“我在仅30分钟内跑完马拉松”” 这应该适用于包含“digit+单词“minutes”实例的任何其他句子 我所尝试的: sent = "I run the marathon in 60 minutes" #example sentence

我一直在努力解决这个问题,如果有人能帮助我,我会很高兴的。 从包含单词和数字的字符串开始,我要做的是查找组合“digit+单词“minutes”的实例,并将其替换为“only”+digit/2

“我在60分钟内跑完马拉松”“--期望输出-->“我在仅30分钟内跑完马拉松”

这应该适用于包含“digit+单词“minutes”实例的任何其他句子

我所尝试的:

sent = "I run the marathon in 60 minutes" #example sentence, a string
sent_list = review.split() #convert to list

for i in range(len(sent_list)):
        if i.isdigit() and sent_list[i+1] == 'minutes': #find digits that are followed by 'minutes'
            sent_list[i] = i/2 #replace with its half
            sent_list.insert([i]-1,'only') #add only before the digit
我已经尝试过这段代码的其他小变化,但我真的被卡住了。这是我得到的错误:

AttributeError: 'int' object has no attribute 'isdigit'

您正在使用i作为循环计数器对列表进行迭代。我将是你的一个号码。你想做的是

sent = "I run the marathon in 60 minutes"
sent_list = review.split() #convert to list

for i in range(len(sent_list)):
        if sent_list[i].isdigit() and sent_list[i+1] == 'minutes': #find digits that are followed by 'minutes'
            sent_list[i] = i/2 #replace with its half
            sent_list.insert([i]-1,'only') #add only before the digit

像这样修改代码

sent = "I run the marathon in 60 minutes" 
sent_list = sent.split() 

for i in range(len(sent_list)):
        print("i",sent_list[i])
        if sent_list[i].isdigit() and sent_list[i+1] == 'minutes':
            n = int(sent_list[i])/2
            sent_list.remove(sent_list[i])           
            break
sent_list.insert(i,int(n))
sent_list.insert(i,'only')
print(sent_list)
然后将列表转换为字符串

sent = " ".join(str(x) for x in sent_list)
输出:

I run the marathon in only 30 minutes'
  • sent\u list=sent.split()
  • i.isdigit()
    将尝试检查循环索引是否为数字。这里您真正想要的是
    发送列表[i].isdigit()
  • sent\u list[i]=int(sent\u list[i])//2
    这是正确获取原始值一半的方法
  • 迭代时不应将元素插入列表
  • 一些问题:

    • 您有一个错误的变量引用
      review
    • i
      是索引,而不是单词,所以做
      i.isdigit()
      是在测试错误的东西。出于同样的原因:
      i/2
      使用了错误的东西。你想要的是单词,而不是索引
    • 您不应该转到最后一个条目,因为
      i+1
      将是列表中不存在的索引
    • 您不希望在
      i-1
      处插入“only”,而是在
      i
      处插入
    • 插入一个单词,意味着您的下一次迭代将再次访问同一个单词,这将导致重复插入“only”
    • 最后,您得到的是一个列表,而不是一个字符串,因此您仍然需要将这些片段重新连接到一个字符串中
    考虑到这些要点:

    sent_list = sent.split()
    
    # Get both the index and the corresponding word. 
    # + Exclude the last word.
    # + Don't iterate the list itself, but a copy 
    for i, word in enumerate(sent_list[:-1]):
        if word.isdigit() and sent_list[i+1] == 'minutes':
            sent_list[i] = str(int(word)//2)
            sent_list.insert(i,'only')
    
    print(" ".join(sent_list))
    
    也可以使用正则表达式执行此操作:

    import re
    result = re.sub(r"(\d+)(\s+minutes)", 
                    lambda m: "only {}{}".format(int(m.group(1))//2, m.group(2)), 
                    sent)
    print(result)
    

    你已经收到了好几个答案。现在你的任务到了。非常感谢!这不仅奏效,而且让我明白我错在哪里了。非常感谢您抽出时间。:)谢谢你的回答。在发布问题之前,我也尝试过这样做,但它不起作用:/