如果我理解正确,在Python 2中,iter(d.keys())就像是一样d.iterkeys().但现在,d.keys()是一个视图,它位于列表和迭代器之间.视图和迭代器之间有什么区别?
换句话说,在Python 3中,有什么区别
for k in d.keys()
f(k)
Run Code Online (Sandbox Code Playgroud)
和
for k in iter(d.keys())
f(k)
Run Code Online (Sandbox Code Playgroud)
另外,这些差异如何在一个简单的for循环中显示(如果有的话)?
我经常看到如下代码:
Iterator i = list.iterator();
while(i.hasNext()) {
...
}
Run Code Online (Sandbox Code Playgroud)
但我写的是(当Java 1.5不可用或者每个都不能使用时):
for(Iterator i = list.iterator(); i.hasNext(); ) {
...
}
Run Code Online (Sandbox Code Playgroud)
因为
i在较小的范围内i外面使用?i声明在哪里?)我认为代码应该尽可能简单易懂,这样我才能制作复杂的代码来完成复杂的事情.你怎么看?哪个更好?
我用这样的原型编写了一些函数:
template <typename input_iterator>
int parse_integer(input_iterator &begin, input_iterator end);
Run Code Online (Sandbox Code Playgroud)
这个想法是调用者将提供一系列字符,并且该函数会将字符解释为整数值并将其返回,从最后使用的字符开始.例如:
std::string sample_text("123 foo bar");
std::string::const_iterator p(sample_text.begin());
std::string::const_iterator end(sample_text.end());
int i = parse_integer(p, end);
Run Code Online (Sandbox Code Playgroud)
这将begin设置为123并i"指向"之前的空间p.
我被告知(没有解释)通过引用传递迭代器是不好的形式.这是不好的形式?如果是这样,为什么?
就时空复杂度而言,以下哪一项是迭代std :: vector的最佳方法,为什么?
方法1:
for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
/* std::cout << *it; ... */
}
Run Code Online (Sandbox Code Playgroud)
方式2:
for(std::vector<int>::size_type i = 0; i != v.size(); i++) {
/* std::cout << v[i]; ... */
}
Run Code Online (Sandbox Code Playgroud)
方式三:
for(size_t i = 0; i != v.size(); i++) {
/* std::cout << v[i]; ... */
}
Run Code Online (Sandbox Code Playgroud)
方式4:
for(auto const& value: a) {
/* std::cout << value; ... */
Run Code Online (Sandbox Code Playgroud) 我一直在玩 JS,但无法弄清楚 JS 如何决定在使用Array.from(). 例如,以下表情符号的 alength为 2,因为它由两个代码点组成,但是,Array.from()将这两个代码点视为一个,给出一个包含一个元素的数组:
const emoji = '';
console.log(Array.from(emoji)); // Output: [""]Run Code Online (Sandbox Code Playgroud)
但是,其他一些字符也有两个代码点,例如这个字符??(也有一个.length2)。但是,Array.from不会“分组”此字符,而是生成两个元素:
const str = '??';
console.log(Array.from(str)); // Output: ["?", "?"]Run Code Online (Sandbox Code Playgroud)
我的问题是:当字符由两个代码点组成时,是什么决定了字符是被分解(如示例二)还是被视为一个单一元素(如示例一)?
码:
c = 0
items.each { |i|
puts i.to_s
# if c > 9 escape the each iteration early - and do not repeat
c++
}
Run Code Online (Sandbox Code Playgroud)
我想抓住前10个项目,然后离开"每个"循环.
我该如何用?替换注释行?有更好的方法吗?更多Ruby惯用的东西?
是否有任何现有的迭代器实现(可能在boost中)实现某种展平迭代器?
例如:
unordered_set<vector<int> > s;
s.insert(vector<int>());
s.insert({1,2,3,4,5});
s.insert({6,7,8});
s.insert({9,10,11,12});
flattening_iterator<unordered_set<vector<int> >::iterator> it( ... ), end( ... );
for(; it != end; ++it)
{
cout << *it << endl;
}
//would print the numbers 1 through 12
Run Code Online (Sandbox Code Playgroud) 我在工作中学习Perl并享受它.我通常用Python做我的工作,但老板想要Perl.
Python和Perl中的大多数概念都很匹配:Python dictionary = Perl hash; Python元组= Perl列表; Python list = Perl数组; 等等
问题:是否有迭代器 /生成器的Python形式的Perl版本?
示例:生成Fibonacci数字的经典Python方法是:
#!/usr/bin/python
def fibonacci(mag):
a, b = 0, 1
while a<=10**mag:
yield a
a, b = b, a+b
for number in fibonacci(15):
print "%17d" % number
Run Code Online (Sandbox Code Playgroud)
如果要根据需要生成更大列表的子部分,迭代器也很有用.Perl'列表'似乎更加静态 - 更像是Python元组.在Perl中,可以foreach是动态的还是仅基于静态列表?
Iterator的Python形式是我已经习惯的形式,我没有在Perl中找到它...除了在循环或递归中编写它或生成一个巨大的静态列表,我如何(为ex)写它在Perl中的Fibonacci子程序?是否有yield我错过的Perl ?
具体来说 - 我该怎么写:
#!/usr/bin/perl
use warnings; use strict; # yes -- i use those!
sub fibonacci {
# What goes here other than returning an array or …Run Code Online (Sandbox Code Playgroud) 我想知道为什么这个Collection.addAll()方法只接受其他Collections但不接受Iterables.这是为什么?
对Iterables 做任何类似的方法?
请考虑以下头文件:
template <typename T> struct tNode
{
T Data; //the data contained within this node
list<tNode<T>*> SubNodes; //a list of tNodes pointers under this tNode
tNode(const T& theData)
//PRE: theData is initialized
//POST: this->data == theData and this->SubNodes have an initial capacity
// equal to INIT_CAPACITY, it is set to the head of SubNodes
{
this->Data = theData;
SubNodes(INIT_CAPACITY); //INIT_CAPACITY is 10
}
};
Run Code Online (Sandbox Code Playgroud)
现在考虑来自另一个文件的一行代码:
list<tNode<T>*>::iterator it(); //iterate through the SubNodes
Run Code Online (Sandbox Code Playgroud)
编译器给我这个错误消息: Tree.h:38:17: error: need ‘typename’ before ‘std::list<tNode<T>*>::iterator’ because ‘std::list<tNode<T>*>’ …
iterator ×10
c++ ×4
java ×2
python ×2
c++11 ×1
dictionary ×1
enumerator ×1
for-loop ×1
iterable ×1
javascript ×1
loops ×1
performance ×1
perl ×1
python-3.x ×1
ruby ×1
std ×1
stl ×1
string ×1
templates ×1
typename ×1
unicode ×1