我正在使用Google Collections库AbstractIterator来实现生成器.我这样做时遇到了一个问题; 我把它缩小为更基本的类型并重现了这个问题.这种减少对于它的作用显然有点过分,通过Iterable从1计算到数字.
基本上在下面的代码中,未注释的版本起作用,而注释的版本不起作用(最后提供一个null元素,而不是以最后一个数字结尾).
我做错了什么,或者这是图书馆的问题?
private Iterable<Integer> elementGenerator(final int numelements) {
return new Iterable<Integer>() {
@Override public Iterator<Integer> iterator() {
return new AbstractIterator<Integer>(){
int localcount=0;
@Override protected Integer computeNext() {
if (localcount++ == numelements) return endOfData();
return localcount;
// return (localcount++ == numelements) ? endOfData() : localcount;
}
};
}
};
}
Run Code Online (Sandbox Code Playgroud)
我也尝试摆弄这个?:安排(例如,在返回前加上前缀并比较+1),但无济于事.我捅了一下寻找关于这个的文档,但没有找到任何东西.显然,?:语法只是方便,不是必需品,但仍然......
我正在尝试运行for循环.这是我的代码部分,我遇到了麻烦:
aldurstengd_ororka = {(18, 19, 20, 21, 22, 23, 24):1, (25):0.95, (26):0.90,
(27):0.85, (28, 29):0.75, (30, 31):0.65, (32, 33):0.55, (34, 35):0.45,
(36, 37):0.35, (40, 41, 42, 43, 44, 45):0.15, (46, 47, 48, 49, 50):0.10,
(51, 52, 53, 54, 55):0.075, (56, 57, 58, 59, 60):0.05, (61, 62, 63, 64,
65, 66):0.025}
for age in aldurstengd_ororka.keys():
for item in age:
if ororkualdur == item:
baetur = baetur + ororkulifeyrir * aldurstengd_ororka([age])
Run Code Online (Sandbox Code Playgroud)
所以我的意图是运行aldurstengd_ororka,对于字典中的每个"age"元组,我为元组内的每个"item"运行另一个for循环.我得到的错误是
TypeError:'int'对象不可迭代
考虑这个例子?
p = [1,2,3,4], (1,2,3), set([1,2,3])]
Run Code Online (Sandbox Code Playgroud)
而不是检查每种类型
for x in p:
if isinstance(x, list):
xxxxx
elif isinstance(x, tuple):
xxxxxx
elif isinstance(x, set):
xxxxxxx
Run Code Online (Sandbox Code Playgroud)
以下是否有一些等价物:
for element in something:
if isinstance(x, iterable):
do something
Run Code Online (Sandbox Code Playgroud) 我试图使用islice函数但收到以下错误:
Traceback (most recent call last):
File "/Users/gavinnachbar/Session 3 copy/all_in_one_almost.py", line 168, in <module>
all_decisions.extend(islice(reader,4))
NameError: name 'islice' is not defined
Run Code Online (Sandbox Code Playgroud)
我已经导入了itertools,但我仍然得到错误,任何想法为什么?
我有一套和一个方法:
private static Set<String> set = ...;
public static String method(){
final String returnVal[] = new String[1];
set.forEach((String str) -> {
returnVal[0] += str;
//if something: goto mark
});
//mark
return returnVal[0];
}
Run Code Online (Sandbox Code Playgroud)
我可以在lambda中终止forEach(使用或不使用例外)吗?我应该使用匿名课吗?
我能做到这一点:
set.forEach((String str) -> {
if(someConditions()){
returnVal[0] += str;
}
});
Run Code Online (Sandbox Code Playgroud)
但是浪费时间.
使用stream.reduce实现
return set.parallelStream().reduce((output, next) -> {
return someConditions() ? next : output;
}).get(); //should avoid empty set before
Run Code Online (Sandbox Code Playgroud)
我正在寻找最快的解决方案,因此如果它们足够快,那么每个循环的异常和"真实"是可以接受的.
我读了这个问题
但是当使用下面的代码时,我会得到一个假的np.array,如下所示.
import collections
isinstance(np.arange(10), collections.Sequence)
# returns false
Run Code Online (Sandbox Code Playgroud)
我觉得有点烦人,我不能做len(1),只是得到1.
我能想到的唯一工作是try except如下声明:
a = 1
try:
print len(a)
except TypeError:
print 1
Run Code Online (Sandbox Code Playgroud)
是否有更多的Pythonic方法来做到这一点?
迭代迭代器时我想避开最终项目并停在倒数第二项 - 我该怎么做?
from itertools import product
from collections import namedtuple
param_list = []
Parameter = namedtuple("Parameter", ['bad', 'good'])
param1 = Parameter(["peanut", "gluten"], ["bacon", "pickle"])
param_list.append(param1)
param2 = Parameter([0], [1, 22])
param_list.append(param2)
param3 = Parameter([0, 1], [2, 3, 4, 9])
param_list.append(param3)
param4 = Parameter(["football"], ["running", "yoga"])
param_list.append(param4)
for prod in product(*param_list): # -- I want to skip the last product --
for sub_prod in product(*prod):
prod = [str(x) if type(x) is not str else x for x in sub_prod]
print …Run Code Online (Sandbox Code Playgroud) 我正在阅读Bill Lubanovic的“ Python简介”,它说
像zip()一样,range()返回一个可迭代的对象,
但这不是不正确的说法吗?例如,
s= zip([1,2,3],['one','two','three'])
next(s)
>> (1,'one')
next(s)
>> (2,'two')
a = range(10)
next(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-54-7b3ad3809256> in <module>()
1 a = range(10)
----> 2 next(a)
TypeError: 'range' object is not an iterator
Run Code Online (Sandbox Code Playgroud)
从上面的代码中,我们可以检查zip()返回的是迭代器而不是可迭代的对象。
我正在运行Python3.6,并且正在使用其中包含其他列表的列表。
list_array = [[1,0,1,0,2,2],
[1,1,2,0,1,2],
[2,2,2,1,0,1]]
Run Code Online (Sandbox Code Playgroud)
我想修改名为list_array的列表,以删除子列表中所有值为2的条目。
我用于此的代码是
for k in list_array:
k = [x for x in k if x!=2]
Run Code Online (Sandbox Code Playgroud)
但是,此代码不会修改list_array。
为什么无法以这种方式替换list_array中列表中的元素?
我正在从https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators学习有关Iterable的内容,并且它清楚地表明Array是Iterable的。在chrome控制台中对其进行检查,我们可以看到它具有Symbol.iterator,并且可以for..of在其上运行。
但是数组似乎并没有遵循next()我拥有的一种方法的迭代器模式,因为您无法重置迭代器,这将严重限制数组。
但是我很难从文档中确定哪些Iterables具有完整的实现(next()),哪些没有完整的实现,或者我在这里错过了一个基本概念。
我想念什么?
iterable ×10
python ×7
iterator ×4
java ×2
arrays ×1
guava ×1
import ×1
int ×1
isinstance ×1
iteration ×1
java-8 ×1
javascript ×1
lambda ×1
list ×1
loops ×1
module ×1
object ×1
python-3.x ×1
typechecking ×1