In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is a detailed introduction to "Python's list methods". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "Python's list methods" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.
List more ways
index(): Returns the index of the specified data location (Note: Error if the data you are looking for does not exist.).
count(): Counts the number of times the specified data appears in the current list.
len(): Access list metric, i.e. the number of data in the list.
in: Determine whether the specified data is in a list sequence, if true, otherwise False
not in: Judge that the specified data is not in a list sequence, if not, return True, otherwise return False
append(): appends data to the end of the list.
extend(): appends data to the end of the list. If the data is a sequence, add the data of this sequence to the list one by one.
insert(): Specify where to add data.
pop(): deletes the data of the specified index (the default is the last one) and returns it.
remove(): removes the first match of a data item in the list.
clear(): Clear the list
reverse()
Sort by: sort()
copy()
def generateRandomList(a, b, n, random_state): from random import randint, seed seed(random_state) ls = [] for i in range(n): ls.append(randint(a, b)) return lsls = generateRandomList(0, 100, 10, 666)print(ls) # [58, 48, 55, 36, 64, 1, 70, 70, 99, 91]# index(): Returns the index of the specified data location (Note: Error if the data found does not exist.). print(ls.index(55))#2 print(ls.index(70))#6print (ls.index(70, 7, 9))#7#print(ls.index(101)) ValueError: 101 is not in list# - count(): Counts the number of times the specified data appears in the current list. print(ls.count(70)) # 2print(ls.count(58)) # 1print(ls.count(666)) #0 #- len(): Access list metric, i.e., the number of data in the list. len can also be used in string/tuple/dictionary etc. print(len(ls))#list traversal using len for i in range(len(ls)): print(ls[i])for x in ls: print(x)#implement index method def my_index(ls, val): for i in range(len(ls)): if ls[i] == val: return i return -1print ("myindex = ", my_index(ls, 70))print ("myindex = ", my_index(ls, 101))# - in: determines that the specified data is in a list sequence, returns True if not, returns False#- not in: determines that the specified data is not in a list sequence, returns True if not, otherwise returns Falseprint (69 in ls)print(70 in ls)print (70 not in ls)print(69 not in ls)#- append(): appends data to the end of the list.#- extend(): append data to the end of the list. If the data is a sequence, add the data of this sequence to the list one by one.# insert(): Specify where to add data. hero_ls = []hero_ls.append("Luban No. 7")hero_ls.append("Daji")hero_ls.append(123)print(hero_ls)hero_ls.insert (1, "School Leader")hero_ls.insert (1, "School Leader's Wife")print(hero_ls)hero_ls.extend("BBQ")print(hero_ls)hero_ls.extend (['Yao',''])hero_ls.extend(['123'])print(hero_ls)#- pop(): deletes the data of the specified subscript (default is the last one) and returns it.#- remove(): removes the first match of a data item in the list.#- clear(): Clear list print (hero_ls.pop())print (hero_ls.pop())print (hero_ls.pop(1))print(hero_ls)hero_ls.remove ('Daji ') print(hero_ls)hero_ls.clear()print(hero_ls)# del hero_ls# print(hero_ls)# -reverse()lss = [1, 2, 3, 4, 5]lss.reverse()print(lss)# -order: sort()print(ls)ls.sort()print(ls)ls.sort(reverse=True)print(ls)# - copy()nls = lss.copy()print(nls)print(nls) nested list
There are also lists in the list that represent matrices in mathematics.
name_list = [[''',''','''], ['Tom', 'Lily', 'Rose'], print(name_list[1]) print (name_list [1][0])
practice
#Design a function to generate n rows of right triangles **, store them in a list and return def generate(n): total_ls = [] for i in range(n): ls = [] for j in range(i+1): ls.append('* ') total_ls.append(ls) return total_lsprint(generate(6))for row in generate(6): for v in row: print(v, end='') print()##Def send1(money, count): money*=100 #min m = money//count #Amount per serving l = money%count bag = [] for i in range(count-1): bag.append(m/100) if l == 0: bag.append(m/100) else: bag.append((m+l)/100) return bagprint(send1(100, 3))##Design a lucky red envelope function def send2(money, count): money *= 100 #min from random import randint bag = [] for i in range(count - 1): m = money // count cur = 2*randint(0, m-1) money -= cur bag.append(cur/100) bag.append(money/100) return bagprint(send2(100, 3))print(sum(send2(100, 3)))##Design a function to grab red envelopes Read here, this article "Python list methods what" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to 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.