【问题标题】:nested for loop for spliting string based on multiple delimiters基于多个分隔符拆分字符串的嵌套for循环
【发布时间】:2012-01-23 07:38:42
【问题描述】:

我正在进行一项 Python 作业,该作业需要将文本分隔、排序和打印为:

  • 句子由.分隔
  • 短语,
  • 然后打印

到目前为止我做了什么:

text =  "what time of the day is it. i'm heading out to the ball park, with the kids, on this nice evening. are you willing to join me, on the walk to the park, tonight." 


for i, phrase in enumerate(text.split(',')):
   print('phrase #%d: %s' % (i+1,phrase)):


phrase #1: what time of the day is it. i'm heading out to the ball park
phrase #2:  with the kids
phrase #3:  on this nice evening. are you willing to join me
phrase #4:  on the walk to the park
phrase #5:  tonight.

我知道需要一个嵌套的 for 循环并尝试过:

for s, sentence in enumerate(text.split('.')):
     for p, phrase in enumerate(text.split(',')):
         print('sentence #%d:','phrase #%d: %s' %(s+1,p+1,len(sentence),phrase)) 

TypeError: not all arguments converted during string formatting

欢迎提供提示和/或简单示例。

【问题讨论】:

  • 专有代词“I”拼写为“I”。提及自己时,请使用“我”。总是。

标签: python string list for-loop


【解决方案1】:

你可能想要:

'sentence #%d:\nphrase #%d: %d %s\n' %(s+1,p+1,len(sentence),phrase)

并且在内部循环中,您当然要拆分sentence,而不是再次拆分text

【讨论】:

    【解决方案2】:

    TypeError:字符串格式化期间并非所有参数都转换

    是一个提示。

    你的循环很好。

    'sentence #%d:','phrase #%d: %s' %(s+1,p+1,len(sentence),phrase) 
    

    错了。

    计算%d%s 转换规范。计算%运算符/之后的值

    数字不一样,是吗?那是TypeError

    【讨论】:

      【解决方案3】:

      您的代码 sn-p 存在几个问题

      for s, sentence in enumerate(text.split('.')):
           for p, phrase in enumerate(text.split(',')):
               print('sentence #%d:','phrase #%d: %s' %(s+1,p+1,len(sentence),phrase)) 
      
      1. 如果我理解正确,您想用分隔的. 分割句子。然后,您希望将这些句子中的每一个拆分为再次由, 分隔的短语。因此,您的第二行实际上应该拆分外部循环枚举的输出。类似的东西

        for p, phrase in enumerate(sentence.split(',')):
        
      2. 打印声明。如果您曾经看到类似TypeError 的错误,您可以确定您正在尝试将一种类型的变量分配给另一种类型。嗯,但是没有作业?它是对打印连接的间接分配。您对打印的承诺是,您将提供 3 个参数,其中前两个是 Integers(%d),最后一个是 string(%d)。但是您最终提供了 3 个 Integers (s+1, p+1, len(sentence),phrase) 这与您的打印格式说明符不一致。要么你删除第三个参数(len(sentence))像

        print('sentence #%d:, phrase #%d: %s' %(s+1,p+1,phrase)) 
        

        或在打印语句中再添加一个格式说明符

        print('sentence #%d:, phrase #%d:, length #%d, %s' %(s+1,p+1,len(sentence),phrase)) 
        

      假设你想要前者想要,那就让我们去

      for s, sentence in enumerate(text.split('.')):
           for p, phrase in enumerate(text.split(',')):
               print('sentence #%d:, phrase #%d:, length #%d, %s' %(s+1,p+1,len(sentence),phrase)) 
      

      【讨论】:

      • 非常感谢,我设法根据所提供的见解制作了正确的显示顺序。
      • @Abhijit 我更喜欢 S.Lott 风格来回答家庭作业问题。见meta
      【解决方案4】:
      >>> sen = [words[1] for words in enumerate(text.split(". "))]
      >>> for each in sen: each.split(", ")
      ['what time of the day is it']
      ["i'm heading out to the ball park", 'with the kids', 'on this nice evening']
      ['are you willing to join me', 'on the walk to the park', 'tonight.']
      

      您可以根据自己的喜好将此未分配的输出转换。

      【讨论】:

        猜你喜欢
        • 2011-11-28
        • 2012-02-27
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        • 2014-03-13
        • 1970-01-01
        相关资源
        最近更新 更多