有更优雅的方式吗?我想让python迭代arg,如果它是一个列表,或者只是拿arg,如果它是一个int。数据是一个numpy.ndarray
def plot(data, run, t):
if isinstance(run, int):
run = [run]
fig = plt.figure()
ax = fig.add_subplot(111)
for run in run:
plt.plot(t, data[run])
plt.savefig('data.png', dpi=200)
答案 0 :(得分:1)
您可以减少代码中的一行:
def fcn(arg):
for a in ([arg] if isinstance(arg, int) else arg):
print a
答案 1 :(得分:-1)
def example(*arg):
print "I was called with", len(arg), "arguments:", arg
>>> example(1)
I was called with 1 arguments: (1,)
>>> example(1, 2,3)
I was called with 3 arguments: (1, 2, 3)