python元组输出平方_【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)

删除并返回指定位置的对象,默认删除最后一个元素

排序

示例代码

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

freshfruit = [' banana', ' loganberry ', 'passion fruit ']

list = [str.strip() for str in freshfruit]

test2:把列表中,大于3的元素,乘以2

list = [2,4,6]

[x*2 for x in list if x>2]

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

>>> lst1 = [2, 4, 6]

>>> lst2 = [4, 3, -9]

>>> [x*y for x in lst1 for y in lst2]

[8, 6, -18, 16, 12, -36, 24, 18, -54]

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

[x**2 for x in range(10)]

map(lambda

x : x*x, range(10))

test5.获取[0-10)中奇数的平方

[x**2 for x in range(10) if x%2==1]

[x**2 for x in filter( 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 = [1,3,5,7,8,25,4,19, 1]

def main():

balances = []

length = len(li)

p = 1

while True:

if p == length-1:

break

if sum(li[:p]) == sum(li[p+1:]):

balances.append(li[p])

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 = [1,3,4,3,3]

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[i] == 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

何时使用列表或元组?

列表和元组怎么互相转化?如何修改元组的元素?

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-03-07 00:11

浏览 1107

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值