我被卡住了,我有一个类似于这样填充的字符串列表:
['one', 'two', 'asdf', 'asdf', 'stuff', 'other', 'asdf', 'other stuff', 'asdf','asdf']
此列表不断变化,但一个不变的是“asdf”将始终存在,第一个 asdf 之前和之后的数据会发生变化。
我的问题是我需要在第一个 asdf 之前删除列表中的所有字符串(在示例中是一个和两个) 或在第一个 asdf 之后计算所有内容。
我正在使用:
data2 = normalize_line_endings(data)
data3 = data2.split('\n')
data3 = list(map(lambda x: str(x) if x else 'asdf' , data3))
print(data3)
target_ibdex = data3.rindex('asdf')
target_ibdex2 = target_ibdex
print(data3[:target_ibdex - target_ibdex2])
但是当它运行时,它使用最后一个 asdf,所以它只是删除整个字符串。
我需要:
a=['one', 'two', 'asdf', 'asdf', 'stuff', 'other', 'asdf', 'other stuff', 'asdf','asdf']
b = code to delete everything before FIRST asdf
len(b)
where b's value is now 8 instead of 10. since one,two got removed.
请您参考如下方法:
您可以使用 list.index()喜欢:
代码:
b = a[a.index('asdf'):]
测试代码:
a = ['one', 'two', 'asdf', 'asdf', 'stuff', 'other', 'asdf', 'other stuff',
'asdf', 'asdf']
b = a[a.index('asdf'):]
print(b)
结果:
['asdf', 'asdf', 'stuff', 'other', 'asdf', 'other stuff', 'asdf', 'asdf']
