[python] 列表解析式的高效与简洁
方法一(列表解析式):
list1 = ["abc","efg","hij"]
list2 = [i[0] for i in list1]
print list2
方法二(普通写法):
list1 = ["abc","efg","hij"] list2 = [] for i in range(len(list1)): list2.append(list1[i][0]) print list2
输出:
['a', 'e', 'h'] ['a', 'e', 'h']
方法一和方法二输出相同,是等价的