我试图将几个列表合并为一个,消除重复.Guava中的mergeSorted方法似乎适用于我的情况.但是当我尝试它时,我看到有关我传递给方法的参数的编译错误.我的代码就像这样简单,我有两个列表,将它们连接成一个然后尝试mergeSort它,但我在第四行得到一个编译错误.
final List<Integer> first = Lists.newArrayList(1, 2, 3);
final List<Integer> second = Lists.newArrayList(4, 2, 5, 6);
Iterable<Integer> some = Iterables.concat(first, second);
final Iterable all = Iterables.<Integer>mergeSorted(some, comp);
System.out.println(all);
Run Code Online (Sandbox Code Playgroud)
看起来它是mergeSorted期待Iterable <?延伸Iterable <?扩展T >> iterables但方法描述似乎表明输入可以是所有给定iterables的合并内容
@Beta public static <T> Iterable <T> mergeSorted(Iterable <?extends Iterable <?extends T >> iterables,Comparator <?super T> comparator)
返回所有给定iterables的合并内容的可迭代.等效条目不会被重复数据删除.
调用者必须确保源迭代按非降序排列,因为此方法不对其输入进行排序.
所以我正在尝试使用treeMap创建数据集合,然后我想在使用自己的比较器时得到一个排序列表.
我的问题是Collections.sort抛出错误,因为Can only iterate over an array or an instance of java.lang.Iterable我的extsListed是ArrayList类型,它确实是Iterable,可以在这里看到http://docs.oracle.com/javase/8/docs/api/java/util/List html的
List<FileInfo> extsListed = new ArrayList<FileInfo>(exts.values());
for (FileInfo info : Collections.sort(extsListed)) {
System.out.println(info.key + ' ' + info.count + ' ' + info.size);
}
System.out.println(totalFound.toString() + ' ' + totalSize);
}
public static class FileInfo implements Comparable<FileInfo>{
String key;
Integer count;
Long size;
public FileInfo(String key, File file){
count = 1;
this.key = key;
this.size = file.length();
}
public int compareTo(FileInfo other) {
if (this.size …Run Code Online (Sandbox Code Playgroud) 我一直试图获得列表的总和,使用函数将其值从字符串更改为int
player_hand = []
def card_type(player_hand):
card_value = 0
if player_hand[0] == 'A':
card_value = 11
if player_hand[0] == 'J':
card_value = 10
if player_hand[0] == 'Q':
card_value = 10
if player_hand[0] == 'K':
card_value = 10
if player_hand[0] == '2':
card_value = 2
if player_hand[0] == '3':
card_value = 3
if player_hand[0] == '4':
card_value = 4
if player_hand[0] == '5':
card_value = 5
if player_hand[0] == '6':
card_value = 6
if player_hand[0] == '7':
card_value = 7
if …Run Code Online (Sandbox Code Playgroud) 我正在构建我自己的集合类,Iterable用Java 实现接口.但是,我收到编译错误说:
Nikolass-MacBook-Pro:week2 nburk$ javac Deque.java
Deque.java:115: error: incompatible types: Item#1 cannot be converted to Item#2
Item currentItem = current.item;
^
where Item#1,Item#2 are type-variables:
Item#1 extends Object declared in class Deque
Item#2 extends Object declared in class Deque.DequeIterator
1 error
Run Code Online (Sandbox Code Playgroud)
以下是我的代码的相关部分:
public class Deque<Item> implements Iterable<Item> {
// return an iterator over items in order from front to end
public Iterator<Item> iterator() {
return new DequeIterator<Item>();
}
private class DequeIterator<Item> implements Iterator<Item> {
public DequeIterator() …Run Code Online (Sandbox Code Playgroud) 在我的功能中,我有:
"""
Iterates 300 times as attempts, each having an inner-loop
to calculate the z of a neighboring point and returns the optimal
"""
pointList = []
max_p = None
for attempts in range(300):
neighborList = ( (x - d, y), (x + d, y), (x, y - d), (x, y + d) )
for neighbor in neighborList:
z = evaluate( neighbor[0], neighbor[1] )
point = None
point = Point3D( neighbor[0], neighbor[1], z)
pointList += point
max_p = maxPoint( pointList …Run Code Online (Sandbox Code Playgroud) 我有一个RDD[(String, (Iterable[Int], Iterable[Coordinate]))]
我想做的,是打破Iterable[Int]元组,每个人都会喜欢(String,Int,Iterable[Coordinate])
举个例子,我想改造:
('a',<1,2,3>,<(45.34,32.33),(45.36,32.34)>)
('b',<1>,<(46.64,32.66),(46.67,32.71)>)
Run Code Online (Sandbox Code Playgroud)
至
('a',1,<(45.34,32.33),(45.36,32.34)>)
('a',2,<(45.34,32.33),(45.36,32.34)>)
('a',3,<(45.34,32.33),(45.36,32.34)>)
('b',1,<(46.64,32.66),(46.67,32.71)>)
Run Code Online (Sandbox Code Playgroud)
Scala是怎么做的?
我有一个fib下面给出的课程.它实现__iter__和__next__.它是一个可迭代的,也是它自己的迭代器.
class fib(object):
def __init__(self):
self.prev = 0
self.curr = 1
def __iter__(self):
return self
def __next__(self):
value = self.curr
self.curr += self.prev
self.prev = value
return value
from collections import Iterable
print(isinstance(fib, Iterable))
Run Code Online (Sandbox Code Playgroud)
print语句返回False,我希望它返回True
我一直在尝试使用普通函数制作迭代器,没有生成器或将Symbol.iterator协议用于学术目的。为此,我创建了一个函数,该函数返回一个带有next参数的对象,但是尝试将它作为循环的iterable参数运行会for...of产生不需要的结果。
到目前为止,这是我从MDN 上的迭代器和生成器页面复制的代码:
function iterateThis(arr){
let i = 0;
return {
next: function() {
return i < arr.length ?
{value: arr[i++], done: false} :
{done: true};
}
};
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试像这样运行它:
const iterable = iterateThis([1,2,3,4,5]);
for(item in iterable){
console.log(item);
}
Run Code Online (Sandbox Code Playgroud)
在控制台上,我只得到一个结果:next.
我在创建函数时做错了iterateThis什么吗?还是for...of仅设计用于与发电机和Symbol.iterator财产一起工作?
在 Node v8.11.1 上执行
在Python 3中,通过定义__iter__和__next__方法,使类成为可迭代和迭代器是标准过程.但我有问题要绕过这个问题.以此示例创建一个仅生成偶数的迭代器:
class EvenNumbers:
def __init__(self, max_):
self.max_ = max_
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max:
result = 2 * self.n
self.n += 1
return result
raise StopIteration
instance = EvenNumbers(4)
for entry in instance:
print(entry)
Run Code Online (Sandbox Code Playgroud)
据我所知(如果我错了,请纠正我),当我创建循环时,通过调用itr = iter(instance)内部调用__iter__方法的内容来创建迭代器.这应该返回一个迭代器对象(实例是由于定义__next__,因此我可以返回self).要从中获取元素,next(itr)直到引发异常时才会调用它.
在这里我想问的是现在:是否以及如何能__iter__和__next__分离,从而使后者的功能的内容被定义别的地方?什么时候这可能有用?我知道我必须改变__iter__它以便它返回一个迭代器.
顺便说一句,这个想法来自这个网站(LINK),它没有说明如何实现这个.
我正在尝试为Web资源(延迟获取的图像)实现可迭代的代理。
首先,我做到了(返回id,在生产中,这些将是图像缓冲区)
def iter(ids=[1,2,3]):
for id in ids:
yield id
Run Code Online (Sandbox Code Playgroud)
效果很好,但现在我需要保持状态。
我阅读了定义迭代器的四种方法。我判断迭代器协议是要走的路。跟随我的尝试和失败,以实现这一目标。
class Test:
def __init__(me, ids):
me.ids = ids
def __iter__(me):
return me
def __next__(me):
for id in me.ids:
yield id
raise StopIteration
test = Test([1,2,3])
for t in test:
print('new value', t)
Run Code Online (Sandbox Code Playgroud)
输出:
new value <generator object Test.__next__ at 0x7f9c46ed1750>
new value <generator object Test.__next__ at 0x7f9c46ed1660>
new value <generator object Test.__next__ at 0x7f9c46ed1750>
new value <generator object Test.__next__ at 0x7f9c46ed1660>
new value <generator object Test.__next__ …Run Code Online (Sandbox Code Playgroud) iterable ×10
python ×5
iterator ×4
java ×3
python-3.x ×3
class ×2
collections ×2
apache-spark ×1
generics ×1
guava ×1
int ×1
javascript ×1
mergesort ×1
rdd ×1
scala ×1