python: data structure-list

  •  
  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
  • In Python lists are written with square brackets.

 

  • to creat a list:
aList =['a','b','c']

>>> aList
['a','b','c']

>>> aList[0]
'a'

# or use list constructor list()

aList=list(('a','b','c'))
  • loop through a list; check if a list has a cerntain item:
aList =['a','b','c']
for cha in aList:
    print(cha)
if 'c' in aList:
    print("Yes")
  • length of a list: 
>>> len(aList)
3
  • add an item to the list:
>>> aList.append('d')
  • remove an item from the list:
>>> aList.remove('d')
  • remove an item at the specific index:
>>> del aList[0]

>>> del aList
  • remove the specific index item or the last item if the index is not specified:
>>> aList.pop(1)

>>> aList.pop()
  • empty a list:
>>> aList.clear

  • it's not recommended to use list for collecting different attributes of individual items, instead, for this case, tuple, named tuple, dictionaries and objects would be more suitable.
  • method sort() can be used to sort the elements in the list.
  • method __lt__ (lt: less than)

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值