如何将列表插入列表列表? [英] How do I insert a list into a list of lists?

查看:116
本文介绍了如何将列表插入列表列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我假装我有



记录= [[狗,亚历克斯,大],[猫,班尼,中等],[鱼,认真,小]]



根据动物的名字,这些记录按字母顺序排列。



我怎么样?在保持字母顺序的同时插入新的动物记录?

let's pretend I have

records = [[dog, Alex, big], [cat, Benny, medium], [fish, Earnest, small]]

This list of records is in alphabetical order according to the animals' names.

How would I insert a new animal record while keeping alphabetical order?

推荐答案

很明显:按照 Richard 的建议,迭代列表直到找到第一个条目必须按字母顺序跟随候选人(您也可以点击列表的末尾),然后在找到的条目之前插入候选人。

如果您的列表很大,那么您可以实现二进制搜索 [ ^ ]。
It is pretty obvious: as suggested by Richard, iterate the list until you find the first entry which must follow the candidate in alphabetic order (you may also hit the end of the list) and then insert the candidate just before the found entry.
If your list is big then you could implement a binary search[^] for the purpose.


这篇关于如何将列表插入列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆