第一章 初识Python

一、安装Python 3: 获取网址:www.python.org

二、IDLE下执行python代码:还是选一个支持Python的集成编译环境吧,Idle与cmd命令下的效果差不多,除了一些配色。

三、关于list的练习 01代码:

**************************e:\pthw\hfpython\ex\ex_01.py**********************************************************

#!/usr/bin/env python3        #配置python的环境信息
# -*- coding:utf-8 -*-         #编码格式
__author__ = "Oldman"       #嗯,对于Coder而言,是Old了

#基础练习1:用循环遍历长度可变、元素包含多层(至多3层)的list,当前仅仅将元素信息打印在屏幕上
#list中最多嵌套3层list
#遍历每一个元素

movies = ['The Holy Grail',1975,"Terry John & Terry Gilliam",91,
  ['Graham Chapman',["Michael Palin","John Cleese",
  "Terry Gilliam",'Eric Idle',"Terry Johns"],'Old Man',
  'Hello World',2008,2022,["中共二十大",
  'Verifone Infomation',20221017],['很神奇','QA']],
  'Yesterday Once More',[1,2,3,4],
  'One Night in Beijing','In NewYork']

for name_1 in movies:
  if isinstance(name_1,list):
    for name_2 in name_1:
      if isinstance(name_2,list):
        for name_3 in name_2:
          print(name_3)
      else:
        print(name_2)
  else:
    print(name_1)

*******************分割******************

执行结果:

E:\Pthw\HFPython\ex>ex_01.py
The Holy Grail
1975
Terry John & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Johns
Old Man
Hello World
2008
2022
中共二十大
Verifone Infomation
20221017
很神奇
QA
Yesterday Once More
1
2
3
4
One Night in Beijing
In NewYork

******************用递归方式来做,可支持任意层的list遍历********************************** 

def recursion(list_1):
  for element in list_1:
    if isinstance(element,list):
      recursion(element)
    else:
      print(element)

movies = ['The Holy Grail',1975,"Terry John & Terry Gilliam",91,
    ['Graham Chapman',["Michael Palin","John Cleese",
    "Terry Gilliam",'Eric Idle',"Terry Johns"],'Old Man',
    'Hello World',2008,2022,["中共二十大",
    'Verifone Infomation',20221017],['很神奇','QA']],
    'Yesterday Once More',[1,2,3,4],
    'One Night in Beijing','In NewYork']

list2 = ['Hello',[12,34,1234.56]]      #这段代码仅仅是说明lsit可以作为另一个list的元素,实现插入、删除、改写等;

movies.append(list2)

print(movies)

recursion(movies)

 

posted @ 2022-10-18 09:44  青萍微澜  阅读(32)  评论(1)    收藏  举报