如何生成Python中列表中尚未包含的随机ID?(How to generate a random ID that is not already in a list in Python?)

编程入门 行业动态 更新时间:2024-10-10 03:24:01
如何生成Python中列表中尚未包含的随机ID?(How to generate a random ID that is not already in a list in Python?)

我正在尝试为电影播放系统制作一个提示列表。

每次创建一个cue我都想在cue对象中添加一个id 。

(看起来像什么)

{ "name": "Cue One", "length": 5000, "id": xxx }

每个提示对象将存储在提示列表中。 但是,我想知道为每个提示生成ids的最佳方法,因为它们最终将在列表中通过ID查找。

我知道我可以做类似的事情( max[cue.id for cues in cuelist] + 1 )或者我可以使用random模块并检查randint是否不在cues id中,但是我不知道最多的是什么“合法”的方式是这样做的。

是否有针对此类问题的特定技术或算法?

I am trying to make a list of cues for a movie playback system.

Every time a cue is created I want to add an id to the cue object.

(Which looks something like)

{ "name": "Cue One", "length": 5000, "id": xxx }

Each cue object will get stored in a list of cues. However, I want to know the best way to generate ids for each cue since they will ultimately be looked up, in the list, by ID.

I know I could do something like (max[cue.id for cues in cuelist] + 1) or I could use the random module and check to see if the randint is not in the cues id, however I do not know what the most "legit" way to do this is.

Is there some specific technique or algorithm for this sort of problem?

最满意答案

我认为您正在寻找的是UUIDs参考python文档: https : //docs.python.org/2/library/uuid.html

I think what you are looking for are UUIDs Refering to python documentation: https://docs.python.org/2/library/uuid.html

更多推荐

本文发布于:2023-04-29 07:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表中   ID   Python   list   random

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!