Python列表列表中的最长列表;空列表支持

时间:2018-03-16 05:19:50

标签: python list

很多线程已经讨论过通过

返回列表列表中最长的列表
✗ git checkout -b testing heroku/master 
Branch testing set up to track remote branch master from heroku.
Switched to a new branch 'testing'
➜  fwdform2 git:(testing) ✗ echo "Garbage" > README.md 
➜  fwdform2 git:(testing) ✗ git add README.md 
➜  fwdform2 git:(testing) ✗ git commit -m "Non-functional changes"
[testing 7dd95cb] Non-functional changes
 1 file changed, 1 insertion(+), 275 deletions(-)
 rewrite README.md (100%)
➜  fwdform2 git:(testing) ✗ git push heroku testing:master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 288.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing dependencies with latest Pipenv…
remote:        Traceback (most recent call last):
remote:          File "/app/.heroku/python/bin/pipenv", line 11, in <module>
remote:            sys.exit(cli())
remote:          File "/tmp/build_ffaa2c482e67c8c4daeb9b3348ef030d/.heroku/python/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 722, in __call__
remote:            return self.main(*args, **kwargs)
remote:          File "/tmp/build_ffaa2c482e67c8c4daeb9b3348ef030d/.heroku/python/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 697, in main
remote:            rv = self.invoke(ctx)
remote:          File "/tmp/build_ffaa2c482e67c8c4daeb9b3348ef030d/.heroku/python/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 1063, in invoke
remote:            Command.invoke(self, ctx)
remote:          File "/tmp/build_ffaa2c482e67c8c4daeb9b3348ef030d/.heroku/python/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 895, in invoke
remote:            return ctx.invoke(self.callback, **ctx.params)
remote:          File "/tmp/build_ffaa2c482e67c8c4daeb9b3348ef030d/.heroku/python/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 535, in invoke
remote:            return callback(*args, **kwargs)
remote:          File "/tmp/build_ffaa2c482e67c8c4daeb9b3348ef030d/.heroku/python/lib/python3.6/site-packages/pipenv/vendor/click/decorators.py", line 17, in new_func
remote:            return f(get_current_context(), *args, **kwargs)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/pipenv/cli.py", line 62, in cli
remote:            from . import core
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/pipenv/core.py", line 30, in <module>
remote:            from requests.packages.urllib3.exceptions import InsecureRequestWarning
remote:        ImportError: cannot import name 'InsecureRequestWarning'
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !    Push rejected to fwdform2.
remote: 
To https://git.heroku.com/fwdform2.git
 ! [remote rejected] testing -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/fwdform2.git'

但如果lst_of_lsts是一个空列表,则会发出此消息。

max(sorted(lst_of_lsts, key = len))

是否有支持空列表的版本?它会返回一个空列表。

由于

3 个答案:

答案 0 :(得分:3)

您可以按[[]]添加列表:

max(lst_of_lsts + [[]], key = len)

答案 1 :(得分:2)

首先,您的原件不正确。 max不需要排序参数,即使您对其进行排序,仍然必须将key传递给max。这是一个例子,如果你不这样做:https://ideone.com/TMJyGK

我能想到的最简单的事情就是检查你的清单是否为空:

max(lst_of_lst if lst_of_lst else [[]], key=len)

OR

max(lst_of_lst, key=len) if lst_of_lst else []

答案 2 :(得分:1)

你想要这样的东西吗?

lst_of_lsts = [[], []]
lst = sorted(lst_of_lsts, key = len)[0]
if lst:
    print(max(lst))
else:
    print() #whatever