为什么我的列表在使用拆分功能后是空的?

我试图将给定数据中的花朵和灌木分离到两个单独的列表中。这是我的代码:


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)

灌木和花的列表显示是空的。为什么??


慕侠2389804
浏览 144回答 5
5回答

蝴蝶刀刀

用连字符拆分时,您需要查看第二项。您还应该使用 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(" - ")清晰的字符串。

紫衣仙女

正如 Thierry Lathuille 指出的那样,您还剩下空格。首先尝试替换所有空格:for i in data:     i.replace(" ", "")     x=i.split("-")

白板的微信

您可以使用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)

互换的青春

你可以试试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 ']那个原始代码没有

慕码人2483693

您忘记了语句中的空格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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python