我正在使用https://github.com/gongzhang/proc-bridge lib与python和java代码进行通信。我需要传递给java代码列表,但我只能传递dict类型。示例代码如下所示:
host = '127.0.0.1'
port = 8077
list = [1,2,3,4,5,6,7,8,9]
def request_handler(api: str, arg: dict) -> dict: #-> here that dict
if api == 'echo':
return list
elif api == 'add':
return {'result': sum(x for x in arg['elements'])}
else:
raise Exception('unknown api')
server = ProcBridgeServer(host, port, request_handler)
server.start()
有一个选项可以将列表转换为类似(或者可能将其更改为仅传递列表):
列表:[1,2,3,4,5,6,7,8,9]
投下Dict之后:[1,2,5,6,7,8,9:3,4]?其中1,2,5,6,7,8,9是值,3,4是键答案 0 :(得分:1)
这会将您的列表转换为字典
alist = [1,2,3,4,5,6,7,8,9]
adict = {}
for value in alist:
adict[value]=value
如果列表不包含简单的数字范围,您可以使用
alist = ['oof','hey',3,4,5,6,7,8,9]
adict = {}
for index in range(len(alist)):
adict[index]=alist[index]
或更简单的版本
alist = ['oof','hey',3,4,5,6,7,8,9]
adict = {'someKey':alist}