首页 / PYTHON / Python家庭作业-创建新列表
Python家庭作业-创建新列表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python家庭作业-创建新列表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1399字,纯文字阅读大概需要2分钟。
内容图文
![Python家庭作业-创建新列表](/upload/InfoBanner/zyjiaocheng/693/7f84a1dd283740529d1ff9210ab92dff.jpg)
分配:
Write a function called
splitList(myList, option)
that takes, as input, a list and an option, which is either 0 or 1. If the value of the option is 0, the function returns a list consisting of the elements in myList that are negative, and if the value of the option is 1, the function returns a list consisting of the elements in myList that are even.
我知道如何确定数字是否为偶数以及数字是否为负数.我正在努力如何基于“选项”返回新的负数或偶数列表
到目前为止,这是我得到的:
def splitList(myList):
newlist = []
for i in range(len(myList)):
if (myList[i]%2 == 0):
newlist.append(myList [i])
return newlist
该程序出现以下错误:
Traceback (most recent call last): File "<string>", line 1, in <fragment>
builtins.TypeError: splitList() takes exactly 1 positional argument (4 given)
解决方法:
正如我在评论中提到的那样,您应该对缩进进行标准化:四个空格是Python标准.通常,您可以将编辑器设置为插入四个空格而不是制表符(也不希望将制表符与空格混合使用).
关于您的实际问题:请尝试编写总共三个函数:一个返回所有负值,一个返回偶数,另一个基于选项调用适当的函数.
def splitlist(myList, option):
if option == 1:
return get_even_elements(myList)
elif option == 0:
return get_negative_elements(myList)
def get_even_elements(myList):
pass # Implementation this method here.
def get_negative_elements(myList):
pass # Implementation this method here.
# Test it out!
alist = [-1, 2, -8, 5]
print splitlist(alist, 0)
print splitlist(alist, 1)
内容总结
以上是互联网集市为您收集整理的Python家庭作业-创建新列表全部内容,希望文章能够帮你解决Python家庭作业-创建新列表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。