我想要一个列表,然后我想测试列表以查看项目是否存在:
这是我的示例代码段:
String[] handToolArray = {"pliers", "screwdriver", "tape measure"};
List<String> handToolList = new ArrayList<String>( Arrays.asList(handToolArray));
if (handToolList.contains("pliers")){
System.out.println("I have pliers");
} else {
System.out.println("I don't have pliers");
}
Run Code Online (Sandbox Code Playgroud)
在第二行中,Arrays.asList(handToolArray)生成:
"Type safety: The expression of type List needs unchecked conversion to conform to Collection<? extends String>"
Run Code Online (Sandbox Code Playgroud)
问题:有没有更好的方法来创建然后查询列表,这是简洁的,不需要抑制未经检查的警告?
我是iPython的新手,并试图帮助其他开发人员开始,我们都遇到了同样的问题.
我们试图从https://docs.python.org/2/library/unittest.html#basic-example在iPython中运行python unittest示例 代码在windows和ubuntu上从命令行运行得很好而没有任何修改完全相同来自iPy笔记本的代码生成以下异常:
AttributeError: 'module' object has no attribute '/home/myuser/'
Run Code Online (Sandbox Code Playgroud)
文件名是: /home/myuser/example_unittest.ipynb
到目前为止,我已经将iPython文档和Google搞砸了,但没有运气.任何调试技巧或解决此问题的线索都值得赞赏.
(全栈):
AttributeError Traceback (most recent call last)
<ipython-input-2-39bc0ec16f11> in <module>()
28
29 if __name__ == '__main__':
---> 30 unittest.main()
31
/usr/lib/python2.7/unittest/main.pyc in __init__(self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer)
92 self.testLoader = testLoader
93 self.progName = os.path.basename(argv[0])
---> 94 self.parseArgs(argv)
95 self.runTests()
96
/usr/lib/python2.7/unittest/main.pyc in parseArgs(self, argv)
147 else:
148 self.testNames = (self.defaultTest,)
--> 149 self.createTests()
150 except getopt.error, msg: …Run Code Online (Sandbox Code Playgroud)