我有一个字典如下:
someDict = {'a':[], 'b':[]}
Run Code Online (Sandbox Code Playgroud)
我想确定这个字典是否有任何非空列表的值.如果是这样,我想返回True.如果没有,我想返回False.有什么办法让这个单线眼镜?
我被告知要
写一个函数square(a),它接受一个数组a,数字并返回一个包含每个平方值的数组.
起初,我有
def square(a):
for i in a: print i**2
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为我正在打印,而不是像我被问到的那样回来.所以我试过了
def square(a):
for i in a: return i**2
Run Code Online (Sandbox Code Playgroud)
但这只是我阵列的最后一个数字.我怎样才能让它在整个列表中找到方位?
我想读一个包含整数列表列表的大文本文件.现在我正在做以下事情:
G = []
with open("test.txt", 'r') as f:
for line in f:
G.append(list(map(int,line.split())))
Run Code Online (Sandbox Code Playgroud)
但是,它需要大约17秒(通过时间).有没有办法减少这个时间?也许,有一种方法不使用地图.
参考这个Python列表理解与.地图问题,有人可以解释为什么List Comprehensions map在列表理解不调用函数时会给出更好的结果,即使在没有lambda函数map但是在调用函数时给出最差的结果时?
import timeit
print timeit.Timer('''[i**2 for i in xrange(100)]''').timeit(number = 100000)
print timeit.Timer('''map(lambda i: i**2, xrange(100))''').timeit(number = 100000)
print timeit.Timer(setup="""def my_pow(i):
return i**2
""",stmt="""map(my_pow, xrange(100))""").timeit(number = 100000)
print timeit.Timer(setup="""def my_pow(i):
return i**2
""",stmt='''[my_pow(i) for i in xrange(100)]''').timeit(number = 100000)
Run Code Online (Sandbox Code Playgroud)
结果:
1.03697046805 <-- list comprehension without function call
1.96599485313 <-- map with lambda function
1.92951520483 <-- map with function call
2.23419570042 <-- list comprehension with function call
Run Code Online (Sandbox Code Playgroud) 什么是Python相当于以下(Javascript):
function wordParts (currentPart, lastPart) {
return currentPart+lastPart;
}
word = ['Che', 'mis', 'try'];
console.log(word.reduce(wordParts))
Run Code Online (Sandbox Code Playgroud)
还有这个:
var places = [
{name: 'New York City', state: 'New York'},
{name: 'Oklahoma City', state: 'Oklahoma'},
{name: 'Albany', state: 'New York'},
{name: 'Long Island', state: 'New York'},
]
var newYork = places.filter(function(x) { return x.state === 'New York'})
console.log(newYork)
Run Code Online (Sandbox Code Playgroud)
最后,这个:
function greeting(name) {
console.log('Hello ' + name + '. How are you today?');
}
names = ['Abby', 'Cabby', 'Babby', 'Mabby'];
var greet = names.map(greeting) …Run Code Online (Sandbox Code Playgroud) 我在列表中有一个元组/列表,如下所示:
[('foo','bar'),('foo1','bar1'),('foofoo','barbar')]
Run Code Online (Sandbox Code Playgroud)
python(在非常低的cpu/ram机器上运行)交换这样的值的最快方法是什么...
[('bar','foo'),('bar1','foo1'),('barbar','foofoo')]
Run Code Online (Sandbox Code Playgroud)
使用:
for x in mylist:
self.maynewlist.append((_(x[1]),(x[0])))
Run Code Online (Sandbox Code Playgroud)
有没有更好或更快的方式?
这个问题可能听起来很主观,但正如"禅宗"所说的那样,(几乎总是)有一种方式可以优先考虑,最后不应该是主观的.
什么方式更好?
[i.something() for i in l]
map(operator.methodcaller('something'), l)
map(lambda x: x.something(), l)
Run Code Online (Sandbox Code Playgroud)
(1)是(IMO)非常清楚,但在很多答案中,map()都是使用的.如果我们这样做,(2)和(3)之间的可读性几乎相同(IMO,至少).
许多其他任务同样重要,但我选择了这个,因为它可以代表所有类似的任务.
虽然我非常喜欢python,但是当我需要在同一行中获得多个整数输入时,我更喜欢C/C++.如果我使用python,我使用:
a = map(int, raw_input().split())
Run Code Online (Sandbox Code Playgroud)
这是唯一的方式还是有任何pythonic方式来做到这一点?考虑到时间,这会花费多少?
我想计算域内偶数的总和.我有两个解决方案,但我不确定每个解决方案的优点/缺点.哪种解决方案最佳?
import sys
domain = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Cal1 = sum(filter(lambda n : n % 2 == 0, domain))
Cal2 = sum([n for n in domain if n % 2 == 0])
sys.stdout.write("Cal1 = {0}\n".format(Cal1))
sys.stdout.write("Cal2 = {0}\n".format(Cal2))
Run Code Online (Sandbox Code Playgroud) 在python 2中,内置函数map似乎调用__len__时长度被覆盖.这是否正确 - 如果是这样,为什么我们计算迭代的长度来映射?Iterables不需要覆盖长度(例如),并且即使长度没有被iterable预定义,map函数也能工作.
地图在这里定义; 它确实指定在传递多个iterables的情况下存在与长度相关的功能.然而,
None
map(f, iterable)基本上相当于:
[f(x) for x in iterable]
但我遇到了一些简单的例子.
例如
class Iterable:
def __iter__(self):
self.iterable = [1,2,3,4,5].__iter__()
return self
def next(self):
return self.iterable.next()
#def __len__(self):
# self.iterable = None
# return 5
def foo(x): return x
print( [foo(x) for x in Iterable()] )
print( map(foo,Iterable()) )
Run Code Online (Sandbox Code Playgroud)
表现得如此,但如果你取消超载的话len,它就不会.
在这种情况下,它会引发AttributeError,因为iterable是None.虽然单位行为是愚蠢的,但我认为在len的规范中没有要求不变性 …
python ×10
arrays ×1
input ×1
javascript ×1
list ×1
map ×1
performance ×1
python-2.x ×1
python-3.x ×1
raw-input ×1
readfile ×1
return ×1
swap ×1
tuples ×1