Python [3]-dictionary dic And set, python dictionary dicsetI. dictionary dict
Dict is stored as a key-value pair. The creation method is to use braces {} and separate the key and value with a colon.
>>> d={'chen':60,'zhang':80}>>>
My Python self-learning Path 3: tuples and dictionary learning summary, the path to python
1. tuples
1. the Python tuples are similar to the list, except that the elements of the tuples cannot be modified.
The tuples use parentheses, And the list uses square brackets.
Creati
1. Dictionaries1.1 The difference between a dictionary type and a sequence type:Data is accessed and accessed in different ways.The sequence type is indexed only by numeric type keys (numerically sequentially from the beginning of the sequence);A mapping type can be used as a key for other object types (such as numbers, strings, ganso, usually strings as keys), and the keys of the sequence type are different, and the key of the mapping type is directl
indexer and slice. For the indexer. netProgramFor example, in list and tuple, we can use the list [Index] method to access the values in the list. This is very similar.. Net indexer. Slice is a part of the returned list.For example>>> A = [1, 2, 3, 4, 5]>>> A [1: 3][2, 3]Through a [], we return the sublist from the second element to the fourth element in list A.
' > >> dict.setdefault (' age ', ' a ') 18>>> dict{' Color ': ' White ', ' name ': ' Tom ', ' age ': ' Hair ': ' Black '}Pretty PrintIf you import the Pprint module in a program, you can use the Pprint () and Pformat () functions, which will print beautifully a dictionary of words.The Pprint.pprint () function is especially useful if the dictionary itself contains nested lists or dictionaries.If you want n
As you can see from the previous sections, Python is the most basic type of programming language.
List
Tuple
Set
Dict
It can be seen that the granularity of segmentation in data structure is to satisfy the need of more domain programming.1 Creating a dictionaryOther methods, such as list and tuple, can also be created, see the manual. The keys of a dictionary are non-repeatable and
a collection. The recursive construction set (set comprehension) is the correct way to resolve the problem in the previous section. We can create a set of non-mass numbers that do not have duplicate elements:>>> fromMathImportsqrt>>> n = 100>>> sqrt_n =Int (sqrt (n))>>> no_primes = {J forIinchRange (2,sqrt_n) forJinchRange (i*2, N, i)}>>>no_primes{4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40,
*7=28 5*7=35 6*7=42 7*7=49#1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64#1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81If you want to know something about the principle of list comprehension, this person's article is very clear: http://blog.chinaunix.net/uid-28631822-id-3488324.html5. Practice:Here is a topic from the Basic
A detailed introduction to the dictionary in python and a detailed introduction to the python dictionary
1. What is a dictionary?
A dictionary is the only ing type in Python.
In a ing o
list derivation, below is my test results in Ipython (test environment Python 2.7.10):
>>> long_list = range(1000)>>> a = []>>> %timeit for i in long_list: a.append(i+1)10000 loops, best of 3: 100 µs per loop>>> %timeit [i+1 for i in long_list]10000 loops, best of 3: 43.3 µs per loopA little more efficient, to see the DIS module.
types, but the key is unique and unique. The value can be unique.
>>> D = {'A': 1, 'B': 2 }>>> d {'B': 2, 'A ': 1 }>>> L = [('jonh', 18), ('nancy ', 19)] >>> d = dict (L) # create from the list containing key values >>> d {'jonh': 18, 'nancy ': 19 }>>> T = tuple (L) >>> T ('jonh', 18), ('nancy ', 19)> d = dict (T) # create a key value using a tuple >>> d {'jonh': 18, 'nancy ': 19 }>>> d = dict (x = 1, y = 3) # create with keyword parameters >>> d {'X
After the study of the list, the meta-group was studied. Tuples and lists function similarly, but tuples cannot be modified, so tuples are called read-only lists.The following is a list of string operations:Name.capitalize () #首字母大写Name.count ("a") #数列表中有几个aName.center (5o, "-") #意思是一共打印50个字符, not enough to "-" fillName.endwith ("ex") #看字符串是否以ex结尾Name.expandtabs (tabsize=30) #将tab键转换成30个空格The following is a dictionary:In dictionaries that are not indexed by location, the
Python uses lambda functionsLearning Resources
Lambda function Learning
List comprehension
Multidimensional array Initialization
Lambda functions
Python supports an interesting syntax that allows you to quickly define the minimum function for a single line. These functions, called Lambda, are borrowed from Lisp and can be use
arguments to reference external functions return "It's a callable%s"% a # directly in intrinsic function using external function parameters Return add # returns the intrinsic function itself, without returning the result of an intrinsic function call running as follows: A1 = Test (1) # Dynamically generated a function that can record an external variable 3.
',)>>> Tuple2 = ('last ',)>>> Tuple1, tuple2 = tuple2, tuple1>>> Tuple1'Last'>>> Tuple2'First'The tuple () function can use other types of data to create a metagroup:>>> Marx_list = ['groucho ', 'chico', 'harpo']>>> Tuple (marx_list)('Groucho ', 'chico', 'harpo ') Dictionary:A dictionary is similar to a list, but the order of the elements is irrelevant because they are not accessed by an offset like 0 or 1. Instead, each element has a different key )
self? A characteristic of eating, everyone is the same, everyone can eat, so this is common to everyone, there is no need to initialize the difference, butFor example, the name, everyone is different, so this name needs to initialize the function to define name, to distinguish each person. Methods, properties, functions, functions. If there is a cup, he has a variety of properties, long, wide, high, shape it has a function is used to fill the water, we call this water-loading action is called m
Dictionary and set in Python, Python dictionary set
I. Dictionary (dict)
1. Overview
The dictionary is the only ing type in Python.
You can only use immutable objects (such as strings)
Lambda functions
Python supports an interesting syntax that allows you to quickly define the minimum function for a single line. These functions, called Lambda, are borrowed from Lisp and can be used wherever functions are needed.
def f (x): Return x*2, replaced with a lambda function can be written as: G = lambda x:x*2 ' g (3) The result is 6. (Lambda x:x*2) (3
Lambda functions
Python supports an interesting syntax that allows you to quickly define the smallest function of a single line. These functions, called Lambda, are borrowed from Lisp and can be used wherever a function is needed.
def f (x): Return x*2, using a lambda function to replace what can be written as: G = lambda x:x*2 ' g (3) results are 6. (Lambda x:x*2) (3
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.