简体   繁体   中英

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. 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 "[( )]". 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. 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] . 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. 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.

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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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