gpt4 book ai didi

.net - 从自定义类型 F# 的列表中过滤不同的值

转载 作者:行者123 更新时间:2023-12-05 00:44:11 26 4
gpt4 key购买 nike

给定一个整数列表,我们可以执行以下操作来获取不同值的列表。

let myList = [ 1; 2; 2; 3; 4; 3 ]
let distinctList = myList |> Seq.distinct |> List.ofSeq

但是,在我的例子中,我有一个定义如下的自定义类型列表:

type Listing(fromDate:NSDate, toDate:NSDate, pricePerNight:int, landlordId:String, landlordName:String, listingId:String, pic1url:NSUrl, pic2url:NSUrl, pic3url:NSUrl, pic4url:NSUrl, pic5url:NSUrl, postcode:String, latitude:Double, longitude:Double, location:String, partnername:String,partnerid:String) = 
member this.fromDate : NSDate = fromDate
member this.toDate : NSDate = toDate
member this.pricePerNight : int = pricePerNight
member this.landlordId : String = landlordId
member this.landlordName : String = landlordName
member this.listingId : String = listingId
member this.pic1url : NSUrl = pic1url
member this.pic2url : NSUrl = pic2url
member this.pic3url : NSUrl = pic3url
member this.pic4url : NSUrl = pic4url
member this.pic5url : NSUrl = pic5url
member this.postcode : String = postcode
member this.latitude : Double = latitude
member this.longitude : Double = longitude
member this.location : String = location
member this.partnername : String = partnername
member this.partnerid : String = partnerid

接下来我有一个定义如下的列表:

let mutable listings : List<Listing> = List.Empty

现在我想过滤此列表以仅获取唯一值(如果值具有唯一的 listingId,则该值是唯一的)。我可以用天真的方式做到这一点并将所有 listingId 添加到字符串列表中,对字符串列表执行不同的操作,然后提取具有不同列表中的 id 的列表,但我认为必须有更好的方法。

最佳答案

您可以使用 Seq.distinctBy使用 lambda 函数来获取您想要的唯一性值:

listings |> Seq.distinctBy (fun l -> l.listingId)

关于.net - 从自定义类型 F# 的列表中过滤不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48975980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com