有人请看看我的Python语法? [英] Someone please take a look at my Python syntax?
问题描述
创建一个函数manipulate_data,执行以下操作: / p>
接受第一个参数,指定要使用的数据结构的列表list,set或dictionary。
根据列表数据结构指定的数据结构(例如[1,4,9,16,25]),接受作为第二个参数的操作数据。
根据第一个参数返回列表的相反,或者添加项目
ANDELA
,TIA
和非洲
到该集合,或将结果集返回到字典的键。
def manipulate_data(kind,data):
if kind ==列表:
[1,4,9,16,25]中的数据:
return data.reverse()
elif kind ==set:
data.add(TIA)
data.add(ANDELA)
data.add )
data.add(AFRICA)
返回数据
elif kind ==dictionary:
用于{nissan中的数据:23, audi:15,mercedes:3,volvo:45}:
return data.key()
根据问题,这个函数只需要2个参数:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $如果data_type = ='set':
return set.union(data,[ANDELA,TIA,AFRICA])
如果data_type =='dict':
return [ key,item.items()]
Can someone please explain to me what is missing in my code? I have been looking at solutions on the internet and I found a template that I thought would be correct but its still giving me errors. Anyone know what I am doing wrong?
Create a function manipulate_data that does the following:
Accepts as the first parameter a string specifying the data structure to be used "list", "set" or "dictionary". Accepts as the second parameter the data to be manipulated based on the data structure specified (e.g [1, 4, 9, 16, 25]) for a list data structure.
Based off the first parameter return the reverse of a list, OR add the items
"ANDELA"
,"TIA"
and"AFRICA"
to the set, or return the resulting set as keys of a dictionary.
def manipulate_data(kind, data):
if kind == "list":
for data in [1, 4, 9, 16, 25]:
return data.reverse()
elif kind == "set":
for data in {"f", "g", "h", "i", "j"}:
data.add("ANDELA")
data.add("TIA")
data.add("AFRICA")
return data
elif kind == "dictionary":
for data in {"nissan": 23, "audi": 15, "mercedes": 3, "volvo": 45}:
return data.key()
This one works according to the question the function only takes in 2 parameters:
def manipulate_data(data_type=None, data=None):
if data_type is 'list':
return data[-1::-1]
if data_type == 'set':
return set.union(data, ["ANDELA", "TIA", "AFRICA"])
if data_type == 'dict':
return [key for key, item in data.items()]
这篇关于有人请看看我的Python语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!