嘿大家,问题是这样的,老实说,这是家庭作业问题,但我只是被卡住了,它永远存在。我的问题是,由于城市在每一行中都有不同的索引位置,我们如何逐行迭代并将城市名称返回到列表中?(任何可以让我开始的帮助将不胜感激,我并不懒惰,我确实已经为此工作了几个小时)
citylist(filename) reads a file in the DOT format
and returns a list of city names, one for each line in
the file. The DOT format may have trailing spaces on
the city name; make sure citylist creates a list of
city names with trailing spaces removed (easy using
Python's strip() method). Two test cases are shown
below, for lines at index 3 and 347.
citylist("DOT500.txt")[3]
'TUCSON'
citylist("DOT500.txt")[347]
'NORTH VILLE'
以下是 DOT 文件(其中的一部分)
1 958164 TOYOTA MOTOR CORPORATION TOYOTA LAND CRUISER 1994 19941223 N 0 0 SERVICE BRAKES, HYDRAULIC:ANTILOCK ARNOLD CA JT3DJ81W8R0 19950103 19950103 ABS SYSTEM FAILURE, AT 20MPH. TT EVOQ V
2 958156 TOYOTA MOTOR CORPORATION TOYOTA PASEO 1994 Y 19941226 N 0 0 PARKING BRAKE:CONVENTIONAL SAN JOSE CA JT2EL45U5R0 19950103 19950103 1 PARKED ON FLAT SURFACE EMERGENCY BRAKING ENGAGED VEHICLE ROLLED REARWARD. TT EVOQ V
3 958124 TOYOTA MOTOR CORPORATION TOYOTA COROLLA 1994 Y 19941128 N 0 0 AIR BAGS:FRONTAL PHOENIX AZ 19950103 19950103 UPON FRONTAL COLLISION, AIR BAG FAILED TO DEPLOY. VEHICLE CLASSIFIED AS TOTALED. PLEASE DESCRIBE DETAILS. TT EVOQ V
4 958122 NISSAN NORTH AMERICA, INC. NISSAN MAXIMA 1994 19950103 N 0 0 SUSPENSION TUCSON AZ JN1HJ01F4RT 19950103 19950103 THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION. TT EVOQ V
5 958122 NISSAN NORTH AMERICA, INC. NISSAN MAXIMA 1994 19950103 N 0 0 ENGINE AND ENGINE COOLING:ENGINE TUCSON AZ JN1HJ01F4RT 19950103 19950103 THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION. TT EVOQ V
6 958247 NISSAN NORTH AMERICA, INC. NISSAN PATHFINDER 1994 N 0 0 SUSPENSION:FRONT ABINGDON MD 19950103 19950103 FRONT TIRES SHOW EXCESSIVE WEAR AND STEEL BELTS. NISSAN IS OFFERING TO REPLACE TWO TIRES. TT EVOQ V
7 958143 FORD MOTOR COMPANY FORD MUSTANG 1991 19941119 Y 0 0 SEATS:FRONT ASSEMBLY:POWER ADJUST PENSICOLA FL 19950103 19950103 DRIVER SIDE POWER SEAT CAUGHT FIRE. TT EVOQ V
8 958141 DAIMLERCHRYSLER CORPORATION CHRYSLER NEW YORKER 1989 19950103 N 0 0 POWER TRAIN:AUTOMATIC TRANSMISSION LEVITTOWN NY 1C3BC4639KD 19950103 19950103 2 TRANSMISSION NOT SHIFTING OUT OF 2ND GEAR, WENT INTO LIM MODE/TRANSMMION NOT SHIFTING THEN JUMPED OUT OF GEAR. *AK EVOQ V
9 958128 DAIMLERCHRYSLER CORPORATION DODGE SHADOW 1990 Y 19941224 N 0 0 SERVICE BRAKES, HYDRAULIC:ANTILOCK WILMINGTON DE 1B3XT44KXLN 19950103 19950103 1 ABS: EXPERIENCED ANTI-LOCK BRAKE FAILURE/EXTENDED STOPPING DISTANCE, RESULTING IN AN ACCIDENT, IMPACT 12:00 POSITION SPEED, 20 MPH. TT EVOQ V
10 958128 DAIMLERCHRYSLER CORPORATION DODGE SHADOW 1990 Y 19940412 N 0 0 SUSPENSION:FRONT WILMINGTON DE 1B3XT44KXLN 19950103 19950103 1 MISALIGNMENT, CAUSING VEHICLE TO VEER TO THE CENTER. *AK EVOQ V
11 958128 DAIMLERCHRYSLER CORPORATION DODGE SHADOW 1990 Y 19941117 N 0 0 ENGINE AND ENGINE COOLING:ENGINE WILMINGTON DE 1B3XT44KXLN 19950103 19950103 1 ENGINE MOTOR MOUNTS FAILED, RESULTING IN ENGINE NOISE. *AK EVOQ V
12 958168 GENERAL MOTORS CORP. CHEVROLET SUBURBAN 1995 Y 19950101 N 0 0 POWER TRAIN:AUTOMATIC TRANSMISSION:GEAR POSITION INDICATION (PRNDL) SHAFTFBURG MI 19950103 19950103 VEHICLE ROLLED DOWN HILL WHILE IN PARK, AND INTO A TREE. TT EVOQ V
13 958169 GENERAL MOTORS CORP. GEO METRO 1995 Y 19941225 N 1 0 SERVICE BRAKES, HYDRAULIC:FOUNDATION COMPONENTS NORFOLK VA 19950103 19950103 BRAKES WOULD NOT WORK ON SEVERAL OCCASIONS, LAST INSTANCE RESULTED IN AN ACCIDENT. TT EVOQ