【问题标题】:List index splitting列表索引拆分
【发布时间】:2016-09-01 23:57:34
【问题描述】:

我正在尝试使用split() 方法返回列表索引的句子长度<=。如果我输入某个索引的确切长度,这可行,但< 似乎没有做任何事情。这是我的代码:

stories = [['With bloody hands, I say good-bye.'],
['TIME MACHINE REACHES                FUTURE!!! ... nobody there ...'],
["Not In My Job Description: Make sure it's done by the end of the day Jones.\nBut, sir, it's not in my ....\nJust do it, and remember, no blood."]]

def len_sentence():
    search = int(input("Enter int"))
    for i in stories:
        len1 = (i[0][0:].split(' '))
        if len(len1) <= search:
            print(i)
len_sentence()

整数 1-5 的用户输入不返回任何内容。如果我将&lt;= 替换为&gt;=,则可以。为什么&lt; 什么都不做?

【问题讨论】:

    标签: python list python-3.x split python-3.5


    【解决方案1】:

    您尝试的输入太小。您拥有的最短的故事有 6 个字长,因此输入 1-5 不会匹配任何内容。代码对我来说看起来不错,它只是您的搜索输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2021-10-02
      • 2018-01-08
      相关资源
      最近更新 更多