python3.5:用函数创建字典

2024-04-26 08:14:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个返回输出的函数。函数返回多个内容。我需要函数1的输出是一个键,函数2的输出是一个值。我不知道这是否可行,希望你能帮我举个例子!你知道吗


Tags: 函数内容例子
3条回答
def the_keys():
    return ('a', 'b', 'c')

def the_values():
    return (1, 2, 3)

dictionary = dict(zip(the_keys(), the_values()))

print(dictionary)

产生:

{'c': 3, 'b': 2, 'a': 1}

就这么做吧!你知道吗

def fun1():
    return "a"

def fun2():
    return 1

a = {fun1(): fun2()}

print a

这张照片:

{'a': 1}

是这个吗?你知道吗

def functionA:
    return "bart"

def functionB:
    return 33

def merge(func1, func2):
    return { func1():func2() }

print merge(functionA, functionB)

相关问题 更多 >