Python将两个列表合并成一个列表

Python将两个列表/数组合并成一个列表/数组

  1. extend方法
>>>c1 = ["Red","Green","Blue"]
>>>c2 = ["Orange","Yellow","Indigo"]
>>>c1.extend(c2)
>>>c1 #["Red","Green","Blue","Orange","Yellow","Indigo"] 
  1. 直接相加
>>>c1 = ["Red","Green","Blue"]
>>>c2 = ["Orange","Yellow","Indigo"]
>>>c3 = c1 + c2
>>>c3 #  ["Red","Green","Blue","Orange","Yellow","Indigo"]
  1. array, 多维数组变成一维数组,flatten()函数用法:
    flatten是numpy.ndarray.flatten的一个函数,即返回一个一维数组。
    flatten只能适用于numpy对象,即array或者mat,普通的list列表不适用。
    a.flatten():a是个数组,a.flatten()就是把a降到一维,默认是按行的方向降 。
    这种方法多用于复杂算法方面,在此只做了解,按照此方法,我们可以先将列表转换成数组,降维后再tolist()

你可能感兴趣的:(笔记,Python)