相关疑难解决方法(0)

如何在python中将元组元素作为参数传递给函数?

我有一个由元组组成的列表,我想将每个元组的元素作为参数传递给函数:

mylist = [(a, b), (c, d), (e, f)]

myfunc(a, b)
myfunc(c, d)
myfunc(e, f)
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

最好的祝福

python arguments tuples list

12
推荐指数
1
解决办法
2万
查看次数

什么是避免在字符串中指定相同值的最pythonic方法

message = "hello %s , how are you %s, welcome %s"%("john","john","john")
Run Code Online (Sandbox Code Playgroud)

什么是避免指定"john"3次而是指定一个短语的最pythonic方法.

python string formatting

8
推荐指数
2
解决办法
286
查看次数

笨重的计算增量数字之间的差异,是否有更美妙的方式?

以下代码工作得很好.但它看起来如此冗长,肯定有一种更优雅的方式来计算它?

我的想法是我有一个包含100个递增时间戳的列表,我想查看这些时间戳并计算每个时间戳之间的平均时间.

下面的代码起作用,但我确信反转这样的列表真的很低效.

有什么建议?

#!/usr/bin/python 

nums = [1,4,6,10]
print nums
nums_orig = list(nums)

nums_orig.pop()
nums.reverse()
nums.pop()
nums.reverse()

print nums
print nums_orig

total = 0

for idx, val in enumerate(nums):
  difference = val - nums_orig[idx]
  total += difference
  print idx, val - nums_orig[idx]

print "Mean difference is %d" % (total / len(nums))
Run Code Online (Sandbox Code Playgroud)

python list

2
推荐指数
2
解决办法
166
查看次数

标签 统计

python ×3

list ×2

arguments ×1

formatting ×1

string ×1

tuples ×1