In [1]: one = [1,2,3] In [2]: two = [4,5,6] In [3]: one + two Out[3]: [1, 2, 3, 4, 5, 6]
两个list一起打乱
1 2 3 4
import random c = list(zip(a, b)) random.shuffle(c) a[:], b[:] = zip(*c)
判断一个list是否为空
1 2 3 4 5 6 7 8 9 10
传统的方式: if len(mylist): # Do something with my list else: # The list is empty 由于一个空 list 本身等同于 False,所以可以直接: if mylist: # Do something with my list else: # The list is empty
声明不定长的list
1
centre_list = [0for x in xrange(self.number_class)]
声明不定长的空list
1 2
devide_list = [[]]*2 [[], []]
但是这样的话,不能用append,如下:
1 2 3 4
In[14]: one[1].append('1') In[15]: one Out[15]: [['1'], ['1'], ['1'], ['1'], ['1']]