【2.1.1】python--列表(list)

一、列表中第一个数的编号为0,

>>> movies=["The holy grail","the life of brian","the meaning of life"]
>>> print(movies)
['The holy grail', 'the life of brian', 'the meaning of life']
>>> print(movies[1])
the life of brian

二、for 与whilie循环

for 目标识别符 in 列表:

>>> cast=["aa","bb"]
>>> for each_item in cast:
        print(each_item)

结果:

aa
bb

代码:

>>> count=0
>>> while count
         print(cast[count])
         count=count+1

结果:

aa
bb

len处理列表时候表示列表元素的个数,处理元素的时候表示元素的字符个数。

三、列表中的列表

python可以处理包换混合类型的数据,在同一个列表中可以同时存放数字和字符串。

>>> movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

>>> print(movies)

['The Holy Grail', 1975, 'Terry Jones & Terry Gilliam', 91, ['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]]

>>> print(movies[4][1][3])     #套嵌列表

Eric Idle

>>> for each_item in movies:
    print(each_item)

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]

四、isinstance是python内置函数,用来判断数据类型

>>> names=["aa","bb"]
>>> isinstance(names,list)

True

>>> num=len(names)
>>> isinstance(num,list)

False

>>> for each_item in movies:

    if isinstance(each_item, list):

        for nested_item in each_item:

            print(nested_item)

    else:

        print(each_item)

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']

如果是更多的套嵌

for each_item in movies:
         if isinstance(each_item, list):
             for nested_item in each_item:
                 if isinstance(nested_item, list):
                     for deeper_item in nested_item:
                               print(deeper_item)
            else:
                print(nested_item)
         else:
             print(each_item)

也可以自定义函数

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,["Graham Chapman", ["Michael Palin", "John Cleese","Terry Gilliam", "Eric Idle", "Terry Jones"]]]

def print_lol(a_list):
    for each_item in a_list:
        if isinstance(each_item, list):
            print_lol(each_item)
        else:
            print(each_item)        
print_lol(movies)

输出的时候缩进一下,给函数定义了了两个参数,其中level不设置的话就是0。

def print_lol(a_list, level=0):
    """Prints each item in a list, recursively descending
       into nested lists (if necessary)."""
 
    for each_item in a_list:
        if isinstance(each_item, list):
            print_lol(each_item, level+1)
        else:
            for l in range(level):
                print("\t", end='')
            print(each_item)

增加以一个参数,默认为Falase,如果为Ture,再来缩进。

def print_lol(a_list, indent=False, level=0):
    """Prints each item in a list, recursively descending
       into nested lists (if necessary)."""

    for each_item in a_list:
        if isinstance(each_item, list):
            print_lol(each_item, indent, level+1)
        else:
            if indent:
                for l in range(level):
                    print("\t", end='')
            print(each_item)

参考资料

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn