如何将数组转换为Java中的列表?
我使用Arrays.asList()但行为(和签名)以某种方式从Java SE 1.4.2(现在存档中的文档)更改为8,我在网上找到的大多数片段使用1.4.2行为.
例如:
int[] spam = new int[] { 1, 2, 3 };
Arrays.asList(spam)
Run Code Online (Sandbox Code Playgroud)
在许多情况下,它应该很容易被发现,但有时它可能会被忽视:
Assert.assertTrue(Arrays.asList(spam).indexOf(4) == -1);
Run Code Online (Sandbox Code Playgroud) 如何在Python中执行以下操作?
array = [0, 10, 20, 40]
for (i = array.length() - 1; i >= 0; i--)
Run Code Online (Sandbox Code Playgroud)
我需要有一个数组的元素,但从最后到开头.
a = [1, 2, 3, 4]
b = a.index(6)
del a[b]
print a
Run Code Online (Sandbox Code Playgroud)
以上显示以下错误:
Traceback (most recent call last):
File "D:\zjm_code\a.py", line 6, in <module>
b = a.index(6)
ValueError: list.index(x): x not in list
Run Code Online (Sandbox Code Playgroud)
所以我必须这样做:
a = [1, 2, 3, 4]
try:
b = a.index(6)
del a[b]
except:
pass
print a
Run Code Online (Sandbox Code Playgroud)
但有没有更简单的方法来做到这一点?
几乎我需要编写一个程序来检查列表是否有任何重复项,如果有,它会删除它们并返回一个新列表,其中包含未复制/删除的项目.这就是我所拥有的,但说实话,我不知道该怎么做.
def remove_duplicates():
t = ['a', 'b', 'c', 'd']
t2 = ['a', 'c', 'd']
for t in t2:
t.append(t.remove())
return t
Run Code Online (Sandbox Code Playgroud) >>> a=[1,2,3]
>>> a.remove(2)
>>> a
[1, 3]
>>> a=[1,2,3]
>>> del a[1]
>>> a
[1, 3]
>>> a= [1,2,3]
>>> a.pop(1)
2
>>> a
[1, 3]
>>>
Run Code Online (Sandbox Code Playgroud)
以上三种方法从列表中删除元素有什么区别吗?
我碰巧发现自己有一个基本的过滤需求:我有一个列表,我必须通过项目的属性过滤它.
我的代码看起来像这样:
my_list = [x for x in my_list if x.attribute == value]
Run Code Online (Sandbox Code Playgroud)
但后来我想,这样写它会不会更好?
my_list = filter(lambda x: x.attribute == value, my_list)
Run Code Online (Sandbox Code Playgroud)
它更具可读性,如果需要性能,可以取出lambda来获得一些东西.
问题是:使用第二种方式有什么警告吗?任何性能差异?我是否完全错过了Pythonic Way™并且应该以另一种方式(例如使用itemgetter而不是lambda)来完成它?
我在Python中有两个迭代,我想成对地遍历它们:
foo = (1, 2, 3)
bar = (4, 5, 6)
for (f, b) in some_iterator(foo, bar):
print "f: ", f, "; b: ", b
Run Code Online (Sandbox Code Playgroud)
它应该导致:
f: 1; b: 4
f: 2; b: 5
f: 3; b: 6
Run Code Online (Sandbox Code Playgroud)
一种方法是迭代索引:
for i in xrange(len(foo)):
print "f: ", foo[i], "; b: ", b[i]
Run Code Online (Sandbox Code Playgroud)
但这对我来说似乎有点不合时宜.有没有更好的方法呢?
我正在寻找最快的方法来了解列表中是否存在值(列表中包含数百万个值)以及它的索引是什么?我知道列表中的所有值都是唯一的,就像我的例子一样.
我尝试的第一种方法是(在我的实际代码中为3.8秒):
a = [4,2,3,1,5,6]
if a.count(7) == 1:
b=a.index(7)
"Do something with variable b"
Run Code Online (Sandbox Code Playgroud)
我尝试的第二种方法是(快2倍:我的实际代码为1.9秒):
a = [4,2,3,1,5,6]
try:
b=a.index(7)
except ValueError:
"Do nothing"
else:
"Do something with variable b"
Run Code Online (Sandbox Code Playgroud)
Stackoverflow用户提出的方法(我的实际代码为2.74秒):
a = [4,2,3,1,5,6]
if 7 in a:
a.index(7)
Run Code Online (Sandbox Code Playgroud)
在我的实际代码中,第一种方法需要3.81秒,第二种方法需要1.88秒.这是一个很好的改进但是:
我是Python /脚本的初学者,我想知道是否有最快的方法可以做同样的事情并节省更多的处理时间?
我的应用程序更具体的解释:
在blender的API中,a可以访问粒子列表:
particles = [1, 2, 3, 4, etc.]
Run Code Online (Sandbox Code Playgroud)
从那里,我可以访问它的位置:
particles[x].location = [x,y,z]
Run Code Online (Sandbox Code Playgroud)
我通过搜索每个粒子的位置测试每个粒子是否存在邻居,如:
if [x+1,y,z] in particles.location
"Find the identity of this neighbour particle in x:the particle's index
in the array"
particles.index([x+1,y,z])
Run Code Online (Sandbox Code Playgroud) 我在Python中有两个列表,如下所示:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Run Code Online (Sandbox Code Playgroud)
我需要创建第三个列表,其中包含第一个列表中不存在于第二个列表中的项目.从我必须得到的例子:
temp3 = ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)
有没有循环和检查的快速方法?
我有一个Python对象列表,我想根据对象本身的属性进行排序.该列表如下:
>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>,
<Tag: aes>, <Tag: ajax> ...]
Run Code Online (Sandbox Code Playgroud)
每个对象都有一个计数:
>>> ut[1].count
1L
Run Code Online (Sandbox Code Playgroud)
我需要按递减计数的数量对列表进行排序.
我已经看过几种方法,但我正在寻找Python的最佳实践.