day5 beginner list

Posted by jcbones on Sun, 16 Jan 2022 08:22:44 +0100

list

Basic characteristics of container: a container type data can store multiple other data at the same time

  1. What is a list

1) What does a list look like: a list is a container data type (sequence); Take [] as the flag of the container, in which multiple elements are separated by commas: [element 1, element 2, element 3...]

2) Characteristics of list: the list is variable (the number, value and order of elements are variable) - add, delete and change; The list is ordered - subscript operations are supported

3) List requirements for elements: no requirements (no matter what type of data can be used as list elements)

1) Empty list

Len (list) - gets the number of elements in the list

list1 = []
list2 = [ ]
print(type(list1), type(list2))    # <class 'list'> <class 'list'>
print(bool(list1), bool(list2))    # False False
print(len(list1), len(list2))       # 0 0

2) The list can save multiple data at the same time

list3 = [89, 90, 20, 10]
print(list3)
list4 = ['ljm', 18, 123454657]
print(list4)
list5 = [10, 12.5, True, 'abc ', [1, 0], {'a': 10}, (20, 9), {20, 9}, lambda x: x*2]
print(list5)
list6 = [
         [1, 2, 3], 
         [4, 5, 6], 
         [7, 8, 9]
         ]
  1. Query - get element

Query is divided into three cases: obtaining a single element, slicing, and traversal (one by one)

1) Get a single element

Syntax: list [subscript]

Function: get the element corresponding to the specified subscript in the list

explain:

List - any expression that results in a list, such as a variable that holds the list, a specific list value, and so on

[] - Fixed writing

Subscript - subscript, also known as index, refers to the position information of elements in an ordered sequence

Each element in the ordered sequence in python has two sets of subscript values, which are: the subscript value increasing from 0 from the front to the back; Subscript value decreasing from - 1 from back to front

names = ['Zhen Ji', 'army officer's hat ornaments', 'Han Xin', 'Lv Bu', 'Zhao Yun', 'offspring', 'Luban', 'Di Renjie']
print(names[1])   # army officer's hat ornaments
print(names[-1])  # Di Renjie

2) Traversal

Method 1 - get each element in the list directly

for variable in list:
    Circulatory body
for x in names:
    print(x, end=(' '))

Method 2 - first obtain the subscript value of each element, and then obtain the element through the subscript

for subscript in range(len(list)):
    Circulatory body
for x in range(len(names)):
    print(names[x], end=(' '))

Method 3 - get the subscript corresponding to each element and element in the list at the same time

for subscript, element in enumerate(list):
    print(subscript, element)
for x, n in enumerate(names):
    print(x, n)
# Exercise 1: count the number of failed students
scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
count = 0
for x in scores:
    if x < 60:
        count += 1
print(count)		# 3
# Exercise 2: count the number of integers in the list
list7 = [89, 9.9, 'abc', True, 'abc', '10', 81, 90, 23]
count = 0
for x in list7:
    if type(x) == int:
        count += 1
print(count)  		# 4
# Exercise 3: sum all even numbers in nums
nums = [89, 67, 56, 90, 98, 30, 78, 51, 99]
sum = 0
for x in nums:
    if x % 2 == 0:
        sum += x
print(sum)  		# 352
  1. Add - add element

1) Add a single element

List Append - adds an element at the end of the list

List Insert (subscript, element) - inserts the specified element before the element corresponding to the specified subscript

movies = ['Fifty Shades of Grey', 'Godzilla vs. King Kong', 'Peach blossom Man vs chrysanthemum monster']
print(movies)   # ['50 degrees grey', 'Godzilla vs. King Kong', 'Peach Blossom man vs. chrysanthemum monster']
movies.append('The Shawshank Redemption')
print(movies)   # ['fifty degrees grey', 'Godzilla vs. King Kong', 'Peach Blossom man vs. chrysanthemum monster', 'Shawshank's redemption']
movies.insert(2, 'Silent lamb')
print(movies)   # ['fifty degrees grey', 'Godzilla vs. King Kong', 'Silent Lamb', 'Peach Blossom man vs. chrysanthemum monster', 'Shawshank's redemption']

2) Batch add

List 1 Extend (list 2) - adds all the elements of list 2 to the end of list 1

movies = []
movies.extend(['Let the bullet fly', 'Out of reach', 'v Word vendetta team'])
print(movies)
Exercise: print passing grades
scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
m = []
for x in scores:
    if x > 60:
        m.append(x)
print(m)	# [89, 67, 90, 98, 78, 99]

Topics: Python