csv文件中包含多行数据(list),每个list由多个元组对组成,根据元组将原始list拆分为多个list
"(39.985392, 116.327128)","(39.984514000000004, 116.327198)","(39.983788, 116.326986)","(39.982999, 116.32693799999998)","(39.982023999999996, 116.327024)","(39.980923, 116.327221)","(39.980173, 116.32709299999999)","(39.979889, 116.32708500000001)","(39.979338, 116.327068)","(39.978390000000005, 116.327197)","(39.977582, 116.327279)","(39.976667, 116.327246)","(39.975746, 116.327243)","(39.974952, 116.327278)","(39.974037, 116.32740600000001)","(39.973134, 116.32741899999999)","(39.972286, 116.327426)","(39.971323, 116.327548)"
[39.985392, 39.984514000000004, 39.983788, 39.982999, 39.982023999999996, 39.980923, 39.980173, 39.979889, 39.979338, 39.978390000000005, 39.977582, 39.976667, 39.975746, 39.974952, 39.974037, 39.973134, 39.972286, 39.971323]
[116.327128, 116.327198, 116.326986, 116.32693799999998, 116.327024, 116.327221, 116.32709299999999, 116.32708500000001, 116.327068, 116.327197, 116.327279, 116.327246, 116.327243, 116.327278, 116.32740600000001, 116.32741899999999, 116.327426, 116.327548]
import csv
datafile = "./test.csv"
with open(datafile, 'r') as read_csv:
reader = csv.reader(read_csv)
for row in reader:
row_list = list(row)
coor_list = []
for elem in row_list:
elem_tuple = tuple(eval(elem))
coor_list.append(elem_tuple)
lat,lng = map(list, zip(*coor_list))
csv文件中包含多行数据(list),每个list由多个元组对组成,根据元组将原始list拆分为多个list"(39.985392, 116.327128)","(39.984514000000004, 116.327198)","(39.983788, 116.326986)","(39.982999, 116.32693799999998)","(39.982023999999996, 116.327024)","(39.980923, 116.327221)","(39.980173, 116.32
如果我们需要将一个列表按指定数目分成多个列表:比如[1,2,3,4,5,6,7,8,9,10]分成[1,2,3][4,5,6][7,8,9][10],我们可以建立一个列表分割的函数split_list.py。
def list_of_groups(init_list, children_list_len):
list_of_groups = zip(*(iter(init_list),) *children_list_len)
end_list = [list(i) for i in list_of_groups]
count = len(init_list) % childre
print(list_a) # 打印结果 (1, 9, 10, 6) 元组是带小括号的
list_b = list(list_a)
print(list_b) # 打印结果 [1, 9, 10, 6] 列表是带中括号的
元组拆包可以应用到任何可迭代对象上(str、list、dict、set),唯一的硬性要求是,被可迭代对象中的元素数量必须要跟接受这些元素的元组的空档数一致。除非我们用*来表示忽略多余的元素
平行赋值拆包:
完全拆包,左侧变量必须和元组元素数量一致;
# 平行赋值
city, year, pop, chg, area = ('Tokyo', 2003, 32450, 0.66, 8014)
print(city, year, pop, chg, area) # Tokyo...