刘伟 发表于 2017-5-4 12:04:12

【Python基础】列表和元组

一.列表
  TODO :操作整理

  判断列表中是否含有某元素
  if element in list:
  or
  if element not in list:


列表类型的内建函数



  、dir([])

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__',
'__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__',
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

  list.append(obj)
将对象obj添加到list中
  list.extend(seq)
将序列seq的内容添加到list中
  list.pop(index) 
删除并返回指定位置的对象,默认删除最后一个元素
  排序


http://nemogu.iteye.com/blog/1493641

  示例代码
  test1:去掉列表中每个元素头尾的空格


freshfruit = ['banana', 'loganberry ', 'passion fruit']
list =
  test2:把列表中,大于3的元素,乘以2


list =

  test3:把列表1的每一个元素和列表2的每一个元素相乘

>>> lst1 =
>>> lst2 =
>>>

  test4.获取[0-10)的平方








map(lambda
x : x*x, range(10))
  test5.获取[0-10)中奇数的平方
  
  或

lambda
x : x%2, range(10) )]

  for in 的其他列表操作
  test6.合并两个列表
  ['a','b','c'] , ['1','2','3'] ==> ['a','b','c','1','2','3']
  list1 + list2
  or
  list1.extend(list2)
  ['a','b','c'] , ['1','2','3'] ==> [['a','b','c'],['1','2','3']]
  list.append(list1)
  list.append(list2)
  enumerate()
函数

>>> list = ['abc','def','ghi']
>>> for i, e in enumerate(list):
...   print i, e
...
0 abc
1 def
2 ghi
  test6
  zip()
函数
  title()
首字母变大写

>>> list1 = ['a','b','c']
>>> list2 = ['A','B','C']
>>> for i, j in zip(list1,list2):
...   print('%s %s' % (i, j) )
...
a A
b B
c C
>>> list1 = ['a','b','c']
>>> list2 = ['A','B','C']
>>> for i, j in zip(list1,list2):
...   print('%s %s' % (i, j) ).title()
...
A A
B B
C C
>>> list1 = ['abc','bcd','cde']
>>> list2 = ['abc','bcd','cde']
>>> for i, j in zip(list1,list2):
...   print('%s %s' % (i, j) ).title()
...
Abc Abc
Bcd Bcd
Cde Cde

   http://www.iteye.com/topic/600079


#平衡点问题,熟悉list的基本操作
li =
def main():
balances = []
length = len(li)
p = 1
while True:
if p == length-1:
break
if sum(li[:p]) == sum(li):
balances.append(li)
p +=1
return balances
if __name__ == '__main__' :
bals = main()
print bals
  支配数/支配点问题
  支配数:数组中某个元素出现的次数大于数组总数的一半时就成为支配数,其所在位序成为支配点;比如int[] a = {3,3,1,2,3};3为支配数,0,1,4分别为支配点;


要求:返回任何一个支配点


本问题可归结为众数问题(统计学范畴),即一组数据中出现次数最多的那个数值,它可以没有也可以为多个。

  questiton1:输出任意支配点即可
  question2:输出支配数及其所有支配点

//question1
li =
def main():
length = len(li)
mid = length/2
print mid
temp_li = []
for t in li:
if t not in temp_li:
temp_li.append(t)

for e in temp_li:
i = 0
count = 0
index = 0
while True:
if i == length:
break
if li == e:
count += 1
#index = i
if count > mid:
print li.index(e) ,e
break
i += 1
if __name__ == "__main__":
main()
   http://www.iteye.com/topic/106747

  http://blog.csdn.net/facevoid/article/details/5338048


二.元组
  如何比较两个列表?
  ‘==’为标准类型
对象比较
操作符,比较的是对象的数值而不是对象本身。
  ‘is’为标准类型对象身份比较
操作符。比较两个对象是否是同一个对象(两个变量是否是同一个引用)。

>>> list1 = ['xyz','abc']
>>> list2 = ['xyz','abc']
>>> list3 = ['abc','xyz']
>>> list1 == list2
True
>>> list1 == list3
False
>>> list1 > list3
True
>>> list1 is list2
False
   何时使用列表或元组?
  列表和元组怎么互相转化?如何修改元组的元素?
页: [1]
查看完整版本: 【Python基础】列表和元组