In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Python data structure list is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Small regular exercise: match all the url of the following string
Import re def find_url (sentence, show_urls=None Delete_urls=None): r = re.compile (r'(? I)\ b ((?): [amerz] [\ w -] +: (?: / {1Mague 3} | [a-z0-9]) | www\ d {0pr 3} [.] | [a-z0-9.\ -] + [a-z0-9.\ -] {2} /) (?: [^\ s ()] + | ([^\ s ()] + | (\ ([^\)]) S ()] +\)) *\) + (?:\ ([^\ s ()] + | (\ ([^\ s ()] +) *\) | [^\ s`! ()\ [\] {} ) url_list = r.findall (sentence) if show_urls = = 1: for i in url_list: print (I [0]) if delete_urls = = 1: for j in url_list: # sentence = sentence.replace (j [0],') sentence = sentence.replace (j [0]) '') return sentence return "matching completed" if _ _ name__ = ='_ _ main__': str1 = 'Hello, boss www.google.com' str2 =' wow www.baidu.com can't be opened 'str3 =' where is the python URL found by https://www.python.com'
The basic use of strings has been basically explained! Those who need complex use also need the actual combat of the project and consult the bosses. The next few articles still want to explain some of the basic uses of data structures. Strings can be seen as data structures that store only sequences of characters, and for the convenience of many users, Python provides several powerful and efficient data structures: lists, tuples, dictionaries, and collections. As users, we just need to use it in combination.
Before we start using data structures, we need to introduce another built-in function of Python: type (), which is extremely convenient when we need to know the data type of a value or variable. As follows: the type output contains the class class, which can return specific values and variables or whether it is a module function. This function plays an important role in code debugging, for example, when using a data collection but not knowing the data type within the collection, you can use type to accurately return the object type.
1 sequence
In Python, a sequence is a set of values in order. Python has three built-in sequence types: strings, tuples, and lists.
The sequence supports slicing and indexing, and has the following characteristics:
1 the first positive index is zero, pointing to the left end
2 the first negative index is-1, pointing to the right end
3 the subsequence can be copied by using slice representation
4 splicing can be carried out using + and *
5 you can use the function len to calculate its length, for example, len (string) returns the number of elements contained in the sequence string
The 6 expression x in string checks whether the sequence string contains the element x.
2 list
In Python, the elements of a list are not fixed, but variable, meaning that list elements can be added, deleted, or modified without copying. The list uses square brackets to enclose the elements, and the elements are separated by commas, to give a few simple examples: you can define a num list casually, you can use + and * to concatenate the list, you can also use len () to get the length of the list, and you can also use slicing and indexing to get the sublists in the list.
It is important to note that lists can contain values of any type: numbers, strings, and even other sequences. An empty list is represented by [], while a single element list that contains only one element (x) is written as [x]. It is accessed in the same way as a string, taking num2 as an example, as shown in the following figure: the list index starts at 0, the second index is 1, and so on. Of course, you can also index from right to left.
In addition to the above indexing function, the list also supports the ability to modify elements. In the following figure, we changed the value of indexes=3 in num2 to 'red' string, and Python deleted the original value.
A conclusion can be drawn here: the list element points to the corresponding value, which also has a very interesting effect. Here, let a list element point to the list itself, creating a self-referenced data structure; [.] in the printout It shows that Python can recognize self-citation and will not foolishly keep printing lists (powerful! ).
2.1 list function
Python also provides very powerful functions for lists, as shown in the following table: all functions involve modifying the lists passed to them, except that the count return value returns only one number. Be careful when using it, or you will mistakenly delete the element in the list or insert the element in the wrong place.
Take the function append as an example, add an element at the end of the list num, a simple programming mode, create an empty list at the beginning of the function, and then add elements to the list
As follows: determine whether the data in a list is positive or negative.
Def list_num (list): msg = [] for num in list: if num < 0: s = str (num) +'is negative 'elif 0
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.