5 回答
TA贡献1801条经验 获得超8个赞
用连字符拆分时,您需要查看第二项。您还应该使用 listappend方法,extend这样您就不会附加字符序列。
data = ["Andromeda - Shrub","Bellflower - Flower","China Pink - Flower","Daffodil - Flower","Evening Primrose - Flower","French Marigold - Flower","Hydrangea - Shrub","Iris - Flower","Japanese Camellia - Shrub","Lavender - Shrub","Lilac- Shrub", "Magnolia - Shrub","Peony - Shrub","Queen Anne's Lace - Flower","Red Hot Poker - Flower","Snapdragon - Flower","Sunflower - Flower","Tiger Lily - Flower","Witch Hazel - Shrub"]
flowers = []
shrubs = []
for i in data:
flower, flower_type = i.split("-")
print(flower)
if "Shrub" in flower_type:
shrubs.append(flower)
elif "Flower" in flower_type:
flowers.append(flower)
print("Shrubs")
print(shrubs)
print("Flowers")
print(flowers)
最好使用,i.split(" - ")但您有一个条目“Lilac- Shrub”,但与此不匹配。如果这不仅仅是错字,请修复它,然后更喜欢使用更i.split(" - ")清晰的字符串。
TA贡献1839条经验 获得超15个赞
正如 Thierry Lathuille 指出的那样,您还剩下空格。首先尝试替换所有空格:
for i in data: i.replace(" ", "") x=i.split("-")
TA贡献1883条经验 获得超3个赞
您可以使用stripPython 字符串的方法来清除无用空间中的数据:
data = ["Andromeda - Shrub","Bellflower - Flower","China Pink - Flower","Daffodil - Flower","Evening Primrose - Flower","French Marigold - Flower","Hydrangea - Shrub","Iris - Flower","Japanese Camellia - Shrub","Lavender - Shrub","Lilac- Shrub","Magnolia - Shrub","Peony - Shrub","Queen Anne's Lace - Flower","Red Hot Poker - Flower","Snapdragon - Flower","Sunflower - Flower","Tiger Lily - Flower","Witch Hazel - Shrub"]
flowers = []
shrubs = []
for i in data:
x = [elm.strip() for elm in i.split("-")]
print(x)
if "Shrub" in x:
shrubs.extend(x)
elif "Flower" in x:
flowers.extend(x)
print(shrubs)
print(flowers)
TA贡献1797条经验 获得超6个赞
你可以试试
data = ["Andromeda - Shrub","Bellflower - Flower","China Pink - Flower","Daffodil - Flower","Evening Primrose - Flower","French Marigold - Flower","Hydrangea - Shrub","Iris - Flower","Japanese Camellia - Shrub","Lavender - Shrub","Lilac- Shrub","Magnolia - Shrub","Peony - Shrub","Queen Anne's Lace - Flower","Red Hot Poker - Flower","Snapdragon - Flower","Sunflower - Flower","Tiger Lily - Flower","Witch Hazel - Shrub"]
flowers = []
shrubs = []
for i in data:
x=i.split("-")
if "Shrub" == x[1].strip():
shrubs.append(x[0])
elif "Flower" == x[1].strip():
flowers.append(x[0])
print(shrubs)
print(flowers)
输出
['Andromeda ', 'Hydrangea ', 'Japanese Camellia ', 'Lavender ', 'Lilac', 'Magnolia ', 'Peony ', 'Witch Hazel ']
['Bellflower ', 'China Pink ', 'Daffodil ', 'Evening Primrose ', 'French Marigold ', 'Iris ', "Queen Anne's Lace ", 'Red Hot Poker ', 'Snapdragon ', 'Sunflower ', 'Tiger Lily ']
那个原始代码没有
TA贡献1860条经验 获得超9个赞
您忘记了语句中的空格split:
data = ["Andromeda - Shrub","Bellflower - Flower","China Pink - Flower","Daffodil - Flower","Evening Primrose - Flower","French Marigold - Flower","Hydrangea - Shrub","Iris - Flower","Japanese Camellia - Shrub","Lavender - Shrub","Lilac- Shrub","Magnolia - Shrub","Peony - Shrub","Queen Anne's Lace - Flower","Red Hot Poker - Flower","Snapdragon - Flower","Sunflower - Flower","Tiger Lily - Flower","Witch Hazel - Shrub"]
flowers = []
shrubs = []
for i in data:
x=i.split(" - ")
print(x)
if "Shrub" in x:
shrubs.extend(x)
elif "Flower" in x:
flowers.extend(x)
print(shrubs)
print(flowers)
添加回答
举报