【问题标题】:MIN function in python - why am i getting this error?python中的MIN函数-为什么会出现此错误?
【发布时间】:2019-10-28 00:09:02
【问题描述】:

我收到错误消息:min() argument is an empty sequence

我正在尝试从 C 列表中获取具有最小长度值的单词。

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        c=[]
        z=0

        for i in range(1,len(strs)):
            for j in strs[i]:

                for x in strs[0]:
                    if strs[i][0] != strs[0][0]:
                        return ""
                    if j == x:
                        z+=1

            c.append(strs[0][0:z])
            z=0

        return min(c,key=len) 

【问题讨论】:

  • 当您创建的列表c 为空时,您将收到该错误。
  • 因为。在某些情况下,您会过早地从函数中返回!因此,在这种情况下,您的返回值变为“”,或者根据您的代码为空字符串
  • 嗯。谢谢。我试图将此代码提交到 leetcode 网站,但似乎他们为此问题输入了一个空列表。

标签: python python-3.x min


【解决方案1】:

不确定您要做什么,但这可能会有所帮助:

def longestCommonPrefix(self, strs: [str]) -> str:
c = []
z = 0

for i in range(1, len(strs)):
    for j in strs[i]:

        for x in strs[0]:
            if strs[i][0] != strs[0][0]:
                # return ""
                print("")
            if j == x:
                z += 1

    c.append(strs[0][0:z])
    z = 0
return min(c, key=len)


a = longestCommonPrefix(['f', 'fo', 'foo'], ['foob', 'fooba', 'foobar'])
print(a)

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2020-02-05
    • 2021-08-04
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多