我想创建一个必须遵守以下规则的列表:所有行最多应包含16列。 我的代码如下:
n14=[]
lastrow14=0
for i in range(0,len(nodes)):
if float(nodes[i][1])<=float(maxX)/2 and float(nodes[i][2])<=float(maxY)/2:
if len(n14)==0 or len(n14)>=1 and len(n14[lastrow14][0])<=15:
n14.append(nodes[i][0])
else:
n14.append([nodes[i][0]])
lastrow14=lastrow14+1
但结果(列表n14
)不是我想要的。结果如下:
1
2
3
4
5
6
7
8
9
1, 0
1, 1
1, 2
1, 3
1, 4
1, 5
1, 6
1, 7
1, 8
1, 9
2, 0
2, 1
2, 2
2, 3
2, 4
9, 7
9, 8
9, 9
1, 0, 0
1, 0, 1
1, 1, 9
1, 2, 0
1, 2, 1
1, 2, 2
1, 2, 3
1, 4, 1
1, 4, 2
1, 4, 3
1, 4, 4
1, 4, 5
1, 6, 3
1, 6, 4
1, 6, 5
1, 6, 6
1, 6, 7
1, 8, 5
1, 8, 6
1, 8, 7
1, 8, 8
1, 8, 9
2, 0, 7
2, 0, 8
2, 0, 9
2, 1, 0
2, 1, 1
2, 1, 2
2, 1, 8
2, 1, 9
2, 2, 0
2, 2, 1
2, 2, 2
2, 2, 3
2, 4, 0
2, 4, 1
2, 4, 2
2, 4, 3
2, 4, 4
2, 4, 5
2, 6, 2
2, 6, 3
2, 6, 4
2, 6, 5
2, 6, 6
2, 6, 7
2, 8, 4
2, 8, 5
2, 8, 6
2, 8, 7
2, 8, 8
2, 8, 9
4, 0, 1
maxX=1300
maxY=1300
nodes=[['1', '0', '0', '-100'], ['2', '0', '0', '0'], ['3', '0', '0', '80'], ['4', '0', '0', '1080'], ['5', '0', '0', '2080'], ['6', '0', '0', '3080'], ['7', '0', '0', '3580'], ['8', '0', '0', '4000'], ['9', '0', '0', '4100'], ['10', '0', '0', '280'], ['11', '0', '0', '480'], ['12', '0', '0', '680'], ['13', '0', '0', '880'], ['14', '0', '0', '1280'], ['15', '0', '0', '1480'], ['16', '0', '0', '1680'], ['17', '0', '0', '1880'], ['18', '0', '0', '2280'], ['19', '0', '0', '2480'], ['20', '0', '0', '2680'], ['21', '0', '0', '2880'], ['22', '0', '0', '3246.66675'], ['23', '0', '0', '3413.33325'], ['24', '0', '0', '3790'], ['25', '0', '1300', '-100'], ['26', '0', '1300', '0'], ['27', '0', '1300', '80'], ['28', '0', '1300', '1080'], ['29', '0', '1300', '2080'], ['30', '0', '1300', '3080'], ['31', '0', '1300', '3580'], ['32', '0', '1300', '4000'], ['33', '0', '1300', '4100'], ['34', '0', '1300', '280'], ['35', '0', '1300', '480'], ['36', '0', '1300', '680'], ['37', '0', '1300', '880'], ['38', '0', '1300', '1280'], ['39', '0', '1300', '1480'], ['40', '0', '1300', '1680'], ['41', '0', '1300', '1880'], ['42', '0', '1300', '2280'], ['43', '0', '1300', '2480'], ['44', '0', '1300', '2680'], ['45', '0', '1300', '2880'], ['46', '0', '1300', '3246.66675'], ['47', '0', '1300', '3413.33325'], ['48', '0', '1300', '3790'], ['49', '1300', '0', '-100'], ['50', '1300', '0', '0'], ['51', '1300', '0', '80'], ['52', '1300', '0', '1080'], ['53', '1300', '0', '2080'], ['54', '1300', '0', '3080'], ['55', '1300', '0', '3580'], ['56', '1300', '0', '4000'], ['57', '1300', '0', '4100'], ['58', '1300', '0', '280'], ['59', '1300', '0', '480'], ['60', '1300', '0', '680'], ['61', '1300', '0', '880'], ['62', '1300', '0', '1280'], ['63', '1300', '0', '1480'], ['64', '1300', '0', '1680'], ['65', '1300', '0', '1880'], ['66', '1300', '0', '2280'], ['67', '1300', '0', '2480'], ['68', '1300', '0', '2680'], ['69', '1300', '0', '2880'], ['70', '1300', '0', '3246.66675'], ['71', '1300', '0', '3413.33325'], ['72', '1300', '0', '3790'], ['73', '1300', '1300', '-100'], ['74', '1300', '1300', '0'], ['75', '1300', '1300', '80'], ['76', '1300', '1300', '1080'], ['77', '1300', '1300', '2080'], ['78', '1300', '1300', '3080'], ['79', '1300', '1300', '3580'], ['80', '1300', '1300', '4000'], ['81', '1300', '1300', '4100'], ['82', '1300', '1300', '280'], ['83', '1300', '1300', '480'], ['84', '1300', '1300', '680'], ['85', '1300', '1300', '880'], ['86', '1300', '1300', '1280'], ['87', '1300', '1300', '1480'], ['88', '1300', '1300', '1680'], ['89', '1300', '1300', '1880'], ['90', '1300', '1300', '2280'], ['91', '1300', '1300', '2480'], ['92', '1300', '1300', '2680'], ['93', '1300', '1300', '2880'], ['94', '1300', '1300', '3246.66675'], ['95', '1300', '1300', '3413.33325'], ['96', '1300', '1300', '3790'], ['97', '0', '0', '80'], ['98', '100', '0', '80'], ['99', '300', '0', '80'], ['100', '400', '0', '80'], ['101', '500', '0', '80'], ['102', '700', '0', '80'], ['103', '800', '0', '80'], ['104', '900', '0', '80'], ['105', '1000', '0', '80'], ['106', '1200', '0', '80'], ['107', '1300', '0', '80'], ['108', '0', '1300', '80'], ['109', '100', '1300', '80'], ['110', '300', '1300', '80'], ['111', '400', '1300', '80'], ['112', '500', '1300', '80'], ['113', '700', '1300', '80'], ['114', '800', '1300', '80'], ['115', '900', '1300', '80'], ['116', '1000', '1300', '80'], ['117', '1200', '1300', '80'], ['118', '1300', '1300', '80'], ['119', '0', '0', '1080'], ['120', '200', '0', '1080'], ['121', '300', '0', '1080'], ['122', '400', '0', '1080'], ['123', '500', '0', '1080'], ['124', '700', '0', '1080'], ['125', '800', '0', '1080'], ['126', '900', '0', '1080'], ['127', '1100', '0', '1080'], ['128', '1200', '0', '1080'], ['129', '1300', '0', '1080'], ['130', '0', '1300', '1080'], ['131', '200', '1300', '1080'], ['132', '300', '1300', '1080'], ['133', '400', '1300', '1080'], ['134', '500', '1300', '1080'], ['135', '700', '1300', '1080'], ['136', '800', '1300', '1080'], ['137', '900', '1300', '1080'], ['138', '1100', '1300', '1080'], ['139', '1200', '1300', '1080'], ['140', '1300', '1300', '1080'], ['141', '0', '0', '2080'], ['142', '200', '0', '2080'], ['143', '300', '0', '2080'], ['144', '400', '0', '2080'], ['145', '600', '0', '2080'], ['146', '700', '0', '2080'], ['147', '800', '0', '2080'], ['148', '900', '0', '2080'], ['149', '1100', '0', '2080'], ['150', '1200', '0', '2080'], ['151', '1300', '0', '2080'], ['152', '0', '1300', '2080'], ['153', '200', '1300', '2080'], ['154', '300', '1300', '2080'], ['155', '400', '1300', '2080'], ['156', '600', '1300', '2080'], ['157', '700', '1300', '2080'], ['158', '800', '1300', '2080'], ['159', '900', '1300', '2080'], ['160', '1100', '1300', '2080'], ['161', '1200', '1300', '2080'], ['162', '1300', '1300', '2080'], ['163', '0', '0', '3080'], ['164', '200', '0', '3080'], ['165', '300', '0', '3080'], ['166', '400', '0', '3080'], ['167', '600', '0', '3080'], ['168', '700', '0', '3080'], ['169', '800', '0', '3080'], ['170', '1000', '0', '3080'], ['171', '1100', '0', '3080'], ['172', '1200', '0', '3080'], ['173', '1300', '0', '3080'], ['174', '0', '1300', '3080'], ['175', '200', '1300', '3080'], ['176', '300', '1300', '3080'], ['177', '400', '1300', '3080'], ['178', '600', '1300', '3080'], ['179', '700', '1300', '3080'], ['180', '800', '1300', '3080'], ['181', '1000', '1300', '3080'], ['182', '1100', '1300', '3080'], ['183', '1200', '1300', '3080'], ['184', '1300', '1300', '3080'], ['185', '0', '0', '3580'], ['186', '200', '0', '3580'], ['187', '300', '0', '3580'], ['188', '400', '0', '3580'], ['189', '600', '0', '3580'], ['190', '700', '0', '3580'], ['191', '800', '0', '3580'], ['192', '1000', '0', '3580'], ['193', '1100', '0', '3580'], ['194', '1200', '0', '3580'], ['195', '1300', '0', '3580'], ['196', '0', '1300', '3580'], ['197', '200', '1300', '3580'], ['198', '300', '1300', '3580'], ['199', '400', '1300', '3580'], ['200', '600', '1300', '3580'], ['201', '700', '1300', '3580'], ['202', '800', '1300', '3580'], ['203', '1000', '1300', '3580'], ['204', '1100', '1300', '3580'], ['205', '1200', '1300', '3580'], ['206', '1300', '1300', '3580'], ['207', '0', '0', '80'], ['208', '0', '130', '80'], ['209', '0', '260', '80'], ['210', '0', '390', '80'], ['211', '0', '520', '80'], ['212', '0', '650', '80'], ['213', '0', '780', '80'], ['214', '0', '910', '80'], ['215', '0', '1040', '80'], ['216', '0', '1170', '80'], ['217', '0', '1300', '80'], ['218', '0', '0', '3580'], ['219', '0', '130', '3580'], ['220', '0', '260', '3580'], ['221', '0', '390', '3580'], ['222', '0', '520', '3580'], ['223', '0', '650', '3580'], ['224', '0', '780', '3580'], ['225', '0', '910', '3580'], ['226', '0', '1040', '3580'], ['227', '0', '1170', '3580'], ['228', '0', '1300', '3580'], ['229', '1300', '0', '80'], ['230', '1300', '130', '80'], ['231', '1300', '260', '80'], ['232', '1300', '390', '80'], ['233', '1300', '520', '80'], ['234', '1300', '650', '80'], ['235', '1300', '780', '80'], ['236', '1300', '910', '80'], ['237', '1300', '1040', '80'], ['238', '1300', '1170', '80'], ['239', '1300', '1300', '80'], ['240', '0', '0', '1080'], ['241', '0', '130', '1080'], ['242', '0', '260', '1080'], ['243', '0', '390', '1080'], ['244', '0', '520', '1080'], ['245', '0', '650', '1080'], ['246', '0', '780', '1080'], ['247', '0', '910', '1080'], ['248', '0', '1040', '1080'], ['249', '0', '1170', '1080'], ['250', '0', '1300', '1080'], ['251', '1300', '0', '1080'], ['252', '1300', '130', '1080'], ['253', '1300', '260', '1080'], ['254', '1300', '390', '1080'], ['255', '1300', '520', '1080'], ['256', '1300', '650', '1080'], ['257', '1300', '780', '1080'], ['258', '1300', '910', '1080'], ['259', '1300', '1040', '1080'], ['260', '1300', '1170', '1080'], ['261', '1300', '1300', '1080'], ['262', '0', '0', '2080'], ['263', '0', '130', '2080'], ['264', '0', '260', '2080'], ['265', '0', '390', '2080'], ['266', '0', '520', '2080'], ['267', '0', '650', '2080'], ['268', '0', '780', '2080'], ['269', '0', '910', '2080'], ['270', '0', '1040', '2080'], ['271', '0', '1170', '2080'], ['272', '0', '1300', '2080'], ['273', '1300', '0', '2080'], ['274', '1300', '130', '2080'], ['275', '1300', '260', '2080'], ['276', '1300', '390', '2080'], ['277', '1300', '520', '2080'], ['278', '1300', '650', '2080'], ['279', '1300', '780', '2080'], ['280', '1300', '910', '2080'], ['281', '1300', '1040', '2080'], ['282', '1300', '1170', '2080'], ['283', '1300', '1300', '2080'], ['284', '0', '0', '3080'], ['285', '0', '130', '3080'], ['286', '0', '260', '3080'], ['287', '0', '390', '3080'], ['288', '0', '520', '3080'], ['289', '0', '650', '3080'], ['290', '0', '780', '3080'], ['291', '0', '910', '3080'], ['292', '0', '1040', '3080'], ['293', '0', '1170', '3080'], ['294', '0', '1300', '3080'], ['295', '1300', '0', '3080'], ['296', '1300', '130', '3080'], ['297', '1300', '260', '3080'], ['298', '1300', '390', '3080'], ['299', '1300', '520', '3080'], ['300', '1300', '650', '3080'], ['301', '1300', '780', '3080'], ['302', '1300', '910', '3080'], ['303', '1300', '1040', '3080'], ['304', '1300', '1170', '3080'], ['305', '1300', '1300', '3080'], ['306', '1300', '0', '3580'], ['307', '1300', '130', '3580'], ['308', '1300', '260', '3580'], ['309', '1300', '390', '3580'], ['310', '1300', '520', '3580'], ['311', '1300', '650', '3580'], ['312', '1300', '780', '3580'], ['313', '1300', '910', '3580'], ['314', '1300', '1040', '3580'], ['315', '1300', '1170', '3580'], ['316', '1300', '1300', '3580'], ['401', '0', '0', '4100'], ['402', '0', '1300', '4100'], ['403', '1300', '0', '4100'], ['404', '1300', '1300', '4100']]
我想要的结果是:
nodeID, x, y, z= nodes row
n14=[[nodeID1,nodeID2,....., nodeID16], [nodeID17,nodeID2,....., nodeID33],[....]]
#for all rows where x<=maxX/2 and y<=maxY/2
答案 0 :(得分:3)
[已修改以修正某些错误]
要理解你想要做什么有点难,但也许你正在寻找这样的东西:
n14=[[]]
for line in nodes:
if int(line[1])*2 <= maxX and int(line[2])*2 <= maxY:
if len(n14[-1]) == 16:
n14.append([])
n14[-1].append(line[0])
或更好:
s = [line[0] for line in nodes if int(line[1])*2 <= maxX and int(line[2])*2 <= maxY]
n14 = [s[i:i+16] for i in range(0, len(s), 16)]