列表练习

发布时间:2019-03-07 19:47:54编辑:auto阅读(2094)

    1.创建一个henan的空列表,添加zhengzhou,kaifeng,shangqiu,jiaozuo,xinyang元素;

    henan = []
    henan.extend(['zhengzhou','kaifeng','shangqiu','jiaozuo','xinyang'])
    print(henan)
    ['zhengzhou', 'kaifeng', 'shangqiu', 'jiaozuo', 'xinyang']

    2.在kaifeng元素前面插入一个luoyang元素;

    henan.insert(1,'luoyang')
    print(henan)
    ['zhengzhou', 'luoyang', 'kaifeng', 'shangqiu', 'jiaozuo', 'xinyang']

    3.把元素xinyang改成中文信阳;

    henan[-1] = '信阳'
    print(henan)
    ['zhengzhou', 'luoyang', 'kaifeng', 'shangqiu', 'jiaozuo', '信阳']

    4.在元素jiaozuo后面插入一个子列表[hebi,xuchang];

    list = ['hebi','xuchang']
    henan.insert(-1,list)
    print(henan)
    ['zhengzhou', 'luoyang', 'kaifeng', 'shangqiu', 'jiaozuo', ['hebi', 'xuchang'], '信阳']

    5.打印元素shangqiu的索引值;

    print('shangqiu的索引值:',henan.index('shangqiu'))
    shangqiu的索引值: 3

    6.创建新列表[1,2,3,4,5,6,2],与henan列表合并;

    number = [1,2,3,4,5,6,2]
    henan.extend(number)
    print(henan)
    ['zhengzhou', 'luoyang', 'kaifeng', 'shangqiu', 'jiaozuo', ['hebi', 'xuchang'], '信阳', 1, 2, 3, 4, 5, 6, 2]

     

关键字