【问题标题】:Problem with using the def to create a function in python使用def在python中创建函数的问题
【发布时间】:2021-07-15 19:09:01
【问题描述】:

我正在尝试创建一个函数hasFinalLetter,它接受两个输入并创建strList 中所有以字母结尾的字符串的列表。 然后创建三个带有字符串的实际列表,以证明函数hasFinalLetter 有效,其中一个实际列表必须给出空输出。

def hasFinalLetter(strList, letters):
    strList2 = []
    for strings in strList:
        length = len(strings)
        lastLetterString = strings[length - 1]
    for lastLetter in letters:
        if lastLetterString == lastLetter:
            strList2.append(strings)
    return strList2

#This will return empty list
people = ["Isaac", "Mitchel", "Priscilla", "Michael"]
lettersForPeople = "bLA"

print(hasFinalLetter(people, lettersForPeople))

# Case 2
things = ["Cars", "Phone", "box", "laptop"]
lettersForThings = "sexe"
print(hasFinalLetter(things, lettersForThings))

# Case 3
things2 = ["Car", "Phone", "box", "Laptop"]
lettersForThings2 = "rexp"

print(hasFinalLetter(things2, lettersForThings2))

但是,我的定义中有一些东西使我的代码输出只是:

[]
[]
['Laptop']

虽然其他都是空白的,即使lettersForThings2 包含things2 中的所有最后一个单词。 我已经调试了很长时间,似乎无法找到问题的根源。

【问题讨论】:

  • 是的,我试过了,仍然给出相同的输出
  • 拜托了!使用has_final_letter 而不是hasFinalLetter,它根本不是pythonic
  • @DevLounge 在我的学校我们使用hasFinalLetter

标签: python list function


【解决方案1】:

请使用下面的代码。

   def hasFinalLetter(strList, letters):

      strList2 = [ ]
      lastLetterString = [ ]

      for strings, letter in zip(strList, letters):

          length = len(strings)

          lastLetterStr = strings[length-1]

          lastLetterString.append(lastLetterStr)

          if lastLetterStr == letter:
              strList2.append(strings)
      return strList2 


   # This will return empty list         
   people = ["Isaac", "Mitchel", "Priscilla", "Michael"]
   lettersForPeople = "bLA"

   print(hasFinalLetter(people, lettersForPeople))

   # Case 2
   things = ["Cars", "Phone", "box", "laptop"]
   lettersForThings = "sexe"
   print(hasFinalLetter(things, lettersForThings))

   # Case 3
   things2 = ["Car", "Phone", "box", "Laptop"]
   lettersForThings2 = "rexp"

   print(hasFinalLetter(things2, lettersForThings2))

输出:

  [ ]
  ['Cars', 'Phone', 'box' ]
  ['Car', 'Phone', 'box' , 'Laptop']

请重新定义你的函数,如上所示:

由于我是 Github 的新手,所以我尝试发送一张不起作用的图片,还尝试评论它也不起作用.....

您可以通过 kelvinlikel@gmail.com 亲自向我发送邮件 我得到了我相信你想要的确切输出

【讨论】:

  • 是的,代码有效。但是您创建了一个新列表 lastLetterString。你能解释一下为什么吗?
  • 对不起,我的朋友,它最初是为了测试目的。所以我可以打印它并找出输出是否是我想要的。请删除包含 lastLetterString 的两行谢谢我的朋友
【解决方案2】:

函数hasFinalLetter有错误的缩进:

def hasFinalLetter(strList, letters):
    strList2 = []
    for strings in strList:
        length = len(strings)
        lastLetterString = strings[length - 1]

        # bad indentation was here:
        for lastLetter in letters:
            if lastLetterString == lastLetter:
                strList2.append(strings)

    return strList2


# This will return empty list
people = ["Isaac", "Mitchel", "Priscilla", "Michael"]
lettersForPeople = "bLA"

print(hasFinalLetter(people, lettersForPeople))

# Case 2
things = ["Cars", "Phone", "box", "laptop"]
lettersForThings = "sexe"
print(hasFinalLetter(things, lettersForThings))

# Case 3
things2 = ["Car", "Phone", "box", "Laptop"]
lettersForThings2 = "rexp"

print(hasFinalLetter(things2, lettersForThings2))

打印:

[]
['Cars', 'Phone', 'Phone', 'box']
['Car', 'Phone', 'box', 'Laptop']

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2022-11-17
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多