python basic
python basic 数据类型 list array, 列表, 数组 list/dict str to list/dict >>> import ast >>> x = '[ "A","B","C" , " D"]' >>> x = ast.literal_eval(x) >>> x ['A', 'B', 'C', ' D'] >>> x = [n.strip() for n in x] >>> x ['A', 'B', 'C', 'D'] colon syntax # check if two list equal sorted(a) == sorted(b) : is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [start:end] [1:5] is equivalent to "from 1 to 5" (5 not included) [1:] is equivalent to "1 to end" [:20] from start to 20 # Python 合并两个列表 # 法一: # Python合并两个列表, 相加是拼接 list1=[1,2,3] list2=[4,5,6,7] list3=list1+list2 print('list3',list3)#输出[1,2,3,4,5,6,7] # 注意:Python合并两个NumPy数组,相加时候是对应相加 import numpy as np arr1=np.array([1,2,3]) arr2=np.array([4,5,6]) arr3=arr1+arr2 print(arr3)#输出[5 7 9] #那么NumPy数组怎么拼接呢,使用concatenate arr4=np.concatenate((arr1,arr2),axis=0) print('arr4',arr4) # 法二: l3=[] l1=[1,2,3] l2=[4,5,6] l3.append(l1) l3.append(l2) print('l3',l3)#输出[[1, 2, 3], [4, 5, 6]],注意这里是二维列表,不是我们想要的结果 # 如何才能达到我们要的结果,使用 extend l1.extend(l2) print('l1',l1)#[1, 2, 3, 4, 5, 6] # 总结: # Python合并两个列表,可用两个列表相加存入新列表,或者使用extend在原列表修改 ———————————————— ...