python - list

作者: Pingouin | 来源:发表于2020-09-01 18:17 被阅读0次

    list

    • 可以是空的,也可以list里带list
    • Just like strings, we can get any single element in a list using an index specified in square brackets
    friends = ['xinlei', 'daidai', 'xiaren']
    print(friends[0])
    

    1. lists are mutable (changeable)

    • strings are immutable. We can not change the contents of a string, we must make a new string to make any change.
    • lists are mutable, we can change an element of a list using the index operator
    fruit = 'banana'
    fruit[0] = 'b' # this will get an error because string doesn't support item assignment
    lotto = [2,33,22,33,44]
    lotto[2] = 28
    print(lotto)
    

    2. how long is a list

    len()
    #len() tells us the number of elements 
    

    3. use the range function

    • The range function returns a list of numbers from zero to one less than the parameter.
    • we can construct an index loop using for and an integer iterator.
    print(range(4))
    # [0,1,2,3]
    friends = ['xinlei', 'daidai', 'xiaren']
    print(len(friends))
    print(range(len(friends)))
    # 两种方法写loop
    for i in range(len(friends)):
        friend = friends[i]
        print('Hi:', friend)
    # 这种方法更好
    for friend in friends:
        print('Hi:', friend)
    

    4. concatenating lists using +

    # we can create a new list by adding two existing lists together
    a = [1,2,3]
    b = [4,5,6]
    c= a + b
    print(c)
    # [1,2,3,4,5,6]
    

    5. lists can be sliced using :

    t = [6,23,35,6,7,8]
    t[1:3]
    t[3:]
    t[:]
    # just like strings,  the second number is up to but not including 
    

    6. list methods

    x = list()
    type(x)
    dir(x)
    # 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
    

    7. build a list from scratch

    • create an empty list and then add elements using append method
    • the list stays in order and new elements are added at the end of the list
    stuff = list() # construct emtpy list
    stuff.append('book')
    stuff.append(99)
    print(stuff)
    

    注意append 和 extend 区别: 列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。 append 是将整个的对象添加到原列表的末尾。 而extend 是将列表与原有的列表合并。

    8. is something in a list

    • python provides two logical operators to let you check if an item is in a list, they don't modify the list
    some = ['ke', 1996, 2, 10, ['bdate']]
    'ke' in some # T
    ['bdate'] in some # T
    

    注意与string不同的是,list可以有list.index 但是没有find

    9. lists are in order

    • list can be sorted
    friends = ['xinlei', 'daidai', 'xiaren']
    friends.sort()
    print(friends)
    

    10.build-in functions and lists

    list = [2,4,6,7,86,543]
    print(len(list))
    print(max(list))
    print(min(list))
    print(sum(list))
    print(sum(list)/len(list)) # return to float
    

    对比两种方法: 同样的output

    #方法一 
    total = 0
    count = 0
    while True:
        inp = input("enter a number:")
        if inp == 'done': break
        value = float(inp)
        total = total + value
        count +=1
    average = total/count
    print('average', average)
    #方法二 用list
    numlist = list()
    while True:
        inp = input('input a number:')
        if inp == 'done': break
        value = float(inp)
        numlist.append(value)
    average = sum(numlist)/len(numlist)
    print('average',average)
    

    11. best friends: strings and lists

    • split breaks a string into parts and produces a list of strings. we can access a particular word or loop through all the words.
    abc = 'with three words'
    stuff = abc.split()
    print(stuff)
    for w in stuff:
        print(w)
    
    • when don't specify a delimiter, multiple spaces are treated like one delimiter.
    line = '1;2;3;line'
    thing = line.split(';')
    print(thing)
    # 返回一个list
    

    12. The double split pattern

    • 可以split两次
    example = 'from ke.zhang@ugent.be sat Jan 5 20:00:00 2020'
    words = example.split() # words : ['from','ke.zhang@ugent.be','sat','Jan','5','20:00:00','2020']
    email = words[1] #email: ke.zhang@ugent.be
    school = email.split('@')
    print(school[1])
    

    相关文章

      网友评论

        本文标题:python - list

        本文链接:https://www.haomeiwen.com/subject/cnkmsktx.html