选择列表中的元素
Selecting an element in a list
我有一个由两只乌龟组成的列表(为简单起见,houses
和 landlords
)。
感谢这里提供的一些建议,我 select 从列表中以最高价格购买了房子,但不幸的是房东不是那所房子的主人,而只是乌龟 select 打了一个勾前。
我知道这只乌龟的定义出了问题。
根据建议,我写的是:
let selected_house max-one-of turtle-set my-list [price]
let selected_landlord [my-landlord] of selected_house
print (word "Landlord" [selected_landlord] of selected_house)
let new_list (list selected_house selected_landlord)
print (word "Landlord " selected_landlord "of house: " selected_house " by price: " [price] of selected_house)
但是,假设我有:
名单:
"Landlord 2 is the owner of house 4 by price 400000"
"Landlord 5 is the owner of house 1 by price 100000"
"Landlord 4 is the owner of house 3 by price 300000"
然后我用selected_house/landlord
给select价格最高的房子的主人,我应该得到(house 4, landlord 2)
;但是,我得到 (house 4, landlord 5)
,即楼主 select 之前打了一个勾。
我对房东和房子的定义如下(我有类似的练习,但主题不同):
if breed = landlords [
hatch-houses n_house [
set price precision (random-float 1) 3
set this-house self
set this-landlord myself
ask myself [
set my-list fput (list this-house this-landlord) my-list
]
]
其中 this-house
和 this-landlord
是全局变量,my-list
是 landlords' own
。
你能帮我理解并解决这部分代码的问题吗?
我认为,当我创建第一个元素 = 房子和第二个元素 = 房东的列表时,可以添加一些条件使 selection 尽可能简单(对于例如,一旦 selecting 第一项,即价格最高的房子,将列表中每个元素的第二项设置为房屋的房东)。
我不确定为什么 this-house
和 this-landlord
是全局变量,我认为(没有完整的代码继续)这可能是你的问题。如果我理解你的模型,每个房东都有一套房子,你希望能够找到最贵的房子和拥有它的房东。如果每个房子都记录其房东和价格,并且每个房东都记录他们的房子,那么这应该允许您 link 任何房子给房东,或者查看房东拥有的所有房屋。
下面的模型可能会给您一个关于如何进行的想法。它创造了房东,然后每个房东都以随机价格建造房屋。然后它会找到最贵的房子及其房东。然后对于每个房东,它找到房东最昂贵的房子。房东可以用 [房东] 保留一份房屋清单,但第二项对于房东的所有房屋都是相同的。
breed [landlords landlord]
breed [houses house]
landlords-own [my-houses my-house-list]
houses-own [price my-landlord]
to test
clear-all
create-landlords 10
ask landlords [
hatch-houses 5 [
set price precision (random-float 1) 3
set my-landlord myself
]
set my-houses houses with [my-landlord = myself]
]
let most-expensive max-one-of houses [price]
show (word "The mostexpensive house is house " most-expensive " owned by " [my-landlord] of most-expensive " with price =" [price] of most-expensive)
ask landlords [
let my-most-expensive max-one-of my-houses [price]
show (word "landlord " self "'s most expensive house is house " most-expensive " with price =" [price] of my-most-expensive)
]
end
另一方面,您可以将所有房东的所有房屋的列表作为全局保存,如果您 运行 list-houses
在测试后,您将得到这样一个列表,您然后可以按价格、房东或其他任何方式排序。
globals [house-list]
to list-houses
set house-list []
ask houses [
set house-list fput (list self my-landlord price) house-list
]
show house-list
let sorted-by-price sort-by [[a b] -> item 2 a > item 2 b] house-list
let most-expensive first sorted-by-price
show (word "the most expensive house is house " item 0 most-expensive " with owner " item 1 most-expensive " and price " item 2 most-expensive)
end
我有一个由两只乌龟组成的列表(为简单起见,houses
和 landlords
)。
感谢这里提供的一些建议,我 select 从列表中以最高价格购买了房子,但不幸的是房东不是那所房子的主人,而只是乌龟 select 打了一个勾前。
我知道这只乌龟的定义出了问题。
根据建议,我写的是:
let selected_house max-one-of turtle-set my-list [price]
let selected_landlord [my-landlord] of selected_house
print (word "Landlord" [selected_landlord] of selected_house)
let new_list (list selected_house selected_landlord)
print (word "Landlord " selected_landlord "of house: " selected_house " by price: " [price] of selected_house)
但是,假设我有:
名单:
"Landlord 2 is the owner of house 4 by price 400000"
"Landlord 5 is the owner of house 1 by price 100000"
"Landlord 4 is the owner of house 3 by price 300000"
然后我用selected_house/landlord
给select价格最高的房子的主人,我应该得到(house 4, landlord 2)
;但是,我得到 (house 4, landlord 5)
,即楼主 select 之前打了一个勾。
我对房东和房子的定义如下(我有类似的练习,但主题不同):
if breed = landlords [
hatch-houses n_house [
set price precision (random-float 1) 3
set this-house self
set this-landlord myself
ask myself [
set my-list fput (list this-house this-landlord) my-list
]
]
其中 this-house
和 this-landlord
是全局变量,my-list
是 landlords' own
。
你能帮我理解并解决这部分代码的问题吗?
我认为,当我创建第一个元素 = 房子和第二个元素 = 房东的列表时,可以添加一些条件使 selection 尽可能简单(对于例如,一旦 selecting 第一项,即价格最高的房子,将列表中每个元素的第二项设置为房屋的房东)。
我不确定为什么 this-house
和 this-landlord
是全局变量,我认为(没有完整的代码继续)这可能是你的问题。如果我理解你的模型,每个房东都有一套房子,你希望能够找到最贵的房子和拥有它的房东。如果每个房子都记录其房东和价格,并且每个房东都记录他们的房子,那么这应该允许您 link 任何房子给房东,或者查看房东拥有的所有房屋。
下面的模型可能会给您一个关于如何进行的想法。它创造了房东,然后每个房东都以随机价格建造房屋。然后它会找到最贵的房子及其房东。然后对于每个房东,它找到房东最昂贵的房子。房东可以用 [房东] 保留一份房屋清单,但第二项对于房东的所有房屋都是相同的。
breed [landlords landlord]
breed [houses house]
landlords-own [my-houses my-house-list]
houses-own [price my-landlord]
to test
clear-all
create-landlords 10
ask landlords [
hatch-houses 5 [
set price precision (random-float 1) 3
set my-landlord myself
]
set my-houses houses with [my-landlord = myself]
]
let most-expensive max-one-of houses [price]
show (word "The mostexpensive house is house " most-expensive " owned by " [my-landlord] of most-expensive " with price =" [price] of most-expensive)
ask landlords [
let my-most-expensive max-one-of my-houses [price]
show (word "landlord " self "'s most expensive house is house " most-expensive " with price =" [price] of my-most-expensive)
]
end
另一方面,您可以将所有房东的所有房屋的列表作为全局保存,如果您 运行 list-houses
在测试后,您将得到这样一个列表,您然后可以按价格、房东或其他任何方式排序。
globals [house-list]
to list-houses
set house-list []
ask houses [
set house-list fput (list self my-landlord price) house-list
]
show house-list
let sorted-by-price sort-by [[a b] -> item 2 a > item 2 b] house-list
let most-expensive first sorted-by-price
show (word "the most expensive house is house " item 0 most-expensive " with owner " item 1 most-expensive " and price " item 2 most-expensive)
end