我正在寻找一种迭代迭代迭代的第一n项的pythonic方法(upd:在常见情况下不是列表,因为列表事情是微不足道的),并且尽可能快地执行此操作非常重要.这是我现在这样做的方式:
count = 0
for item in iterable:
do_something(item)
count += 1
if count >= n: break
Run Code Online (Sandbox Code Playgroud)
对我来说似乎并不整洁.另一种方法是:
for item in itertools.islice(iterable, n):
do_something(item)
Run Code Online (Sandbox Code Playgroud)
这看起来不错,问题是它是否足够快与一些发电机一起使用?例如:
pair_generator = lambda iterable: itertools.izip(*[iter(iterable)]*2)
for item in itertools.islice(pair_generator(iterable), n):
so_something(item)
Run Code Online (Sandbox Code Playgroud)
与第一种方法相比,它运行得足够快吗?有没有更简单的方法呢?
我对一些关于定义我自己的迭代器的概念感到困惑:
从这里:http://www.cs.northwestern.edu/~riesbeck/programming/c++/stl-iterator-define.html,这似乎建议使用定义运算符的内部迭代器类.许多其他人继承基类iterator来重新定义运算符.
我对应该使用哪种方法感到很困惑.为什么会这样
typedef ptrdiff_t difference_type;
Run Code Online (Sandbox Code Playgroud)
例如,在容器类的定义的开头?
非常感谢你!
以下代码:
a = list(range(10))
remove = False
for b in a:
if remove:
a.remove(b)
remove = not remove
print(a)
Run Code Online (Sandbox Code Playgroud)
输出[0, 2, 3, 5, 6, 8, 9],而不是[0, 2, 4, 6, 8]使用Python 3.2时.
请注意,我不是要解决这个行为,而是要了解它.
a并且b是Iterator[String]类型的值.我需要c包括的所有元素a和b.令人惊讶的是我无法弄清楚如何实现这一目标.你碰巧知道吗?
有没有聪明的方法可以在多个列表上编写列表理解?
我知道我可以使用单独的范围列表作为索引,但这样我必须知道长度(或通过len()函数调用单独获取它).
>>> a = range(10)
>>> b = range(10, 0, -1)
>>> [(a[x],b[x]) for x in range(10)]
[(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]
Run Code Online (Sandbox Code Playgroud)
我喜欢这样的东西:
>>> [(a,b) for a in range(10) and b in range(10, 0, -1)]
[(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]
Run Code Online (Sandbox Code Playgroud)
你会怎么写列表理解?有没有办法用itertools做到这一点?
范围列表只代表任何列表,我不一定想得到元组.还可以有一个以a和b为参数的函数.拉链不是我想要的.
更新:"所以拉链不是我想要的." 我的意思是我不想要zip(range(10), range(10, 0, …
我需要一个针对以下情况的建议 - 我几个小时都无法弄明白:如何通过多个seq.容器大小相同(这里:两个向量)的简单方法?
int main() {
int size = 3;
std::vector<int> v1{ 1, 2, 3 }, v2{ 6, 4, 2 };
// old-fashioned - ok
for (int i = 0; i < size; i++) {
std::cout << v1[i] << " " << v2[i] << std::endl;
}
// would like to do the same as above with auto range-for loop
// something like this - which would be fine for ONE vector.
// But this does not work. Do I need …Run Code Online (Sandbox Code Playgroud) 我有一个终身问题,我正在尝试实现一个迭代器,通过引用返回它的项目,这里是代码:
struct Foo {
d: [u8; 42],
pos: usize
}
impl<'a> Iterator<&'a u8> for Foo {
fn next<'a>(&'a mut self) -> Option<&'a u8> {
let r = self.d.get(self.pos);
if r.is_some() {
self.pos += 1;
}
r
}
}
fn main() {
let mut x = Foo {
d: [1; 42],
pos: 0
};
for i in x {
println!("{}", i);
}
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码编译不正确,我得到一个与参数生命周期有关的问题,这里是相应的错误:
$ rustc test.rs
test.rs:8:5: 14:6 error: method `next` has an incompatible type for trait: expected …Run Code Online (Sandbox Code Playgroud) 我需要迭代一个Vec但我需要每个迭代元素的位置.我确定这已经在API中,但我看不到它.
我需要这样的东西:
fn main() {
let v = vec![1; 10];
for (pos, e) in v.iter() {
// do something here
}
}
Run Code Online (Sandbox Code Playgroud) 我在使用矢量迭代器时遇到了麻烦.我在一些地方读过,检查null迭代器是不可能的,检查迭代器的常用方法是在搜索后检查vector.end().例如:
vector< Animal* > animalList;
vector<Animal*>::iterator findInList(const type_info& type)
{
// Loop through list of Animals, if Dog found, return iterator to it
}
auto it = findInList(typeid(Dog));
// With a pointer I can check if it's null, but with an iterator I have to check against animalList.end();
Run Code Online (Sandbox Code Playgroud)
问题是容器可能是空的.使用迭代器,我不能返回null以指示容器为空或搜索失败.我可以返回vector :: end(),但cplusplus.com说:
If the container is empty, vector::end() function returns the same as vector::begin()
Run Code Online (Sandbox Code Playgroud)
然后对于vector :: begin()它说:
If the container is empty, the returned iterator value shall not be dereferenced.
Run Code Online (Sandbox Code Playgroud)
所以,如果我有一个空容器,vector …
并且它们都获得Consumer作为参数.因此,如果Java 8是为了避免混淆,就像它在Time API中所做的那样,为什么它会增加一个新的混乱?还是我错过了一些观点?