[Python 3.6.2] 5.1 Data Structure -- LIST METHODS
来源:互联网 发布:JavaScript post 编辑:程序博客网 时间:2024/06/14 21:15
Python官网截取的data structure的用法
5.1 List
here are all the methods of list objects:
- list.append() # add an item to the end of the list. equivalent to a[len(a):] = [x]
- list.extend(iterable) # extend the list by appending all the items from the iterable
[Notes]: the difference between append and extend!
list.append method appends an object to the end of the list. Whatever the object is (a string, another list, or sth else), it gets added onto the end of the original list as a single entry.
my_list = ['foo', 'bar']
my_list.append('baz')
my_list
>> ['foo','bar','baz']
another_list = [1,2,3]
my_list .append(another_list)
my_list
>>['foo','bar','baz',[1,2,3]]
The list.extend() method extends a list by appending elements from an iterable:
my_list.extend(anoher_list)
my_list
>>['foo','bar',1,2,3]
my_list.extend('baz')
my_list
>>['foo','bar',1,2,3,'b', 'a', 'z']
- list.insert(i,x) #insert an item at a given position. i is the index of the element before which to insert, x iss the item to be inserted.
a.insert(0, x) inserts at the front of the list
a.insert(len(a), x) is equivalent to a.append(x)
- list.remove(x) # remove the first item from the list whose value is X. It is an error if there is no such item.
- list.pop([i]) #remove the item at the given position in the list and return it. if no index, a.pop() removes and returns the last item in the list. Note that: the squre brackets around i means: the parameter is optional (此处.pop([i])中的中括号表示.pop()的参数是可选项,而不表示参数需要加上中括号,其他地方用法相同)
- list.clear() #remove all items from the list.Equivalent todel a[ : ]
- list.index(x[, start[,end]]) # return 0-based index in the list of the first item whose value is x. Error raised if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is compulated relative to the beginning of the full sequencce rather than the start argument.
- list.count(x) # return the number of times x appears in the list.
- list.sort(key = None, reverse = False) #sort the items of the list in place ( the arguments can be used for sort customization, seesorted for their explanation)
- list.reverse() #reverse the elments of the list in place
- list.copy() #return a shallow copy of the list. Equivalent to a[ : ]
Example:
fruits = ['apple', 'banana', 'orange', 'watermelon', 'pear', 'banana','apple','orange','apple']
print(fruits.count('apple'))
>> 3
print(fruits.index('apple',3))
>>6
print(fruits)
>>['apple', 'banana', 'orange', 'watermelon', 'pear', 'banana', 'apple', 'orange', 'apple']
fruits.reverse()
print(fruits)
>>['apple', 'orange', 'apple', 'banana', 'pear', 'watermelon', 'orange', 'banana', 'apple']
fruits.sort()
print(fruits)
>>['apple', 'apple', 'apple', 'banana', 'banana', 'orange', 'orange', 'pear', 'watermelon']
print(fruits.pop(2))
>>apple
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'orange', 'orange', 'pear', 'watermelon'] #前面.pop删除了位置2处的apple.
fruits.insert(4,'grape')
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'grape', 'orange', 'orange', 'pear', 'watermelon']
vegetable = ['potato', 'tomato']
fruits.extend(vegetable)
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'grape', 'orange', 'orange', 'pear', 'watermelon', 'potato', 'tomato']
fruits.append(vegetable)
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'grape', 'orange', 'orange', 'pear', 'watermelon', 'potato', 'tomato', ['potato', 'tomato']]
fruits.clear()
print(fruits)
>>[]
阅读全文
0 0
- [Python 3.6.2] 5.1 Data Structure -- LIST METHODS
- data structure in python
- Python data structure
- python data structure
- HackerRank-Data Structure-linked list
- Single Linked List--Data Structure
- Double Linked List--Data Structure
- Data Structure(2)
- C++ Data Structure 2
- Data Structure Alignment 2
- Implementing the skip list data structure
- Data Structure Study: Linked List(Part A)
- Python 3.3 Tutorial Notes - 3:Data Structure
- [Python for Data Analysis]Python Basic2--Data Structure and Sequences
- 42 data structure day-2
- PCM data flow - 2 - ASoC data structure
- Data Structure
- Data structure
- PL/SQL:WHILE循环:打印2-200之间的素数
- 51nod【1381】硬币游戏【思维】
- pygame安装
- 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- dp基础7-- [HDU-1087] [Problem E]
- [Python 3.6.2] 5.1 Data Structure -- LIST METHODS
- 欢迎使用CSDN-markdown编辑器
- 基于spring-boot的邮件发送的代码
- 微信小程序开发
- 在maven上搭建Springmvc步骤
- 第八章 javascript函数笔记摘要
- 自相关与互相关的理解与物理意义
- java多线程售票小例子
- RocketMQ——其他注意事项