scala> data.take(5).foreach(println)
(Diamond Fear No Evil Compound Bow Package,CompactBuffer((45,Diamond Fear No Evil Compound Bow Package,599.99)))
(Fitness Gear Pro Half Rack,CompactBuffer((11,Fitness Gear Pro Half Rack,349.99), (16,Fitness Gear Pro Half Rack,349.99)))
(Nike Men's Air Max 2014 Running Shoe,CompactBuffer((19,Nike Men's Air Max 2014 Running Shoe,149.99), (49,Nike Men's Air Max 2014 Running Shoe,149.99)))
(Nike Men's Andrew Luck Jersey - Home Game Ind,CompactBuffer((59,Nike Men's Andrew Luck Jersey - Home Game Ind,100.0)))
(Diamondback Grind BMX Bike 2014,CompactBuffer((10,Diamondback Grind BMX Bike 2014,199.99), (43,Diamondback Grind BMX Bike 2014,199.99)))
如果运行查询
scala> data.map(x => x._2._1).take(5).foreach(println)
<console>:34: error: value _1 is not a member of Iterable[(Int, String, String)]
data.map(x => x._2._1).take(5).foreach(println)
答案 0 :(得分:2)
让我们分析您对数据data.map(x => x._2._1).take(5).foreach(println)
(Diamond Fear No Evil Compound Bow Package,CompactBuffer((45,Diamond Fear No Evil Compound Bow Package,599.99)))
x._2
CompactBuffer((45,Diamond Fear No Evil Compound Bow Package,599.99))
不是tuple
,因此您无法使用compactBuffer
符号访问underscore
元素作为元组的元素_1
那就是错误意味着什么
CompactBuffer
是iterator
,其中包含Tuple3
,因此您可以使用underscore
符号对其进行迭代并访问元素,如下所示。
data.map(x => x._2.map(y => y._1)).take(5).foreach(println)
将为您提供结果
List(45)
List(11, 16)
......
而不是
data.map(x => x._2.map(y => y._3)).take(5).foreach(println)
会给你
List(599.99)
List(349.99, 349.99)
..........
等等。