How to use and distinguish between lists and tuples in Python _python

Source: Internet
Author: User

One or two differences

List:

1. Can add list content append

2. You can 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 to append to the list Extedn

4. You can query a list segment at the location of the entire list index

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

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

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

8. Can forward reverse sort reverse

9. You can sort by letter or number

10. Use bracket "[]" when defining the list

Note: in the list, if one of the two list segments is the same, whether using index or remove is the top-most-listed segment of the statistic

META Group:

1. You can count the number of times a tuple segment appears in the entire tuple

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

3. Use parentheses when defining tuples "()"

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 >&G T;> name_list.count (' Jack ') 2 #使用extend向列表中增加列表段 >>> name_list.extend (' hello,my name is Sean ') >>> name_list [' Sean ', ' Tom ', ' Jack ', ' Angelia ', ' Daisy ', ' Jack ', ' David ', ' H ', ' e ', ' l ', ' l ', ' o ', ', ', ', ', ', ', ', ', ', ' A ', ', ', ' e ', ', ' I ', ' s ', ', ', ', ' e ', ' a ', ' n ', #查看列表段所在的索引号, note here the statistics of Jack for the first Jack ID number >>> Name_list.index (' j Ack ') 2 >>> name_list.index (' Tom ') 1 #向索引号为2的地方插入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 ', ' n '] #删除最后一个列表段 >>> 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 that the deletion here is the first Jack >>> name_list.remove (' Jack ') >>> name_list [' Sean ', ' Tom ', ' a  Dam ', ' Angelia ', ' Daisy ', ' Jack ', ' David ', ' H ', ' e ', ' l ', ' l ', ' o ', ', ', ' m ', ' y ', ', ' n ', ' A ', ' m ', ' e ', ', ' I ', ' s ', ', ', ', ' e ', ' a '] #对整个列表进行倒序 >>> name_list.reverse () >>> name_list [' A ', ' e ', ' s ', ', ', ', ', ', ', ', ' ', ' ', '], ' , ' e ', ' m ', ' a ', ' n ', ', ' y ', ' m ', ', ', ' o ', ' l ', ' l ', ' e ', ' H ', ' David ', ' Jack ', ' Daisy ', ' Angelia ', ' Adam ', ' Tom ', ' SE An '] #对整个列表进行倒序 >>> 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 ', ', ', ', ' e ', ' a ', #对整个列表进行列表 The first letter of the paragraph is sorted >>> 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, The number of times the xiaohong appears within the tuple
>>> name_tuple.count (' xiaoming ')
2
>>> name_tuple.count (' Xiaohong ')
1
#查询xiaoming, Xiaohong, Xiaozhang ID number within a tuple
>>> 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 has no attribute ' append '

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

>>> tuple_a = (1,2,{' k1 ': ' v1 '})
>>> for I in tuple_a:
... print i ... 
1
2
{' K1 ': ' v1 '}
#更改元素
>>> tuple_a[2][' k1 '] = ' v2 '
>>> for-I in Tuple_a:
... print I ... 
1
2
{' 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.