In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use List in Python". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use List in Python".
What is list?
List is a sequence! A string of data that can be appended.
We can think of it as a train played by children in a large shopping mall. it can be hung up (and can be added) just like a train.
So tuple (tuple) is a series of cars that are welded to death!
In data structures, we have also studied linked lists, and to some extent list is an implementation given by python.
It can store data indefinitely and get the elements at a specific location through the subscript (counting from 0).
With all that said, let's look at the code and feel it:
List_a = [1,2,3] list_b = ['hello','lei',' Committee', 666]
Above is the list in python.
What can list do?
We have tried + and * in tuple before. Can we do these list?
The answer is yes.
Here the school committee copied the previous tuple code to make changes:
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:36 on 2021-10-31 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: list_demo.py# @ Project: helloa = [1,2 3] print (a [0]) t = list (a) print (t [0:2]) print (type (t [0:2])) # t [0] = 22 # list can modify the value of the corresponding subscript! A [0] = 22mylist = list ([a, [2,3]]) print (mylist) ([22,2,3], [2,3]) mylist [0] [0] = 100 # this is OK! That's great! Print (mylist) print (type (mylist)) dynamic length parameter transfer def show_info_v2 (name, title, * info): print ("name:", name) print ("title:", title) print ("other comments:", info) show_info_v2 ('Lei Xue Committee', 'brick master', "love technology", "love life") parameters will be changed by the function?
Let's take a look at the following procedure:
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:39 on 2021-10-24 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: func_call.py# @ Project: hellodef compute_v1 (list): sum = 0 for x in list: sum + = x list = list + [sum] print ("New address:" Id (list)) return sumdef 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 ("before calling calculation function v1:", _ list) print ("memory address before calling calculation function v1:" Id (_ list) print (compute_v1 (_ list)) print ("after calling calculation function v1:", _ list) print ("memory address after calling calculation function v1:", id (_ list)) _ list = [1,2,3,4,5] print ("before calling calculation function v2:", _ list) print ("memory address before calling calculation function v2:" Id (_ list)) print (compute_v2 (_ list)) print ("after calling calculation function v2:", _ list) print ("memory address after calling calculation function v2:", id (_ list))
This is the effect of the code running:
We can modify the elements of list.
What can an object like list do? #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:36 on 2021-10-31 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: list_demo2.py# @ Project: hellolist = [3,3 3] # new_list = list-list # TypeError: unsupported operand type (s) for -: 'listle' and' listle'new_list = list + listprint (new_list) # the committee still likes Sanlian very much. 666new_list = list * 3print ("list:", new_list) # new_list = list / 3 # not sure next time! Print ("'666' in new_listle?", '666' in new_list)
Here is how it works:
As you can see, our previous operations in tuple list are also supported one by one.
Tuple to list
Go directly to the code:
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:36 on 2021-10-31 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: list_demo2.py# @ Project: hellomylist = [3,3,3] new_list = [x * 2 for x in mylist] print ("doubled list:", new_list) # show tuple to listtup = (6,6) from here 6) new_list = list (tup) print ("convert list to list:", new_list) if 6 in new_list: print ("new_list has 6!") Having said so much, there seems to be something missing. The traversal list hasn't been shown yet.
This is very simple. I'll be right there.
List = [1,2,3] for x in list: do_on_value (x)
In this way, in fact, the above display code is covered, but not specifically said.
This is the sentence 'new_list = [x * 2 for x in mylist]', which traverses the list directly and forms a new list with the elements generated by the values of each function x2.
The above is all the contents of the article "how to use List in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.