List list function of python3.6 data type

- list 'list'    dynamic array --[]-- can store multiple data types very powerful

    -create  

        -sequence 

        list1=[1,2,3,4,5,6]   

    -increase

         1. list1.append(item) adds to the tail (only one element can be added)

         2.list1.insert(index.item) (the disadvantage is to increase the memory to run)

                                            Insert into the specified place Adds an element of any type at the specified index subscript position,

                                            Elements in their original positions are automatically moved backwards

          3.list1.extend(list2) iteratively traverse list 2 to list 1 (ps: must be in the form of a list)

          4.name+['teacher','classmate'] is combined into a new list

          5. *  list1*3

    -delete

          1. list1.pop(index) If you do not specify an element, the last element will be deleted by default

          2.del: delete the list, if there is no list, you can delete the elements in the list by index, and you can also delete the object

                del name[1]

          3.tony.clear() clears the list, the list is empty

          4.remove(): Remove an element in the list by the value in the list If there are the same elements, only the first element is removed (ps: not by index)

                name.remove('Hello')

    -Change, copy directly

    - lookup, index (subscript value)

          index returns the index found 

          list1[0]=0

          list1 --[0,2,3,4,5,6]  

    -index 

          same string   

    -slice

          same string 

    -sort

          name.sort() can only be sorted when all elements in the list are of the same type (according to ascii order)

          tony.reverse() reverse---combined with sort, it can be done from big to small

    -copy

            list.copy() copy

            a = [1,2,3]

            b = a

            print(a)

            print(b)

            b[2] = 9

            print(a)  --[1,2,9]

            print(b)  --[1,2,9]

     -dir(object) output all functions and properties of a data type

     -len(list) number of list elements

       __len__() __ type functions represent private and generally do not call

     -max(list) letters return the maximum value of the list element in ascii

        min(list) returns the minimum value of the list element

     -list() convert tuples to lists

     -List comprehension: quickly create a new list based on an existing list

          tony = [nums**2 for nums in range(1,11)]

           print(tony)

    - Iterator: It is a way of accessing a collection of lists. Can only go forward, not backward

    - 2D list

        - list nesting

            li1=['a','b']

            li2=[1,2,3,4,5]

            li3=[li1,li2]

            print(li3[1][3])


#Exercise: Create 2 lists, one for name and one for phone
 # -------------Address management system------------
 # 1.Add name and phone
 # 2.Delete Name
 # 3. Modify phone
 # 4. Check all users
 # 5. Find phone number by name
 # 6. Exit
names=['Wang Yong','Zhao Fan']
phones=['13938493783','166666666']

print('***********************')
while(True):
    print('=======Address Book Management System======== ')
    print('1. Add name and mobile phone')
    print('2. Delete name')
    print('3. Modify the phone')
    print('4. Query all users')
    print('5. Find the phone number based on the name')
    print('6. Exit')
    print('============================')
    i=int(input('Please choose:'))
    if(i==1):
        name=input('Please enter your name:')
        phone=input('Please enter phone number:')
        while phone in phones:
            phone=input('The phone already exists, re-enter the phone:')
        names.append(name)
        phones.append(phone)
        print('Entry successful')
    elif i == 2:
        name = input('Please enter your name:')
        if name in names:
            index=names.index(name)
            # names.pop(index)
            # phones.pop(index)
            del names[index]
            del phones[index]
            print('Delete successful')
        else:
            print('Name does not exist')
    elif i == 4:
        for i in range(names.__len__()):
            print(names[i],phones[i])
        print('Output completed')
    elif i == 3:
        phone = input('Please enter the phone number:')
        if phone in phones:
            new_phone = input('Please enter a new phone:')
            while new_phone in phones:
                new_phone = input('The new phone already exists, re-enter the phone:')
            index=phones.index(phone)
            phones[index]=new_phone
            print('Update succeeded')
        else:
            print('Phone does not exist')
    elif i == 5:
        name = input('Please enter your name:')
        if name in names:
            index = names.index(name)
            print('Phone:',phones[index])
        else:
            print('Name does not exist')
    elif i == 6:
        print('Thank you for using')
        break



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325853727&siteId=291194637