How to use and distinguish between lists and tuples in Python

Source: Internet
Author: User
One or two differences

List:

1. You can add the contents of the list append

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

3. You can insert a string and append each letter of the entire string as a list segment to the list Extedn

4. You can query the position of a list segment in the entire list index

5. You can insert a list segment at a specified location

6. You can delete the last list segment of a list pop

7. You can delete a list segment in the specified list remove

8. You can reverse sort reverse

9. Sort by letter or number

10. When defining a list, use the brackets "[]"

Note: In the list, if two list segments are the same, whether you use index or remove is the top-most list segment of the statistic

Meta-group:

1. You can count the number of occurrences of a tuple segment in an entire tuple

2. You can query the tuple number of a tuple segment in an entire tuple index

3. Defining tuples with parentheses "()"

Two or two ways to use them

List

#定义列表 >>> name_list = [' Sean ', ' Tom ', ' Jack ', ' Angelia ', ' Daisy ', ' Jack '] #查看定义的列表 >>> name_list[' Sean ', ' Tom ', ' Jack ', ' Angelia ', ' Daisy ', ' Jack '] #增加david列表段 >>> name_list.append (' David ') >>> name_list[' Sean ', ' Tom ', ' Jack ', ' Angelia ', ' Daisy ', ' Jack ', ' David '] #统计david列表段出现次数 >>> name_list.count (' David ') 1> >> name_list.count (' Jack ') use extend to add list segments 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 ', ' n '] #查看列表段所在的索引号, note that Jack here counts for the first Jack ID number >>> Name_list.index ( ' Jack ') 2>>> Name_list.index (' Tom ') insert Adam>>> Name_list.insert (2, ' Adam ') in place of index number 2 >>>  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 ', ' N ' #删除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 '] #删除指定列表段, note this Removed is the first jack>>> 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 '] #对整个列表进  Line reverse >>> name_list.reverse () >>> name_list[' A ', ' e ', ' s ', ', ', ' s ', ' I ', ' ', ' e ', ' m ', ' a ', ' n ', ', ' Y ', ' M ', ', ', ' o ', ' l ', ' l ', ' e ', ' H ', ' David ', ' Jack ', ' Daisy ', ' Angelia ', ' Adam ', ' Tom ', ' Sean '] #对整个列表进行倒序 >>> 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 '] #对整个列表进行列表段的首字母进行排序 >>> name_list.sort ( ) >>> name_list[', ',"', ', ', ' Adam ', ' Angelia ', ' Daisy ', ' H ', ' m ', ' a ', ' a ', ' David ', ' e ', ' e ', ' e ', ' I ', ' Jack ', ' l ', ' l ', ' m ', ' n ', ' o ', ' s ', ' s ', ' Sean ', ' Tom ', ' Y ']>>>

Meta-group

#定义元组name_tuple >>> name_tuple = (' xiaoming ', ' xiaohong ', ' Xiaoli ', ' Xiaozhang ', ' xiaoming ') >>> name_ Tuple (' xiaoming ', ' xiaohong ', ' Xiaoli ', ' Xiaozhang ', ' xiaoming ') #统计xiaoming, xiaohong occurrences within tuples >>> name_ Tuple.count (' xiaoming ') 2>>> name_tuple.count (' Xiaohong ') # query Xiaoming, xiaohong, Xiaozhang ID numbers within tuples > >> name_tuple.index (' xiaoming ') 0>>> name_tuple.index (' Xiaohong ') 1>>> name_tuple.index (' Xiaozhang ') 3>>> #尝试增加一个元组单元 >>> name_tuple.append (' Xiaowang ') Traceback (most recent call last): File "<pyshell#49>", line 1, in <module>name_tuple.append (' Xiaowang ') Attributeerror: ' Tuple ' object have no Attribute ' append ' >>>

The elements of the tuple are immutable, and the elements of the tuple elements are mutable

>>> tuple_a = (1,2,{' k1 ': ' v1 ') >>> for i in tuple_a: ... print I ... 12{' K1 ': ' v1 '} #更改元素 >>> tuple_a[2][' k1 '] = ' v2 ' >>> for i in tuple_a: ... print I ... 12{' K1 ': ' V2 '}>>>

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.