- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我编写了一个简单的脚本来模拟基于每用户平均收入 (ARPU)、利润率和客户保持客户的年数 (ltvYears) 的客户生命周期值(value) (LTV)。下面是我的脚本。它在“ltvYears = ltvYears + [ltv_loop]”这一行抛出错误。错误信息是“TypeError: can only concatenate tuple (not "list") to tuple”。有人能告诉我是什么原因造成的吗?我怀疑问题可能源于“ltvYears = ('f',[])”,我在其中添加了类型代码以消除另一个错误(将 float 乘以 int)。
我是 Python 的新手,所以这段代码很可能是初学者的错误。
lowYears = 0
highYears = 20
modeYears = 3
ARPU = 65.0*12.0
MARGIN = .30
ltvYears = ('f',[])
ltv = []
def ltv(arpu, years, margin):
return arpu * years * margin
N = 10000
for n in range(N):
#estimate LTV
ltv_loop = random.triangular(lowYears, highYears, modeYears)
ltvYears = ltvYears + [ltv_loop]
ltv = ltv + [ltv(ARPU, ltvYears, MARGIN)]
show = 0
if (show==1):
#plot ltv histogram
plt.hist(ltv,bins=10)
plt.title("LTV Probability Density")
plt.xlabel("")
plt.ylabel("$")
plt.show()
编辑 - 这是我的变量的屏幕截图。
EDIT2 ---感谢下面的帮助,我找到了解决方案。一共有三个问题:
+1 @DonCallisto 和@RikPoggi 对项目 2 和 3 的帮助
最佳答案
ltvYears
是一个元组,它的定义如下
ltvYears = ('f',[])
所以说到这一行:
ltvYears = ltvYears + [ltv_loop]
Python 不知道该怎么做,因为您的错误清楚地表明:
"TypeError: can only concatenate tuple (not "list") to tuple"
不清楚该行的行为应该是什么,因此很难提出解决方案。
也许您只需要将 ltvYears 定义为 list
并调用 .append
方法。
Here OP 说:
ltvYears = ('f',[])
I added the'f'
to indicate that the array is to contain floats. Before I did this I was getting an error about multiplying floats and ints.
你不需要那样做。 Python 不是 C。
只是做:
ltvYears = []
及以后:
ltvYears.append(ltv_loop)
关于python - 类型错误 : can only concatenate tuple (not "list") to tuple",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506904/
我有一个包含文件名 和文件路径 的元组列表。我想找到重复的 filename(但 filepath 可能不同),即文件名相同但 filepath 可能不同的元组。 元组列表示例: file_info
我有一个像这样定义的变量 auto drum = std::make_tuple ( std::make_tuple ( 0.3f , Ex
我有一个包含几个字段的自定义结构,我想在快速 switch 语句中对其进行模式匹配,这样我就可以通过将其中一个字段与另一个字段进行比较来自定义匹配正则表达式。 例如鉴于这种结构: struct MyS
我有一种动态元组结构: template //Should only be tuples class DynamicTuple { vector data; //All data is st
这个问题在这里已经有了答案: What and When to use Tuple? [duplicate] (5 个答案) 关闭 8 年前。 我正在查看 Tuple 的在线示例,但我没有看到任何理
在我的项目中我有很多坐标要处理,在二维情况下我发现(cons x y)的构造比(list x y)快和 (vector x y)。 但是,我不知道如何将 cons 扩展到 3D 或更进一步,因为我没有
我有以下 Scala 代码: def f(x: Int, y: Int): Option[String] = x*y match { case 0 => None case n =>
我的直觉告诉我,在一般情况下,只有宏或复杂类型的体操才能解决这个问题。 Shapeless 或 Scalaz 可以在这里帮助我吗?这是 N=2 问题的具体实例,但我正在寻找的解决方案适用于所有合理的
为什么这段 Scala 代码是这样的: class Test { def foo: (Int, String) = { (123, "123") } def bar: Unit
我是 python 和 pygame 的新手,我正在尝试学习向量和类的基础知识,但在这个过程中我搞砸了,而且我在理解和修复标题中的错误消息方面苦苦挣扎。 这是我的 Vector 类的代码: impor
我正在编写一个程序来打开和读取一个 txt 文件,并在每一行中循环。将第 2 列和第 4 列中的值相乘并将其分配给第 5 列。 A 500.00 A 84.15 ? B 648.80 B 77.61
我知道还有其他几个问题提出了完全相同的问题,但是当我运行时: 导入命令 从 pyDes 导入 * def encrypt(data, password,): k = des(password,
我有一个元组列表,内容如下: >>>myList [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', '
给定一个 boost::tuple 和 std::tuple,你如何在它们之间进行转换? 也就是说,您将如何实现以下两个功能? template boost::tuple asBoostTuple(
我无法初始化 std::tuple来自 std::tuple 的逐元素元素兼容类型。为什么它不像 boost::tuple 那样工作? #include #include template st
我是 Storm 的新手并且我正在尝试找出如何编写一个 bolt 测试来测试子类 BaseRichBolt 中的 execute(Tuple tuple) 方法。 问题是 Tuple 似乎是不可变的,
如果我有如下元组列表: [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')] 我想删除重复的元组(在内容和内部项目顺序方面重复)以便输出为: [('a',
我编写了一个简单的脚本来模拟基于每用户平均收入 (ARPU)、利润率和客户保持客户的年数 (ltvYears) 的客户生命周期值(value) (LTV)。下面是我的脚本。它在“ltvYears =
以下是我的代码,它是一组元组:。输出:设置([(‘A’,20160129,36.44),(‘A’,20160104,41.06),(‘A’,20160201,37.37)])。如何将另一个元组(‘A’
我用以下代码编写了一个程序: import pandas as pd import numpy as np from typing import Tuple def split_data(self,
我是一名优秀的程序员,十分优秀!