How to use list and metadata in Python

Source: Internet
Author: User
This article mainly introduces the usage of lists and metadata groups in Python and detailed information about the differences. For more information, see the following:

List:

1. added the list content append.

2. count the number of times a list segment appears in the entire list

3. you can insert a string and split each letter of the entire string as a list segment and append it to extedn in the list.

4. you can query the index of a list segment in the whole list.

5. insert a list segment at a specified position

6. you can delete the last list segment pop.

7. remove a list segment from the specified list

8. reverse can be sorted in reverse order.

9. sort can be sorted by letters or numbers

10. use brackets "[]" to define the list.

Note: in the list, if two list segments are the same, both index and remove are the top list segments in statistics.

Tuples:

1. you can count the number of times a single segment appears in the entire tuples.

2. you can query the meta-group number index of a specific segment in the entire tuples.

3. use parentheses "()" when defining tuples "()"

II. usage of the two

List

# Definition list> name_list = ['Sean ', 'Tom', 'Jack', 'angelia ', 'Daisy ', 'Jack'] # view the definition list> name_list ['Sean ', 'Tom', 'Jack', 'angelia ', 'Daisy ', 'Jack'] # add the david list section> name_list.append ('David')> name_list ['Sean ', 'Tom', 'Jack', 'angelia ', 'Daisy', 'Jack', 'David'] # count the number of occurrences of the david list segment >>> name_list.count ('David') 1 >>> name_list.count ('Jack ') 2 # Use extend to add a list segment to the list> name_list.extend ('Hello, My name is sean ')> name_list ['Sean', 'Tom ', 'Jack', 'angelia ', 'Daisy', 'Jack', 'David', 'H', 'e', 'L', 'L', 'O ', ',', 'M', 'y', '', 'n', 'A', 'M', 'e','', 'I ','s ', '','s', 'e', 'A', 'A', 'n'] # view the index number of the list segment, note that jack is counted as the first jack ID >>> name_list.index ('Jack') 2 >>> name_list.index ('Tom ') 1 # insert Adam> name_list.insert (2, 'Adam ')> name_list ['Sean', 'Tom ', 'Adam ', 'Jack', 'angelia ', 'Daisy', 'Jack', 'David', 'H', 'e', 'L', 'L', 'O ', ',', 'M', 'y', '', 'n', 'A', 'M', 'e','', 'I ','s ', '','s', 'e', 'A', 'A', 'n'] # delete the last list segment >>> name_list.pop () 'n'> name_list ['Sean ', 'Tom', 'Adam ', 'Jack', 'angelia', 'Daisy', 'Jack', 'David ', 'H', 'e', 'L', 'L', 'O', 'M', 'y', '', 'n ', 'A', 'M', 'e', ',' I ', 'S',', 'S', 'e ', 'A'] # Delete the specified list segment. Note that the first jack is deleted here> name_list.remove ('Jack') >>> name_list ['Sean ', 'Tom ', 'Adam ', 'angelia', 'Daisy', 'Jack', 'David', 'H', 'e', 'L', 'L', 'O ', ',', 'M', 'y', '', 'n', 'A', 'M', 'e','', 'I ','s ', '','s', 'e', 'A'] # Reverse Order of the entire list >>> name_list.reverse () >>> name_list ['A ', 'E', 'S', ',' s ',' I ',', 'e', 'e', 'M', 'A', 'n ', '', 'y', 'M', ',', 'O', 'L', 'L', 'e', 'H', 'David ', 'Jack', 'Daisy', 'angelia ', 'Adam', 'Tom ', 'Sean'] # Sort the entire list in reverse order >>>> name_list.reverse () >>> name_list ['Sean ', 'Tom', 'Adam ', 'angelia', 'Daisy', 'Jack', 'David', 'H', 'e ', 'L', 'L', 'O', ', 'M', 'y', '', 'n', 'A', 'M ', 'E', '', 'I','s ',','s ', 'e ', 'A'] # sort the first letter of the entire list segment> name_list.sort ()> name_list ['','','',',', 'Adam ', 'angelia', 'Daisy', 'H', 'M', 'A', 'A', 'David ', 'e', 'e ', 'E', 'I', 'Jack', 'L', 'L', 'M', 'n', 'O', 'S ', 'Sean ', 'Tom', 'y'] >>>

Tuples

# Define the tuples name_tuple >>> name_tuple = ('xiaoming', 'xiaohong ', 'Xiaoli', 'xiaozhang', 'xiaoming') >>> name_tuple ('xiaoming ', 'xiaohong', 'Xiaoli', 'xiaozhang', 'xiaoming') # count the number of times xiaoming and xiaohong appear in the meta-group> name_tuple.count ('xiaoming ') 2 >>> name_tuple.count ('xiaohong ') 1 # query the IDs of xiaoming, xiaohong, and xiaozhang in The MetaBase >>>> name_tuple.index ('xiaoming ') 0 >>> name_tuple.index ('xiaohong ') 1 >>>> name_tuple.index ('xiaozhang') 3 >># try to add a tuples >>> name_tuple.append ('xiaowang ') traceback (most recent call last): File"
 
  
", Line 1, in
  
   
Name_tuple.append ('xiaowang ') AttributeError: 'tuple' object has no attribute 'append' >>>
  
 

The elements of the tuples are immutable, and the elements of the tuples are immutable.

>>> Tuple_A = (1, 2, {'k1 ': 'v1'}) >>> for I in tuple_A :... print I... 12 {'k1 ': 'v1'} # Change element >>> tuple_A [2] ['k1 '] = 'V2' >>> for I in tuple_A :... print I... 12 {'k1 ': 'V2'} >>>

For more information about how to use the list and metadata in Python and how to use the metadata, see PHP!

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: info-contact@alibabacloud.com 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.