Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use dictionaries and strings of Python advanced variables

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Python advanced variable dictionary and string how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python advanced variable dictionary and string how to use" it!

1. The definition of dictionary

Dictionary (dictionary) is the most flexible data type in Python outside the list. Dictionary can be used to store multiple data, usually used to store relevant information describing an object.

The difference between a dictionary and a list:

A list is an ordered collection of objects

A dictionary is a collection of objects that are not needed.

Dictionaries are defined with {}

Dictionaries use key-value pairs to store data, use between key-value pairs, split

Key: Key, is the index

Value: Value, data

To use between keys and values; split.

The key must be unique

Values can take any data type, but keys can only use strings, numbers, or tuples

The # dictionary is an unordered collection of data. When using the print function to output the dictionary, the order of the # output is usually inconsistent with the order of the definition! Xiaoming = {"name": "Xiaoming", "age": 18, "gender": True, "height": 1.75, "weight": 75.5} basic use of print (xiaoming) dictionary xiaom_dict = {"name": "Xiaoming"} # value # when taking a value, if the specified Key does not exist, the program will report an error! Print (xiaom_dict ["name"]) # add / delete # if key does not exist, add key value pair xiaom_dict ["age"] = "if key exists, modify existing key value pair xiaom_dict [" name "] =" Xiao Ming "# Delete # if it is worthwhile to delete the specified key, if the specified key does not exist The program will report an error xiaom_dict.pop ("name") print (xiaom_dict) xioaming_dict = {"name": "Xiaoming", "age": 18} # Statistics on the number of key-value pairs print (len (xioaming_dict)) # merge dictionary temp_dict = {"height": 1.75, "age": 20} # Note if the merged dictionary contains existing key-value pairs Will overwrite the original key-value pair xioaming_dict.update (temp_dict) # clear dictionary xioaming_dict.clear () print (xioaming_dict) 2, loop traversal

Traversal is to get all the key-value pairs from the dictionary in turn.

Xiaoming_dict = {"name": "Xiaoming", "qq": "123456", "phone": "10086"} # iterative traversal dictionary # variable K is the Keyfor K in xiaoming_dict of the key-value pair obtained in each loop: print ("% s -% s"%)

Tip: in the actual development, because each key value in the dictionary is different for the type of data stored, there is not much requirement for loop traversal of the dictionary.

# using multiple key-value pairs to store related new descriptions of an object, more complex data information # put multiple dictionaries in one list Then traverse card_list = [{"name": "Zhang San", "qq": "123456", "phone": "110"}, {" name ":" Li Si "," qq ":" 10086 "},] for card_info in card_list: print (card_info) 3, string definition

A string is a string of characters that represent the data type of text in a programming language:

Although you can use "or'to escape a string, in actual development:

If you need to use 'inside the string, you can use define string

You can use the index to get the characters at a specified position in a string, with the index count starting at 0

You can also use for to loop through the string by adding a character.

Most programming languages use "to define strings.

4. Common operations of strings

Define a string in ipython3, for example: hello_str = ""

Enter hello_str. Press the TAB key, and ipython prompts you that the string can be used in the following ways:

Hello_str = "hello hello" # Statistical string length print (len (hello_str)) # count the number of occurrences of a small string print (hello_str.count ("llo")) print (hello_str.count ('abc')) # the location of a string print (hello_str.index ("llo")) # Note: if the string passed by the index method does not exist, an error will be reported! # print (hello_str.index ("abc")) string find and replace hello_str = "hello world" # determine whether to start with a specified string print (hello_str.startswith ("hello")) # determine whether to end print with a specified string (hello_str.endswith ("world")) # find the specified string # index can also find the index print (hello) of the specified string in the big string _ str.find ("llo") # index if the specified string does not exist Error # find if the specified string does not exist, it will return-1print (hello_str.find ("abc")) # replacement string # after the execution of the replace method is complete A new string will be returned # Note: the content of the original string print (hello_str.replace ("world", "python")) print (hello_str) string text alignment exercise # assumption: the following content is captured from the network # requirements: order and center alignment output the following contents poem = ["climb the Magpie Tower", "Wang Zhiyi", "the Sun gradually sets against the mountains" "the Yellow River flows eastward toward the sea", "if you want to see the scenery of thousands of miles", "Please climb another tall building"] for poem_str in poem: print ("|% s |"% poem_str.center (10, ")) remove white space characters # assumption: the following content is captured from the network # requirements: order and center alignment output the following content poem = ["\ t\ nclimb Magpie Tower "," Wang Zhiyi " "the sun gradually sets against the mountains", "the Yellow River flows to the sea", "if you want to see the scenery of thousands of miles", "Please climb another tall building"] for poem_str in poem: # first use the strip method to remove the blank characters in the string # and then use the center method to center the text print ("|% s |"% poem_str.strip (). Center (10). (")) string split and concatenation # suppose: the following is to get # from the network: remove all white space characters from the string and use"as the delimiter Spliced into a neat string poem_str = ["climb Magpie Tower\ t", "Wang Zhiyi\ t", "the sun gradually sets against the mountains", "the Yellow River flows eastward toward the sea\ t\ t", "if you want to see the scenery of thousands of miles\ t\ t" Print (poem_str) # split string poem_list = poem_str.split () print (poem_list) # merge string result = ".join (poem_list) print (result) 5, slicing of the string

Slices use index values to limit the range, cutting out small strings from a large string

Lists and tuples are ordered collections, and the corresponding data can be obtained through index values.

A dictionary is an unordered collection that uses key-value pairs to save data.

Thank you for your reading, the above is the content of "how to use the dictionary and string of Python advanced variables". After the study of this article, I believe you have a deeper understanding of how to use the dictionary and string of Python advanced variables, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report