我有一个这样的列表:
array=['for','loop','in','python']
for arr in array:
print arr
Run Code Online (Sandbox Code Playgroud)
这将给我输出
for
lop
in
python
Run Code Online (Sandbox Code Playgroud)
我想要打印
in
python
Run Code Online (Sandbox Code Playgroud)
如何跳过python中的前2个索引?
我有一些python代码来比较两个图像:
detector_FeatureDetector_1 = cv2.FastFeatureDetector_create()
detector_FeatureDetector_2 = cv2.FastFeatureDetector_create()
detector_DescriptorExtractor_1 = cv2.BRISK_create()
detector_DescriptorExtractor_2 = cv2.BRISK_create()
detector_DescriptorMatcher_1 = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck = True)
detector_DescriptorMatcher_2 = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck = True)
image_1 = cv2.imread('/Users/rui/image1.png')
image_2 = cv2.imread('/Users/rui/image2.png')
obj_descriptor_keypoints_1 = detector_FeatureDetector.detect(image_1)
obj_descriptor_keypoints_2 = detector_FeatureDetector.detect(image_2)
keypoints1, obj_descriptor_descriptors_1 = detector_DescriptorExtractor.compute(image_1, obj_descriptor_keypoints_1)
keypoints2, obj_descriptor_descriptors_2 = detector_DescriptorExtractor.compute(image_2, obj_descriptor_keypoints_2)
matches = detector_DescriptorMatcher.knnMatch(obj_descriptor_descriptors_1, obj_descriptor_descriptors_2, k=6)
Run Code Online (Sandbox Code Playgroud)
但detector_DescriptorMatcher.knnMatch()只能当k=1.如果k值不同1,则返回以下错误:
OpenCV Error: Assertion failed (K == 1 && update == 0 && mask.empty()) in batchDistance, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-3.0.0/modules/core/src/stat.cpp, line 3682 …Run Code Online (Sandbox Code Playgroud) 在我的生产代码中,我在枚举的构造函数中构造一个EnumSet,但它无法检测到我的类是一个实际的枚举.这个错误打破了我的真实生产代码
为了测试目的,我编写了下面的类,但即使使用这个小例子,它仍然无法识别枚举.我如何将我的类标记为EnumSet知道它是枚举的方式的枚举?
简单的源代码:
package test;
import java.util.*; // Set, EnumSet, Arrays
public enum Alphabet {
A,
B(A),
C(A, B),
D(A, B, C),
// You get the point
;
Set<Alphabet> prevLetters;
Alphabet() {
prevLetters = EnumSet.noneOf(Alphabet.class); // <- EnumSet here
}
Alphabet(Alphabet... prev) {
this();
prevLetters.addAll(Arrays.asList(prev));
}
public static void main(String[] args) {
System.out.println(Arrays.toString(Alphabet.values()));
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ClassCastException: class test.Alphabet not an enum
at java.util.EnumSet.noneOf(EnumSet.java:112)
at test.Alphabet.<init>(Test.java:13)
at test.Alphabet.<clinit>(Test.java:4)
Run Code Online (Sandbox Code Playgroud) max获取可迭代参数并返回iterable的最大值.对于整数,这是明显的行为,因为它可以确定哪个数字最大.对于角色,它改为使用词典排序:
>>> max("hello world!")
'w'
>>> 'w' > 'r'
True
>>> max("Hello World!")
'r'
>>> 'W' > 'r'
False
Run Code Online (Sandbox Code Playgroud)
但是,Python对集合列表做了什么?目前还不清楚集合的排序是如何工作的,尽管我认为这与长度有关:
>>> set([1]) < set([5])
False
>>> set([1]) > set([5])
False
>>> set([1]) < set([1, 2])
True
>>> set(range(5)) < set(range(7)) < set(range(1000))
True
Run Code Online (Sandbox Code Playgroud)
但这不是产生max价值的因素:
>>> max([set([1]), set([5]), set([2, 4, 7]), set([100000])])
set([1])
>>> max([set([]), set([11]), set([5]), set([2, 4, 7]), set([100000])])
set([11])
>>> max([set(range(45)), set(range(12)), set(range(100, 1260, 40))])
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取一个数据集并将其划分为3个部分:训练:60%,测试:20%,验证:20%。
part1 <- createDataPartition(fullDataSet$classe, p=0.8, list=FALSE)
validation <- fullDataSet[-part1,]
workingSet <- fullDataSet[part1,]
Run Code Online (Sandbox Code Playgroud)
当我做同样的事情再次分区时:
inTrain <- createDataPartition(workingSet$classe, p=.75, list=FALSE)
Run Code Online (Sandbox Code Playgroud)
我得到错误:
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
Run Code Online (Sandbox Code Playgroud)
有没有一种方法要么a)创建3个不同大小的分区,要么b)像我尝试做的那样嵌套一个分区?我考虑过c)使用sample()代替,但这是针对讲师仅使用createDataPartition的类,并且我们必须显示代码。有人在这里有什么建议吗?
我正在寻找一个id列表并返回一个多次出现的id列表.这就是我设置的工作方式:
singles = list(ids)
duplicates = []
while len(singles) > 0:
elem = singles.pop()
if elem in singles:
duplicates.append(elem)
Run Code Online (Sandbox Code Playgroud)
但是id列表可能会变得很长,而且我实际上不希望在一个昂贵的len调用上使用while循环,如果我可以避免它.(我可以走不优雅的路线并且调用len一次,然后在每次迭代时减少它,但是如果可以的话我宁愿避免它).
我是使用Python进行信号处理的新手.我想了解如何将加速度计的幅度值转换为频域.我的示例代码如下:
在[44]中:
x = np.arange(30)
plt.plot(x, np.sin(x))
plt.xlabel('Number of Sample')
plt.ylabel('Magnitude Value')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在这里,我想将数据绘制到域频率.所需的输出可能是这样的:

我想按照他们的分数对名单进行排序.到目前为止我所拥有的是什么
file = open("scores.txt", 'r')
for line in file:
name = line.strip()
print(name)
file.close()
Run Code Online (Sandbox Code Playgroud)
我不确定如何对它们进行排序.
这是文件内容:
Matthew, 13
Luke, 6
John, 3
Bobba, 4
Run Code Online (Sandbox Code Playgroud)
我希望输出是什么:
John 3
Bobba 4
Luke 6
Matthew 13
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我见过人们经常建议使用isinstance()而不是type(),我发现这很奇怪,因为type()似乎对我来说更具可读性.
>>> isinstance("String",str)
True
>>> type("String") is str
True
Run Code Online (Sandbox Code Playgroud)
后者似乎更加pythonic和清晰,而前者稍微混淆,并暗示一个特定的用途(例如,测试用户构建的类而不是内置类型)但是还有一些我不知道的其他好处?type()会导致一些不稳定的行为或错过一些比较吗?
我正在创建一个列表理解,我从字典中获取键列表,忽略某些指定的键。
[x if x not in ignoreKeys else None for x in entity]
Run Code Online (Sandbox Code Playgroud)
我目前正在使用 else None 作为我不附加被忽略的键的方式,但理想情况下我会让列表理解通过该迭代。不幸的是 pass 给出了一个语法错误,所以我想知道是否有某种方法可以模拟 pass 功能?
python ×8
python-2.7 ×2
enums ×1
for-loop ×1
java ×1
matplotlib ×1
max ×1
opencv ×1
python-3.x ×1
r ×1
r-caret ×1
set ×1
sorting ×1
types ×1