纯爱战士Dylan1 发表于 2023-3-1 21:52:53

关于set的添加元素和删除元素的问题!

求大佬看看哪里错了!
# Use the set f_suburbs as given as your starting point. Then,
# 1. Remove all suburbs that don't start with a F
# 2. Ensure that the suburbs listed below are in your set
#         Fairfield
#         Fairfield East
#         Fairfield Heights
#         Fairfield West
#         Fairlight
#         Fiddletown
#         Five Dock
#         Flemington
#         Forest Glen
#         Forest Lodge
#         Forestville
#         Freemans Reach
#         Frenchs Forest
#         Freshwater
f_suburbs = {"Randwick", "Kensington", "Frenchs Forest", "Flemington"}
f_suburbs.remove("Randwick")
f_suburbs.remove("Kensington")
f_suburbs.extend{'Frenchs Forest', 'Flemington', 'Freshwater', 'Fairfield West', 'Five Dock', 'Fairfield Heights', 'Fairfield', 'Forest Lodge', 'Fairlight', 'Fairfield East', 'Freemans Reach', 'Forest Glen', 'Fiddletown', 'Forestville'}
print(f_suburbs)

isdkz 发表于 2023-3-1 21:56:29

本帖最后由 isdkz 于 2023-3-1 21:59 编辑

集合没有 extend 方法,你可以用 |= 或者 update 来扩展集合,而且使用方法不要把小括号漏掉了:

f_suburbs = {"Randwick", "Kensington", "Frenchs Forest", "Flemington"}
f_suburbs.remove("Randwick")
f_suburbs.remove("Kensington")
f_suburbs |= {'Frenchs Forest', 'Flemington', 'Freshwater', 'Fairfield West', 'Five Dock', 'Fairfield Heights', 'Fairfield', 'Forest Lodge', 'Fairlight', 'Fairfield East', 'Freemans Reach', 'Forest Glen', 'Fiddletown', 'Forestville'}
print(f_suburbs)



f_suburbs = {"Randwick", "Kensington", "Frenchs Forest", "Flemington"}
f_suburbs.remove("Randwick")
f_suburbs.remove("Kensington")
f_suburbs.update({'Frenchs Forest', 'Flemington', 'Freshwater', 'Fairfield West', 'Five Dock', 'Fairfield Heights', 'Fairfield', 'Forest Lodge', 'Fairlight', 'Fairfield East', 'Freemans Reach', 'Forest Glen', 'Fiddletown', 'Forestville'})
print(f_suburbs)
页: [1]
查看完整版本: 关于set的添加元素和删除元素的问题!