Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将输出汇总到字典中_Python - Fatal编程技术网

Python 将输出汇总到字典中

Python 将输出汇总到字典中,python,Python,如何正确运行最后一行?有什么想法吗?首先,您需要确保函数实际返回一个值。将其从打印更改为返回。然后在字典中调用这些函数(注意函数名周围的括号?) 您应该返回函数中的值,如果您只是print(),它们的返回值将是None。如果您的目标是在firstone()的输出是键,而firsttwo()的输出是值时创建dict: 代码: def firstone(): x= 1 return x def firsttwo(): y=1 return y d = {"

如何正确运行最后一行?有什么想法吗?

首先,您需要确保函数实际返回一个值。将其从打印更改为返回。然后在字典中调用这些函数(注意函数名周围的括号?)


您应该
返回函数中的值,如果您只是
print()
,它们的
返回值将是
None
。如果您的目标是在
firstone()
的输出是
键,而
firsttwo()
的输出是
值时创建
dict

代码

def firstone():
    x= 1
    return x


def firsttwo():
   y=1
   return y

d = {"firstone":firstone(),"firsttwo":firsttwo()}
def firstone():
    return 1


def firsttwo():
   return 1

d = {firstone(): firsttwo()}
输出

def firstone():
    x= 1
    return x


def firsttwo():
   y=1
   return y

d = {"firstone":firstone(),"firsttwo":firsttwo()}
def firstone():
    return 1


def firsttwo():
   return 1

d = {firstone(): firsttwo()}

另外,这两个函数都打印到屏幕上,并且不返回任何内容。这就是为什么你不能合并结果:没有。@Mark谢谢你的建议,我编辑了它。@576i因为线程中没有firsttwo()和firstone()代码,我想了解如何在字典中一起运行它们?