Python学习笔记,基础2
来源:互联网 发布:php是做前端还是后端 编辑:程序博客网 时间:2024/06/04 21:16
'''在列表中存储列表(嵌套)'''movie = ['The Holy Grail', 1975, 'director', 91, ['starring', ['actor1', 'actor2', 'actor3']]] #列表内元素可以是各种数据类型,可嵌套'''使用if条件语句和for循环语句输出列表中嵌套的列表,本方法之判断嵌套的第一层列表isinstance()为判断条件,返回true or false;isinstance()为BIF,根据参数判断数据类型'''for each_item in movie: if isinstance(each_item, list): for nested_item in each_item: print(nested_item) else: print(each_item)输出: The Holy Grail 1975 director 91 starring ['actor1', 'actor2', 'actor3']#仍有一个嵌套列表没有得到正确处理。'''解决方法:创建一个函数'''
python中函数的创建:
def+函数名(参数):
数组代码
'''创建一个递归函数解决多层嵌套列表的输出pring_lol为函数名the_list为参数'''movie = ['The Holy Grail', 1975, 'director', 91,['starring',['actor1', 'actor2', 'actor3']]]def print_lol(the_list): for each_item in the_list: if isinstance(each_item, list): print_lol(each_item) else: print(each_item)pirint_lol(movie) #函数调用
0 0
- Python学习笔记,基础2
- Python学习笔记2:Python基础
- Python基础学习笔记
- python基础学习笔记
- Python基础学习笔记
- python基础学习笔记
- python基础学习笔记
- python基础学习笔记
- 【Python学习笔记】基础
- Python学习笔记-基础
- Python 基础学习笔记
- Python基础学习笔记
- Python学习笔记(基础)
- Python学习笔记(基础)
- Python基础学习笔记
- Python基础学习笔记
- Python基础学习笔记
- Python学习基础笔记
- mybatis generator 使用指南
- #pragma once 与 #ifdef 的区别
- Failed to resolve: com.android.support:appcompat-v7:23.2.0的问题
- 第四届_翻硬币
- iOS面试题汇总
- Python学习笔记,基础2
- 集训队专题(7)1008 Kakuro Extension
- iOS知识树,知识目录(包括对象、Block、消息转发、GCD、运行时、runloop、动画、Push、KVO、tableview,UIViewController、提交AppStore)
- 华为oj 学英语
- Gt9xxxx系列------如何确定config档
- Provisioning Profile -- 运行报错 Command /usr/bin/codesign failed with exit code 1
- find: missing argument to `-exec' & 删除指定时间的文件
- ActiveX控件安全初始化之一:实现ISafeObject接口(转)
- 马克扎克伯格谈未来科技趋势:视频、人工智能、VR