xjqj.net
当前位置:首页 >> python list >>

python list

除了直接相加(生成新的list),还有两种方法(修改其中一个list): 用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部,例如: >>> L1 = [1, 2, 3, 4, 5]>>> L2 = [20, 30, 40]>>> L1.extend(L2)>>> L1[1, 2, 3, 4, 5,...

$ python Python 2.7.2+ (default, Jul 20 2012, 22:12:53) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a=raw_input(' ') [ ['0'], ['0'] ] >>> a "[ ['0'], ['0'] ]" >>> eval(a) [['...

for index, value in enumerate(alist): if value==0: alist[index]=1

l=[1,2,3,4,1] #目标数列targetnum=1 #元素number=l.count(targetnum)print('个数: '+str(number))i=numberindex=[]while i>0: for x in range(len(l)): if l[x]==targetnum: index.append(x+1) i=i-1print('位置(第几个):'+str(index))

标准写法: listVal = []; for i in range(100): listVal.append(0); 快速写法1: listVal = [[0]*100]; 快速写法2: listZero = [0] listVal = listZero * 100;

list=[[1,2,3],[4,5,6],[7,8,9]] f=open('demo.txt','w') for i in list: k=' '.join([str(j) for j in i]) f.write(k+"\n") f.close()

def getnewlist(alist): result=[] if len(alist)==0: return [] elif len(alist)

lst = [1, 2, 3, 4, 5]num = 1for i in lst: num *= iprint num

words=[1,2,3,4]for w in words[:]: print(w)#在python中,words[:]代码复制words对象到另外一个内存空间。还有类似的, words[1:],复制从第一个元素到最后一个,words[:1],复制第0个元素到第一个(不包含)。

>>> a = [["a"],["b","c"],["d"],["e","f"]]>>> for i in eval('itertools.product' + str(tuple(a))):print i('a', 'b', 'd', 'e')('a', 'b', 'd', 'f')('a', 'c', 'd', 'e')('a', 'c', 'd', 'f')感觉这样应该最简单,而且兼容性比较好。不得不...

网站首页 | 网站地图
All rights reserved Powered by www.xjqj.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com