from collections import OrderedDict
list_of_interest =\
[OrderedDict[[['name', 'Viscozyme'], ['company', 'Roche [Chile]']]],
[OrderedDict[[['name', 'Davictrel'], ['company', None]]],
OrderedDict[[['name', 'Enbrel Sureclick'], ['company', None]]],
OrderedDict[[['name', 'Tunex'], ['company', None]]]],
OrderedDict[[['name', 'Angiox'], ['company', None]]],
[OrderedDict[[['name', 'Enantone'], ['company', None]]],
OrderedDict[[['name', 'Leuplin'], ['company', 'Takeda']]],
OrderedDict[[['name', 'LeuProMaxx'], ['company', 'Baxter/Teva']]],
OrderedDict[[['name', 'Leupromer'], ['company', None]]],
OrderedDict[[['name', 'Lutrate'], ['company', None]]],
OrderedDict[[['name', 'Memryte'], ['company', 'Curaxis']]],
OrderedDict[[['name', 'Prostap 3'], ['company', 'Takeda UK']]],
OrderedDict[[['name', 'Prostap SR'], ['company', 'Takeda UK']]],
OrderedDict[[['name', 'Viadur'], ['company', 'Bayer AG']]]],
OrderedDict[[['name', 'Geref'], ['company', 'Serono Pharma']]]]
names = []
for item in list_of_interest:
if isinstance[item, OrderedDict]:
names.append[item['name']]
else:
for list_ord_dict in item:
names.append[list_ord_dict['name']]
print[names]
#['Viscozyme', 'Davictrel', 'Enbrel Sureclick', 'Tunex', 'Angiox', 'Enantone', 'Leuplin', 'LeuProMaxx', 'Leupromer', 'Lutrate', 'Memryte', 'Prostap 3', 'Prostap SR', 'Viadur', 'Geref']
Bạn có hai loại mục, bạn có thể biết rằng lặp và in loại thông qua danh sách chính của bạn. Nếu bạn có độ sâu hơn, bạn có thể sử dụng chức năng đệ quy sẽ tự gọi khi gặp danh sách. Đối với bộ dữ liệu bạn đã cung cấp, mã trên hoạt động tốt.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận in Python is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized.
- Trong bài viết này, chúng ta sẽ thảo luận về cách chuyển đổi một trật tự lồng nhau để ra lệnh? Trước đó, chúng ta phải trải qua một số khái niệm:OrderedDict is a dictionary subclass that remembers the order in that keys were first inserted.
- Từ điển trong Python là một bộ sưu tập các giá trị dữ liệu không được đặt hàng, được sử dụng để lưu trữ các giá trị dữ liệu như bản đồ, không giống như các loại dữ liệu khác chỉ chứa một giá trị duy nhất là một phần tử, từ điển giữ cặp khóa: giá trị. Giá trị khóa được cung cấp trong từ điển để làm cho nó được tối ưu hóa hơn.preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order and iterating it gives the values in an arbitrary order. By contrast, the order in the items are inserted is remembered by OrderedDict.
Một đơn đặt hàng là một lớp con từ điển nhớ rằng thứ tự trong đó các khóa được chèn lần đầu tiên.OrderedDict, we are using the collections module in python.
Python3
from
collections
import
OrderedDict
import
=
3
{'1': 'one', '2': 'two'}8
{'1': 'one', '2': 'two'}9
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}0
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}1
{'1': 'one', '2': 'two'}8
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}3
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}4
=
OrderedDict[[[
{'1': 'one', '2': 'two'}0
{'1': 'one', '2': 'two'}1
{'1': 'one', '2': 'two'}2
from
0from
1
{'1': 'one', '2': 'two'}9
{'1': 'one', '2': 'two'}4
from
4from
5from
6from
7from
8from
0
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}4
=
{'1': 'one', '2': 'two'}09
from
7collections
5collections
6
{'1': 'one', '2': 'two'}8
{'1': 'one', '2': 'two'}9
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}0
import
0{'1': 'one', '2': 'two'}8
import
2Output:
Xem thảo luận
Cải thiện bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viếtOrderedDict to dict in just one line, but this is for only simple OrderedDict,not for the nested OrderedDict.
Python3
from
collections
import
OrderedDict
import
=
3
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}4
=
OrderedDict[[[
{'1': 'one', '2': 'two'}0
{'1': 'one', '2': 'two'}1
{'1': 'one', '2': 'two'}2
from
0{'1': 'one', '2': 'two'}8
{'1': 'one', '2': 'two'}9
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}0
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}1
{'1': 'one', '2': 'two'}8
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}3
Output:
{'1': 'one', '2': 'two'}
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}4
=
{'1': 'one', '2': 'two'}09OrderedDict to dict, we are using json.loads[] and json.dumps[] methods.
- Xem thảo luận
- Phương thức JSON.LOADS [] có thể được sử dụng để phân tích chuỗi JSON hợp lệ và chuyển đổi nó thành từ điển Python. Nó chủ yếu được sử dụng để giải phóng chuỗi tự nhiên, byte hoặc mảng byte bao gồm dữ liệu JSON vào từ điển Python. method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.
- Hàm json.dumps [] chuyển đổi một đối tượng python thành chuỗi json.
Python3
from
collections
import
OrderedDict
import
=
3
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}4
=
OrderedDict[[[
{'1': 'one', '2': 'two'}0
{'1': 'one', '2': 'two'}1
{'1': 'one', '2': 'two'}2
from
0from
1
{'1': 'one', '2': 'two'}9
{'1': 'one', '2': 'two'}4
from
4from
5from
6from
7from
8from
0
collections
0
{'1': 'one', '2': 'two'}9
collections
2from
6from
7collections
5collections
6
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}4
=
{'1': 'one', '2': 'two'}09
{'1': 'one', '2': 'two'}8
{'1': 'one', '2': 'two'}9
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}0
import
0{'1': 'one', '2': 'two'}8
import
2Output:
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}