我正在查找列表中存在的字符串,以获取字符串的索引,以便我可以将列表B的内容插入到列表A中。
我的代码说字符串不在列表中,我不知道为什么(因为它在那里!)。 THX!
HEADER = ['ROAD_ID: R1.6', 'ROAD_TYPE: Curve', 'ARC_RADIUS: 1650.000000', 'SPIRAL_LEN1: 300.000000', 'SPIRAL_LEN2: 300.000000', 'SUPERELEVATION: 0.030000', 'CONTROL_POINT: 56551.970145 41423.581706 117.415152', 'VCURVE_LEN: 1298.247591', 'VCURVE_MIN: 20.000000', 'SLOPE1: 0.020632', 'SLOPE2: 0.019981', 'PATHNAME: Default Road', 'SPEED: 45.000000', 'NO_PASSING: FALSE', 'STORE_HPR: FALSE', 'NUM_POINTS: 130']
K_WIDTH = 'WIDTH: '
W_REC = ['PROFILE_POINT: 25.500 0.000','PROFILE_POINT: -25.500 0.00','WIDTH: 51.000','CENTER2LEFT: 25.500']
what = 'PATHNAME:'
for n in HEADER:
print n, type(n)
if what in n:
print '////////////////////'
print 'Found:', what, type(n)
if not K_WIDTH in HEADER:
print 'Looking for:', what
if what in HEADER:
where = HEADER.index(what)
HEADER.insert(where,w_rec)
else:
print 'Not found', what
输出:
ROAD_ID: R1.6 <type 'str'>
ROAD_TYPE: Curve <type 'str'>
ARC_RADIUS: 1650.000000 <type 'str'>
SPIRAL_LEN1: 300.000000 <type 'str'>
SPIRAL_LEN2: 300.000000 <type 'str'>
SUPERELEVATION: 0.030000 <type 'str'>
CONTROL_POINT: 56551.970145 41423.581706 117.415152 <type 'str'>
VCURVE_LEN: 1298.247591 <type 'str'>
VCURVE_MIN: 20.000000 <type 'str'>
SLOPE1: 0.020632 <type 'str'>
SLOPE2: 0.019981 <type 'str'>
PATHNAME: Default Road <type 'str'>
////////////////////
Found: PATHNAME: <type 'str'>
SPEED: 45.000000 <type 'str'>
NO_PASSING: FALSE <type 'str'>
STORE_HPR: FALSE <type 'str'>
NUM_POINTS: 130 <type 'str'>
Looking for: PATHNAME:
Not found PATHNAME:
答案 0 :(得分:1)
我认为问题是当你定义什么时,它被定义为'PATHNAME:'。这个str本身不在列表中,它只是字符串的一部分而程序无法识别。要解决此问题,我会将内容更改为“PATHNAME:Default Road”。我在一个程序中对此进行了测试,似乎感觉它就在那里。我希望这会有所帮助。
答案 1 :(得分:1)
子字符串PATHNAME:
是名为HEADER
的字符串变量列表的一部分。因此,您必须遍历每个单独的字符串并将其与您的子字符串匹配,如if what in s for s in HEADER
,更新的代码如下所示:
HEADER = ['ROAD_ID: R1.6', 'ROAD_TYPE: Curve', 'ARC_RADIUS: 1650.000000', 'SPIRAL_LEN1: 300.000000', 'SPIRAL_LEN2: 300.000000', 'SUPERELEVATION: 0.030000', 'CONTROL_POINT: 56551.970145 41423.581706 117.415152',
'VCURVE_LEN: 1298.247591', 'VCURVE_MIN: 20.000000', 'SLOPE1: 0.020632', 'SLOPE2: 0.019981', 'PATHNAME: Default Road', 'SPEED: 45.000000', 'NO_PASSING: FALSE', 'STORE_HPR: FALSE', 'NUM_POINTS: 130']
K_WIDTH = 'WIDTH: '
W_REC = ['PROFILE_POINT: 25.500 0.000', 'PROFILE_POINT: -25.500 0.00',
'WIDTH: 51.000', 'CENTER2LEFT: 25.500']
what = 'PATHNAME:'
for n in HEADER:
print n, type(n)
if what in n:
print '////////////////////'
print 'Found:', what, type(n)
if not K_WIDTH in HEADER:
print 'Looking for:', what
where = [HEADER.index(i) for i in HEADER if what in i]
if (where):
HEADER.insert(where[0], w_rec)
else:
print 'Not found', what
答案 2 :(得分:0)
您可以尝试迭代并查找您的子字符串是否匹配
<强>代码强>
HEADER = ['ROAD_ID: R1.6', 'ROAD_TYPE: Curve', 'ARC_RADIUS: 1650.000000', 'SPIRAL_LEN1: 300.000000', 'SPIRAL_LEN2: 300.000000', 'SUPERELEVATION: 0.030000', 'CONTROL_POINT: 56551.970145 41423.581706 117.415152', 'VCURVE_LEN: 1298.247591', 'VCURVE_MIN: 20.000000', 'SLOPE1: 0.020632', 'SLOPE2: 0.019981', 'PATHNAME: Default Road', 'SPEED: 45.000000', 'NO_PASSING: FALSE', 'STORE_HPR: FALSE', 'NUM_POINTS: 130']
K_WIDTH = 'WIDTH: '
W_REC = ['PROFILE_POINT: 25.500 0.000','PROFILE_POINT: -25.500 0.00','WIDTH: 51.000','CENTER2LEFT: 25.500']
what = 'PATHNAME:'
for n in HEADER:
print n, type(n)
if what in n:
print '////////////////////'
print 'Found:', what, type(n)
if not K_WIDTH in HEADER:
print 'Looking for:', what
found = 0
for i in range(0,len(HEADER)):
if what in HEADER[i]:
found = 1
HEADER.insert(i,W_REC)
if found == 0:
print 'Not found', what