python - 在Python中调用函数后如何使用方括号
问题描述
我想在调用函数后使用方括号。是这样的
函数()['名称']
我希望输出是“安迪”。def 函数语法应该如何?我总是得到错误,说它不可下标。这是我当前的代码
def function1():
A={'Name':'Andy'}
return
function1()['Name']
解决方案
我不太明白你的问题。但是如果你想使用方括号,并通过字符串查找你的值,你应该使用字典。所以函数和打印应该是这样的``
def function():
return {'Name':'andy'}
print(function()['Name'])
EDIT: You can do something like this, and that will print the answer, but i dont think its really practicall, because, yo are not storing the name anywhere
def function():
return {'Name':print('Andy')}
function()['Name']
推荐阅读
- cupy - 遍历数组时,Cupy 比 numpy 慢
- microsoft-graph-api - 如何使用图形 API 从 OneDrive 中的 Excel 获取图表
- javascript - I have two data structures that have some duplicated information. Is there a way to reduce this duplication using .map?
- node.js - 在 Windows 中运行在 Mac 上创建的电子项目时如何避免错误?
- vue.js - vue-router 中的 next() 守卫功能如何工作?
- python - Django/Python 缓存解决方案,我可以使用复杂的键而不是字符串来访问
- python - 变量和损失评估的奇怪排序
- amazon-web-services - 允许使用小写字母数字字符和连字符的 Terraform RDS 实例错误
- python - 从熊猫数据框中删除方括号
- php - 删除项目时的Laravel分页问题