为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕侠2389804 2023-02-22 19:12:56
我试图将给定数据中的花朵和灌木分离到两个单独的列表中。这是我的代码: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)灌木和花的列表显示是空的。为什么??
查看完整描述

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(" - ")清晰的字符串。


查看完整回答
反对 回复 2023-02-22
?
紫衣仙女

TA贡献1839条经验 获得超15个赞

正如 Thierry Lathuille 指出的那样,您还剩下空格。首先尝试替换所有空格:

for i in data:
    i.replace(" ", "")
    x=i.split("-")


查看完整回答
反对 回复 2023-02-22
?
白板的微信

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)


查看完整回答
反对 回复 2023-02-22
?
互换的青春

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 ']

那个原始代码没有


查看完整回答
反对 回复 2023-02-22
?
慕码人2483693

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)


查看完整回答
反对 回复 2023-02-22
  • 5 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信