简体   繁体   English

你如何在 python 中创建一个类或函数,允许你创建具有特定特征的序列类型

[英]How do you create a class, or function in python that allows you to make a sequence type with specific characteristics

1) My goal is to create a sequence that is a list that contains ordered dictionaries. 1)我的目标是创建一个序列,它是一个包含有序字典的列表。 The only problem for me will be described below.对我来说唯一的问题将在下面描述。

  • I want the list to represent a bunch of "points" which are for all intents and purposes just an ordered dictionary.我希望该列表代表一堆“点”,它们出于所有意图和目的只是一个有序的字典。 However, I notice that when I use OrderedDict class, when I print the dictionary it comes up as OrderedDict([key value pair 1, key value pair 2, ... etc)] For me, I would rather it behave like an ordered dictionary, BUT not having those DOUBLE "messy/ugly" "end marks" which are the "[( )]".但是,我注意到当我使用 OrderedDict 类时,当我打印字典时,它会显示为 OrderedDict([key value pair 1, key value pair 2, ... etc)] 对我来说,我宁愿它表现得像一个有序字典,但没有那些双重“凌乱/丑陋”“结束标记”,即“[( )]”。 I don't mind if the points have ONE, and only one, type of "end marks".我不介意这些点是否只有一种类型的“结束标记”。 Also I would also like it if when I print this data type that stuff like OrderedDict() doesn't show up.此外,如果我打印此数据类型时, OrderedDict()类的东西没有出现,我也希望如此。 However, I do not mind if it shows up in return values.但是,我不介意它是否出现在返回值中。 Like you know how when you print a list it doesn't show up as list(index0, index1, ... etc) but instead it shows up as [index0, index1, ... etc] .就像您知道如何打印列表时它不会显示为list(index0, index1, ... etc)而是显示为[index0, index1, ... etc] That is what I mean.这就是我的意思。 Inside the point, it would look like this在点内,它看起来像这样

point = {'height': 1, 'weight': 3, 'age': 5, etc} <- It could be brackets or braces or parentheses. point = {'height': 1, 'weight': 3, 'age': 5, etc} <- 可以是方括号、大括号或圆括号。 Just some type of "end mark", but I preferably would like it to be in {} and having key value pairs indicated by key: value and have them separated by commas.只是某种类型的“结束标记”,但我更希望它在 {} 中,并且具有由 key: value 指示的键值对,并用逗号分隔它们。

what_i_am_looking_for = [point0, point1, point2, point3, ... etc] what_i_am_looking_for = [point0, point1, point2, point3, ... etc]

In Python 3.6, the ordinary dict implementation was re-written and maintains key insertion order like OrderedDict , but was considered an implementation detail.在 Python 3.6 中,普通的dict实现被重写,并像OrderedDict一样维护键插入顺序,但被认为是一个实现细节。 Python 3.7 made this feature an official part of the language spec, so if you use Python 3.6+ just use dict instead of OrderedDict if you don't care about backward-compatibility with Python 3.5 or earlier. Python 3.7 将此功能作为语言规范的官方部分,因此如果您使用 Python 3.6+,如果您不关心与 Python 3.5 或更早版本的向后兼容性,只需使用dict而不是OrderedDict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM