如何比较列表元素和下一个元素,以产生这个元素?

时间:2015-06-29 19:32:47

标签: scala

正如我在标题中所指出的,如何将索引N的元素与索引N + 1的元素进行比较,如果比较的元素完全相同,则只生成一次yield元素。 我知道我可以使用toSet来获取一组独特的元素,但这对我没有帮助,因为我的列表可以包含重复的元素,但重复的元素不能是我列表中的下一个元素。

val ll = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)
// Desired output: List(1, 2, 3, 6, 3, 7, 5, 6, 3)

我使用zipWithIndex.collect获得了“近乎工作的解决方案”,但是当我在其中进行比较时,index会运行OutOfBounds。如果我可以在里面使用两个条件,我可以使这个工作,首先检查最大索引是index =(list.size-1)然后我可以比较列表(索引)!= list(索引+ 1)然后产量列表(索引) )

我没有成功的尝试(因为OutOfBounds),是:

times.zipWithIndex.collect
{
    case (element, index)
        // index+1 will be incremented out of my list
        if (times(index) != times(index+1)) => times(index)
}

如果我可以再使用一个条件来限制索引,则可以使用,但不能用于两个条件:

times.zipWithIndex.collect
{
    case (element, index)
        if (index < times.size)
            if (times(index) != times(index+1)) => times(index)
}

我感谢任何其他选择。

3 个答案:

答案 0 :(得分:2)

怎么样

Function Iterate-Subnet($Subnet) {  #in the format of "10.10.10."
    $arrIPs = @();$arrValid = @() #Creates 2 arrays, one to hold the IP
                                  #addresses and the other to hold confirmed
                                  #Windows machines
    For($x=1; $x -lt 254; $x++) {
        #Starting at 1 and incrementing to 254, each time building a new IP
        #address based on the subnet
        $IPAddress = $Subnet + $x;$arrIPs += $IPAddress
    }
    ForEach ($IPfound in $arrIPs) {
        $ping = Get-WMIObject -Class Win32_PingStatus -Filter "Address='$IPfound'" #Ping each IP address in the subnet
        If ($ping.StatusCode -eq "0") {
             #Attempt to connect to each online machine using WMI, this
             #confirms whether it's a Windows machine
             $checkOS = Get-WMIObject -Class Win32_OperatingSystem -ComputerName "$IPfound" -ErrorAction SilentlyContinue
             #Add this computer name to the valid array
             If ($checkOS -ne $null) {$arrValid += @($checkOS.CSName)}
        }
    }
    #Remove any duplicate entries, this accounts for any multihomed machines
    $arrValid = $arrValid | Select-Object -Unique
    #Return the valid array to any script you choose to call this function from
    return $arrValid 
}

#Split the computer name at the device position number
#and store the beginning and end into two variables
$computer = hostname
$cname1 = $computer.substring(0,8)
$cname2 = $computer.substring($computer.length -5,5)

#Find and define the site subnet for pingsweep
#referencing current machine IP Address
$getip = Test-Connection -ComputerName $computer -Count 1 
$ip = $getip.IPV4Address.IPAddressToString
$IPByte = $ip.Split(".")
$sitesub = ($IPByte[0]+"."+$IPByte[1]+"."+$IPByte[2]+".")

#build and retrieve the list of site machines
#from the function Iterate-Subnet
$sitemachines = Iterate-Subnet -Subnet "$sitesub"
Start-Sleep -s 30
$sitemachines.length
$sitemachines

答案 1 :(得分:1)

你可以使用自己的zip列表,删除第一个元素,以便比较索引N处的元素和N + 1.你只需要追加最后一个元素(你可能想要使用ListBuffer作为附加最后一个元素需要复制列表。)

val r = times.zip(times.drop(1)).withFilter(t => t._1 != t._2).map(_._1) :+ times.last
scala> val times = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)
times: List[Int] = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)

scala> val r = times.zip(times.drop(1)).withFilter(t => t._1 != t._2).map(_._1) :+ times.last
r: List[Int] = List(1, 2, 3, 6, 3, 7, 5, 6, 3)

答案 2 :(得分:1)

这是我使用滑动功能的替代选择:

val ll = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)
ll.sliding(2)
    .filter( t => t.length > 1 && t(0) != t(1) )
    .map( t => t(0) )
    .toList :+ ll.last