Tôi đã sao chép cái này:
import json
class S:
def serialize[self, obj]:
return [json.dumps[self.to_dictionary[obj]]]
def to_dictionary[self, obj]:
obj_dict = dict[]
for key in obj.__dict__.keys[]:
try:
if[type[obj.__dict__[key]] is list]:
content = []
for count in range[0, obj.__dict__[key].__len__[]]:
content.append[self.to_dictionary[obj.__dict__[key][count]]]
obj_dict[key] = content
# elif[isinstance[obj.__dict__[key], DomainObject]]:
# obj_dict[key] = self.to_dictionary[obj.__dict__[key]]
else:
obj_dict[key] = obj.__dict__[key]
except KeyError:
pass
except AttributeError:
pass
return [obj_dict]
class A:
def __init__[self]:
self.a = "1234"
self.b = [1, 2, 3, 4]
self.c = [1, 2, 3, 4]
self.d = { 1: 2, 3: 4}
print S[].serialize[A[]]
Output:
{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
Vấn đề là khi mã đệ quy thành to_dictionary[]
với các mục trong danh sách, mã luôn mong đợi rằng nó đang đệ quy với một đối tượng chứa __dict__
. Nhưng cả một chuỗi [cũng không phải int] đều không chứa __dict__
. [Mà tôi thấy bạn đã nêu tương tự trong câu hỏi.]
Tôi sẽ nộp một vấn đề trên github đề cập đến câu hỏi này.
Khác với việc vá mã của họ, tùy chọn duy nhất của bạn là tạo một lớp có các mục bạn muốn. Có vẻ như họ không hỗ trợ string
hoặc
{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
0 trừ khi là giá trị trong __dict__
.Bạn có thể sử dụng
{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
2 làm danh sách:class ReturnList:
def __init__[self]:
self.list = { 1: "Item1", 2: "Item2" }
redirect.returnKeys[ReturnList[]]
Addendum:
Chờ đợi! Tại sao trường hợp tuple hoạt động ?? Tôi nghĩ bạn có thể làm:
redirect.returnKeys[[1,2,3,4]]
Ồ, tôi hiểu tại sao nó hoạt động. Đó là một lỗi. Họ đặc biệt xử lý
{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
3 nhưng không phải {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
4 và nó không tái phát cho các loại {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
4. Vì {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
4 chứa trực tiếp các loại ____ 17 có thể, nó chỉ chuyển đổi nó thành danh sách {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
7. Nếu {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
4 của bạn chứa một lớp hoặc các đối tượng phức tạp hơn [không phải là 17] khác, nó sẽ thất bại.Nếu bạn muốn vá mã của họ cục bộ hoặc yêu cầu họ vá mã của họ, nó có thể được sửa đổi như thế này để cả chấp nhận các bộ dữ liệu và xử lý đúng cách ____10 và string
:
if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
Để duy trì [trong trường hợp bạn có được phiên bản SDK mới của họ], có lẽ tôi sẽ tìm ra cách sử dụng
{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
2 thay vì {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
3 và chỉ cần đưa các chỉ mục vào số nguyên {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
2 của bạn, tương tự như {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
3. tức là, class ReturnList:
def __init__[self]:
self.list = { 1: "Item1", 2: "Item2" }
redirect.returnKeys[ReturnList[]]
7Một cách nhanh chóng để chuyển đổi
{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
3 của bạn thành {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
2 là vớil = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
Mảng [Kiểu dữ liệu, danh sách giá trị]:- Hàm này được sử dụng để tạo một mảng với kiểu dữ liệu và danh sách giá trị được chỉ định trong các đối số của nó. ....
append []:- hàm này được sử dụng để thêm giá trị được đề cập trong các đối số của nó ở cuối mảng ..
Chúng ta có thể gán một danh sách cho một từ điển trong Python không?
Mảng [Kiểu dữ liệu, danh sách giá trị]:- Hàm này được sử dụng để tạo một mảng với kiểu dữ liệu và danh sách giá trị được chỉ định trong các đối số của nó. ....
append []:- hàm này được sử dụng để thêm giá trị được đề cập trong các đối số của nó ở cuối mảng ..
Chúng ta có thể gán một danh sách cho một từ điển trong Python không?
Bạn có thể chuyển đổi danh sách Python thành từ điển bằng phương thức Dict.FromKeys [], một cách hiểu từ điển hoặc phương thức zip []. Phương thức Zip [] rất hữu ích nếu bạn muốn hợp nhất hai danh sách thành một từ điển.ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.
Cách tiếp cận
Để chuyển đổi một mảng numpy thành từ điển, chương trình sau sử dụng Dict [Enumerate [Array.flatten [], 1]] và đây là những gì nó làm chính xác: & nbsp;dict[enumerate[array.flatten[], 1]]and this is what it exactly does:
- Array.flatten: Hàm này được sử dụng để có được một bản sao của mảng đã cho, sụp đổ thành một chiều.: This function is used to get a copy of given array, collapsed into one dimension.
- Tái tạo: Phương pháp liệt kê đi kèm với bộ đếm/chỉ mục tự động cho mỗi mục có trong danh sách. Giá trị chỉ mục đầu tiên sẽ bắt đầu từ 0: Enumerate method comes with an automatic counter/index for each of the items present in the list. The first index value will start from 0
- Dict: Hàm này được sử dụng để chuyển đổi bất kỳ đối tượng nào thành từ điển.: this function is used to convert any object to dictionary.
Ví dụ 1:
Python3
redirect.returnKeys[[1,2,3,4]]
0 redirect.returnKeys[[1,2,3,4]]
1redirect.returnKeys[[1,2,3,4]]
2redirect.returnKeys[[1,2,3,4]]
3 redirect.returnKeys[[1,2,3,4]]
4redirect.returnKeys[[1,2,3,4]]
5redirect.returnKeys[[1,2,3,4]]
6redirect.returnKeys[[1,2,3,4]]
7redirect.returnKeys[[1,2,3,4]]
6redirect.returnKeys[[1,2,3,4]]
9if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
0if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
1if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
2if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
3redirect.returnKeys[[1,2,3,4]]
6if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
5redirect.returnKeys[[1,2,3,4]]
6if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
7if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
0if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
1if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
2l = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
1redirect.returnKeys[[1,2,3,4]]
6l = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
3redirect.returnKeys[[1,2,3,4]]
6l = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
5l = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
6l = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
7redirect.returnKeys[[1,2,3,4]]
3 {"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
2to_dictionary[]
0to_dictionary[]
1to_dictionary[]
2to_dictionary[]
3to_dictionary[]
4to_dictionary[]
5to_dictionary[]
6
to_dictionary[]
5to_dictionary[]
0to_dictionary[]
9__dict__
0
to_dictionary[]
5__dict__
2
to_dictionary[]
5to_dictionary[]
0to_dictionary[]
9__dict__
6
Output:
[[’A’ ’bv’ c,]
[’
[’
[’
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9 : 'tôi'}
[’
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9 : 'tôi'}
Python3
Ví dụ 2:
redirect.returnKeys[[1,2,3,4]]
0 redirect.returnKeys[[1,2,3,4]]
1redirect.returnKeys[[1,2,3,4]]
2redirect.returnKeys[[1,2,3,4]]
3 redirect.returnKeys[[1,2,3,4]]
4__dict__
2redirect.returnKeys[[1,2,3,4]]
6__dict__
4redirect.returnKeys[[1,2,3,4]]
6__dict__
6__dict__
7__8888if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
1if[isinstance[obj.__dict__[key], [list, tuple]]:
for count in range[0, obj.__dict__[key].__len__[]]:
item = obj.__dict__[key][count]]
if not isinstance[item, [str, int]]:
item = self.to_dictionary[obj.__dict__[key][count]]
content.append[item]
2{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
06redirect.returnKeys[[1,2,3,4]]
6{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
08redirect.returnKeys[[1,2,3,4]]
6{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
10__dict__
7{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
12__dict__
7{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
14l = ['string1', 'string2', ]
dictified_list = dict[zip[l, range[0, len[l]]]]
redirect.returnkeys[dictified_list]
6Các
to_dictionary[]
5to_dictionary[]
6
to_dictionary[]
5to_dictionary[]
0to_dictionary[]
9__dict__
0
to_dictionary[]
5__dict__
2
to_dictionary[]
5to_dictionary[]
0to_dictionary[]
9__dict__
6
Output:
7l = ['string1', 'string2', ] dictified_list = dict[zip[l, range[0, len[l]]]] redirect.returnkeys[dictified_list]
3redirect.returnKeys[[1,2,3,4]]
2{"a": "1234", "b": [1, 2, 3, 4], "d": {"1": 2, "3": 4}}
to_dictionary[]
0to_dictionary[]
1to_dictionary[]
2to_dictionary[]
3to_dictionary[]
4[[‘1,’ 2, 3 3
[‘6,’ 7, ’8 8
[’
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9 : 'tôi'}
[’