specifying str.lower function to use for sortingprint sorted(strs, key=str.lower) ## ['aa', 'BB', 'CC', 'zz']#3.You can also pass in your own MyFn as the key function, like this:## Say we have a list of strings we want to sort by the last letter of the string. strs = ['xc', 'zb', 'yd' ,'wa'] ## Write a little function that takes a string, and returns its last letter. ## This will be the key function (takes in 1 value, returns 1 value). def MyFn(s): return s[-1] ## Now pass key=
This article describes how to use the Python dictionary dict. For more information, see
Create a Python dictionary
Method 1:
>>> blank_dict = {}>>> product_dict = {'MAC':8000,'Iphone':5000, 'ipad':4000, 'mp3': 300}>>> product_dict{'ipad': 4000, 'MAC': 8000, 'Iphone': 5000, 'mp3': 300}>>> blank_dict,product_dict({}, {'
Dictionary: an object associated with an array or a hash list that can be indexed by a keyword. Usage: defines an object that can contain multiple named fields, it can also be used as a container dictionary to quickly search for unordered data. it is the most comprehensive data type in python. it is most commonly used in programs to store and process data diction
Python dictionary Value Comparison function example, python example
This example describes the dictionary Value Comparison function implemented by Python. We will share this with you for your reference. The details are as follows:
# Coding = utf8import loggingimport osfrom L
The list is a class in Python. A specific table, such as nl [1, 3, 8], is an object of this class. We can call some methods of this object, such as nlappend (15 ). We will introduce a new class, dictionary ). Similar to the list, the basic tutorial introduces basic concepts, especially objects and classes.
The advanced tutorial further expands the basic tutorial to illustrate the details of
DescriptionIt is obvious that the learning process of a dictionary in Python is the same as a list, mainly focusing on the following functions:>>> xpleaf.xpleaf.clear (Xpleaf.copy (Xpleaf.get (Xpleaf.has_key (Xpleaf.items (Xpleaf.keys (Xpleaf.pop ( Xpleaf.popitem (Xpleaf.setdefault (Xpleaf.update (
1. Basic operation--Create a dictionary>>> xpleaf = {...' N
This article mainly introduces the basic operations of Python list, dictionary, and string. This article summarizes the most basic and commonly used operations, for more information, see create a list.
The code is as follows:
Sample_list = ['A', 1, ('A', 'B')]
Python list operations
The code is as follows:
Sample_list = ['A', 'B', 0, 1, 3]
Obtain a value in the
The code is as follows
Copy Code
#!/usr/bin/env python#-*-Encoding:utf-8-*-Dict2 = {}A = ' a 'Dict2.setdefault (a,1) #加入一个键和值 a,1A = ' B 'Dict2.setdefault (a,1) #加入一个键和值 b,1A = ' a 'Dict2.setdefault (a,2) #加入一个键和值 a,1, the resulting duplicate key cannot be repeated by adding {' A ': 1, ' B ': 1}Print Dict2#遍历key和valueFor key in Dict2.keys ():print
Basic python tutorials-A Dictionary of notes
Create dictionary
A dictionary consists of multiple key-value pairs. Each key and corresponding value are separated by a colon, and items are separated by a comma. The entire dictionary is enclosed by a pair of braces, as shown
It is often used in development to traverse the dictionary, list, and other data cyclically. However, in python, dictionary traversal is very unfamiliar to many beginners, today, we will talk about the circular traversal of dictionary and list data in python, but the
'
The acquisition of value in a dictionary
If key does not exist, then Dict[key] throws an error, sometimes in order to avoid the error, we use the Get () function to obtain the value of the key, if the key does not exist, then the default returns None
Animals.get (1)
' Tiger '
Animals.get (4, ' default ')
' Default ' #key不存在的时候, return defaults
adding elements
Animals[4]= ' Eagle ' #只需要为字典中key进行赋值
Animals
{1: ' Tiger ', 2: ' Lion ', 3: ' Horse ', 4:
the above code is more elegant, but similarly to the Dict function, where the key is a variable and only the literal value is used.1.5 Dictionary deletionYou can call the Python built-in keyword del to delete a key value>>>Info= dict (name= " Cold ' blog= ' linuxzen.com ' ) Span class= "o" >>>> info{ ' blog ' : ' linuxzen.com ' ' name ' : ' cold ' Span class= "p" >}>>> del info[ ' name ' ]>>> info{ '
Python creates a dictionary without too many quotes.
This example describes how to create a dictionary without too much reference in python. Share it with you for your reference. The specific implementation method is as follows:
1. Use the itertools Module
import itertoolsthe_key = ['ab','22',33]the_vale = ['aaaa',"ddd
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.