current position:Home>Stroke the list in python (top)

Stroke the list in python (top)

2022-01-30 21:07:35 Lei Xuewei

「 This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge

ceremonial Python Column No 26 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

The previous school committee shared Tuple, When it comes to the Tuple Follow the list list The relationship between .

This time we turn our attention to list

What is? list?

list It's a sequence ! A string of data , This string can append data .

We can think of it as a large shopping mall , Children play in tandem cars , It's like a train , You can hang it up ( It can also add cars ).

such tuple( Tuples ) It's a series of welded cars !

In data structure , We have also studied linked lists , In a way list Namely python An implementation is given .

It can store data indefinitely , And through subscript ( from 0 Start counting ) Get the element at a specific location .

With that said, let's look at the code , Feel the :

list_a = [1, 2, 3]
list_b = ['hello','lei',' Academic Committee ', 666]
 Copy code 

Above is python Medium list.

list What can be done ?

Ahead of us tuple Tried + and *, these list Can you do it ?

The answer is yes .

The school committee here copied the front tuple Changes to the code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : list_demo.py
# @Project : hello


a = [1, 2, 3]
print(a[0])

t = list(a)
print(t[0:2])
print(type(t[0:2]))
# <class 'list'>
t[0] = 22 # list You can modify the value of the corresponding subscript !
a[0] = 22
mylist = list([a, [2, 3]])
print(mylist)
([22, 2, 3], [2, 3])
mylist[0][0] = 100 # This one can ! That's great !
print(mylist)
print(type(mylist))
 Copy code 

Dynamic length parameter transfer


def show_info_v2(name, title, *info):
    print(" The name is :", name)
    print(" The title is :", title)
    print(" Other comments :", info)


show_info_v2(' Lei Xuewei ', ' Brick master ', " Love technology ", " love your life ")
 Copy code 

Whether the parameter will be changed by the function ?

Let's take a look at the following program :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/24 11:39  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : func_call.py
# @Project : hello


def compute_v1(list):
    sum = 0
    for x in list:
        sum += x
    list = list + [sum]
    print(" New address :", id(list))
    return sum


def compute_v2(list):
    sum = 0
    for x in list:
        sum += x
    list[0] = list[0] * 100
    return sum


_list = [1, 2, 3, 4, 5]
print(" Call the calculation function v1 Before :", _list)
print(" Call the calculation function v1 Previous memory address :", id(_list))
print(compute_v1(_list))
print(" Call the calculation function v1 after :", _list)
print(" Call the calculation function v1 Then the memory address :", id(_list))

_list = [1, 2, 3, 4, 5]
print(" Call the calculation function v2 Before :", _list)
print(" Call the calculation function v2 Previous memory address :", id(_list))
print(compute_v2(_list))
print(" Call the calculation function v2 after :", _list)
print(" Call the calculation function v2 Then the memory address :", id(_list))
 Copy code 

This is the effect of code running :

 Screenshot  2021-11-01  Afternoon 11.39.26.png

We can modify list The elements of the .

list What can this object do ?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : list_demo2.py
# @Project : hello


list = [3, 3, 3]
# new_list = list - list #TypeError: unsupported operand type(s) for -: 'listle' and 'listle'
new_list = list + list
print(new_list)

#  The academic committee still likes the three companies ,666
new_list = list * 3
print(" Three times in a row list:", new_list)
# new_list = list / 3 #  I don't support it. It must be next time !

print("'666' in new_listle ? ", '666' in new_list)
 Copy code 

Here's how it works :

 Screenshot  2021-11-01  Afternoon 11.44.33.png

You can see , We were tuple The operation list Also supported one by one .

tuple turn list

Go straight to the code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : list_demo2.py
# @Project : hello


mylist = [3, 3, 3]

new_list = [x * 2 for x in mylist]
print(" Doubled list:", new_list)

#  Start here to show tuple  turn list

tup = (6, 6, 6)
new_list = list(tup)
print(" hold list turn list: ", new_list)
if 6 in new_list:
    print("new_list has 6 !")
 Copy code 

summary

list There are many other functions , Let's continue with the next article , This is a tuple Introduction article :juejin.cn/post/702518…

We see list and tuple There is some symmetry in the design !

The former uses '[]' To surround the elements , Then use '()' To surround the elements , The operation is also very similar , It can even be said to be very consistent !

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302107343898.html

Random recommended