问题描述
oak
elm
tulip
redbud
birch
/plants/
allium
bellflower
ragweed
switchgrass
我要做的就是将树木和草种分成两块,这样我就可以分别称呼它们:
print(trees)
oak
elm
tulip
redbud
birch
print(herbs)
allium
bellflower
ragweed
switchgrass
正如您在示例数据中看到的那样,数据块的长度不相等,因此我必须根据分隔符“ / plants /”进行分割。如果我尝试拼接,则数据现在仅由空格分隔:
for groups in plant_data:
groups = groups.strip()
groups = groups.replace('\n\n','\n')
pos = groups.find("/plants/")
trees,herbs = (groups[:pos],groups[pos:])
print(trees)
oa
el
tuli
redbu
birc
alliu
bellflowe
ragwee
switchgras
如果我尝试简单地拆分,我会得到列表(就我的目的而言这是可以的),但它们仍不能分为两组:
for groups in plant_data:
groups = groups.strip()
groups = groups.replace('\n\n','\n')
trees = groups.split("/plants/")
print(trees)
['oak']
['elm']
['tulip']
['redbud']
['birch']
['']
['','']
['']
['allium']
['bellflower']
['ragweed']
['switchgrass']
要删除我认为是问题的空白行,我尝试了以下操作:How do I remove blank lines from a string in Python? 而且我知道,类似地在这里也有人问过用字符分割字符串:Python: split a string by the position of a character
但是我对为什么不能将两者分开感到非常困惑。
解决方法
spam = """oak
elm
tulip
redbud
birch
/plants/
allium
bellflower
ragweed
switchgrass"""
spam = spam.splitlines()
idx = spam.index('/plants/')
trees,herbs = spam[:idx-1],spam[idx+2:]
print(trees)
print(herbs)
输出
['oak','elm','tulip','redbud','birch']
['allium','bellflower','ragweed','switchgrass']
当然,除了使用idx-1,idx + 2之外,您还可以使用其他方法(例如列表理解)删除空str
spam = [line for line in spam.splitlines() if line]
idx = spam.index('/plants/')
trees,herbs = spam[:idx],spam[idx+1:]