如何在Python中为序列创建子序列,例如[1,2,3],不包括不相邻的子序列(例如[1,3])
例如,如果我有序列 [1,2,3],那么生成子序列的算法是什么:
[1]
[2]
[3]
[1,2]
[2,3]
[1,2,3]
但不是
[1,3]
,我也
[3,2]
希望将它们作为字典中的键以及查找的结果插入在数据库中收集这些唯一的子集,形成值。我想知道你能帮忙吗?
非常感谢!
So for example, if I have the sequence [1,2,3], what would be an algorithm to produce the subseqeunces:
[1]
[2]
[3]
[1,2]
[2,3]
[1,2,3]
but not
[1,3]
nor
[3,2]
I'm then hoping to insert these as the keys in a dictionary along with the result from looking up these unique subsets in a database forming the value. I wonder if you could help with that?
Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者按照您要求的顺序获取它们:
您不能使用列表作为字典中的键,因为字典要求其键是可散列的,而您不能对列表进行散列。
您需要使用元组作为键。
Or to get them in the order you requested:
You can't use lists as keys in a dictionary because a dictionary requires that its keys are hashable and you can't hash lists.
You'll need to use tuples as your keys instead.