gpt4 book ai didi

java - 将列表转换为 dict(字典)。过程桥通讯

转载 作者:行者123 更新时间:2023-12-02 12:00:53 25 4
gpt4 key购买 nike

我正在使用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]

Actor 后字典:[1,2,5,6,7,8,9:3,4]?其中 1,2,5,6,7,8,9 是值,3,4 都是键

最佳答案

这会将您的列表转换为字典

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}

关于java - 将列表转换为 dict(字典)。过程桥通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47260564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com