Python meta group list and dictionary learning Notes

Source: Internet
Author: User
Tags extend in python


This article describes three combinations of data types in Python, and the second half of the article describes how to use "fragmentation" to fetch data in these combined data types.

Article catalog

0x1. META Group

In Python, tuples are created using brackets, if the parentheses contain only one element, you need to add a comma at the end of the element (no comma data will be created as a string or numeric data type), and after the tuple is created, you cannot add the elements that are deleted, and the values are fixed, see the following example:

#创建三个元组, the B-tuple contains only one string element, so you need to add a comma at the end, otherwise B will be created as a string object, and the C tuple is a multidimensional tuple containing A and B tuples
>>> a= (1,2,3,4,5)
>>> b= ("",)
>>> c= (a,b)

>>> Print (a)
(1, 2, 3, 4, 5)
>>> print (b)
(' ',)
>>> Print (c)
((1, 2, 3, 4, 5), (' ',))

#打印出a元组第一个元素, Python's index starts at 0.
>>> print (a[0])

#打印出a元组中最后一个元素, the Len () function Gets the number of a-tuple elements because the index starts at 0, so the number of elements is reduced by one, which is the index number of the last element.
>>> Print (A[len (a)-1])

#另一种简便的获取元组中最后一个元素的方法, direct use-1, and so on, get the penultimate element to use-2 as the index value
>>> print (a[-1])
>>> print (A[-2])

#获取多维元组中, the value of index 2 in the No. 0 element (a tuple), which is 3 of the a sub-tuple.
>>> print (c[0][2])
0x2. List

Python, you can use the brackets to create the list, and most of the actions in the tuple instance above apply to the list. Instead of adding parentheses to brackets, the difference is that when you include only one element in the list, you don't need to add commas to the end, and you can add or remove elements from the list, consider the following example:

#创建三个列表, where C is a multidimensional list, containing A and b
>>> a=[1,2,3,4,5]
>>> b=[""]
>>> C=[a,b]

>>> Print (a)
[1, 2, 3, 4, 5]
>>> print (b)
[' ']
>>> Print (c)
[[1, 2, 3, 4, 5], [' ']]

#使用list. Append () method, add an element to a list 6
>>> A.append (6)
>>> Print (a)
[1, 2, 3, 4, 5, 6]

The #append () method can only add a single element at a time, while the List.extend () method adds multiple elements at a time
>>> a.extend ([7,8,9])
>>> Print (a)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#利用extend () method, you can copy an element from a list to another list, create an empty list D, and then copy the elements in the C list to D
>>> d=[]
>>> D.extend (c)
>>> Print (d)
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [' ']]


          #使用list. Remove () method deletes an element from the list, and this method receives not the index value. Instead, the element value (this example deletes the elements 1 and 2 in a list directly)
          >>> a.remove (1)
          >>> A.remove (2)
           >>> Print (a)
          [3 , 4, 5, 6, 7, 8, 9]


          #list. Pop () method receives an index value that defaults to the index value of the last element if the index value is not specified. This method takes out the corresponding element and then deletes the element from the list
          >>> print (A.pop ())
           >>> Print (a)
          [3, 4, 5, 6, 7, 8]
& nbsp;         >>> Print (a.pop (0))
          >>> print (a)
          [4, 5, 6, 7, 8]

#使用set () can delete duplicate values in the list
>>> e=["A", "a", "B", "C", "B"]
>>> Print (e)
[' A ', ' a ', ' B ', ' C ', ' B ']

>>> E=set (E)
>>> Print (e)
{' A ', ' B ', ' C '}
0x3. Dictionary

In Python, you can create a dictionary with braces, and each element in the dictionary is stored in the form of a "key value pair", see the following example:

#有两种方法可以创建字典, the first is to create an empty dictionary, and then add the key values individually
>>> a={}
>>> a["Breakfast"]= "milk eggs"
>>> a["Lunch"]= "Cola Steak"
>>> a["Dinner"]= "fruit salad"
>>> Print (a)
{' Breakfast ': ' Milk eggs ', ' dinner ': ' Fruit salad ', ' Lunch ': ' Cola Steak '}

#第二种方法创建字典, add all key values at once, separate each group of elements with a colon, "key" before the colon, and "value" after the colon.
>>> a={' breakfast ': ' Milk eggs ', ' dinner ': ' Fruit salad ', ' Lunch ': ' Cola Steak '}

#python允许不同的键拥有相同的值, so the following syntax is correct
b={"one": "Qing", "two": "Qing"}

#有两种方法可以读取字典中的值, use the dictionary [key] to read the value directly, or use the dictionary. Get (key) to read the value
>>> Print (a["breakfast"])
Milk and eggs
>>> Print (A.get ("Lunch"))
Cola Steak

>>> print (A.keys ())
Dict_keys ([' Breakfast ', ' dinner ', ' Lunch '])
>>> print (A.values ())
Dict_values ([' Milk eggs ', ' fruit salad ', ' cola Steak '])
0x4. Data Fragment Example

In Python, you can pass data fragments, the idea of reading a single character in a segment of string is also applicable to tuples and lists, and if a string is stored in the list, the first few characters of an element can be removed by slicing technology, which is useful in some environments, see the following example:

#首先来看python对字符串数据的分片提取方法, this example takes out a single character with an index position of 2 in the string data that a points to
>>> a= "ABCDEFG"
>>> print (a[2])

#在列表数据中, you can think of each string element as a child list, using the idea of multidimensional lists to extract values from the corresponding index in the child list
>>> b=["", "ABCDEFG", "12345678"]
>>> print (b[0][4])

>>> print (b[2][-1])
Using string fragmentation techniques, the first letter of each element in a set of lists is extracted as a key to the dictionary and corresponds to the value of this element, and the following is a more complete program:

#!/usr/bin/env python3
           a=["Qingsword", "John", "Tom", "George", "Kali", "Eva"]
          # Create an empty dictionary
          while X<len (a):
           print (b)  

{' E ': ' Eva ', ' G ': ' George ', ' Q ': ' Qingsword ', ' K ': ' Kali ', ' J ': ' John ', ' T ': ' Tom '}
In addition to the simple slicing technology described above, Python also provides a special slicing technique, semicolon slicing, see the following example:

#分号切片同样适用于字符串或元组列表, syntax is "[Start index: End index (not included)]"

The position of the #从字符串a索引为2 (3) begins slicing until the position of index 6 (7), which intercepts the data output (contains the starting index value, does not contain the end index value)
>>> a= "123456789"
>>> print (A[2:6])

The position of the

          #从b中索引为3 (d) begins until the location of index 5 (f)
           >>> b=["A", "B", "C", "D", "E", "F", "G"]
           >>> Print (B[3:5])
           [' d ', ' e ']

Related Article

Contact Us

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: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.