TypeError: only the tuple concatenation (and not the "list") for the tuple can

I wrote a simple script to simulate the customer life cycle (LTV) value based on the average revenue per user (ARPU), margin and the number of customers remaining customers (ltvYears). Below is my script. It throws an error in this line "ltvYears = ltvYears + [ltv_loop]". The error message "TypeError: can only concatenate the tuple (and not the" list ") for the tuple." Can someone tell me what causes this? I suspect that the problem may be related to "ltvYears = ('f', [])", where I added type code to fix another error (multiplying float by int).

I am new to Python, so a beginner error is very likely in this code.

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() 

EDIT is a screenshot of my variables. enter image description here

EDIT2 --- I understood the solution thanks to the help below. There were three problems in total:

  • I mistakenly assigned the same variable and function name (+1 @autopopulated to indicate this)
  • This line was an extraneous "ltvYears = ltvYears + [ltv_loop]"
  • This line should use the used "ltv_loop" for the second argument "ltv = ltv + [calculateltv (ARPU, ltv_loop, MARGIN)]"

+1 @DonCallisto and @RikPoggi for help, which in paragraphs 2 and 3

+6
source share
5 answers

ltvYears is a tuple, it is defined as

 ltvYears = ('f',[]) 

so when it comes to this line:

 ltvYears = ltvYears + [ltv_loop] 

Python does not know what to do, as your error clearly says:

"TypeError: can only concatenate the tuple (and not the" list ") for the tuple"

It is not clear what should behave in this line, so it is difficult to propose a solution.

Maybe you just need to define ltvYears as list and call the .append method.


Edit

Here the OP said:

ltvYears = ('f',[]) I added 'f' to indicate that the array should contain floats. Before I did this, I received a message about multiplying float and ints.

You do not need to do this. Python is not C.

Just do:

 ltvYears = [] 

and later:

 ltvYears.append(ltv_loop) 
+9
source

if ltvYears is a tuple, then you can do this:

 ltvYears += (ltv_loop,) 
+4
source

From your code

 ltvYears = ltvYears + [ltv_loop] 

ltvYears is a tuple, and so you get this error.

This is because you cannot add list to a tuple. You may need to change the type of ltvYears to make it a list

+2
source

(This is another problem you are asking, see @Rik for an answer, but you also have this problem)

You defined a function with the same name as the variable:

 ... ltv = [] def ltv(arpu, years, margin): return arpu * years * margin ... 

This will replace your list variable with a function object, which of course you cannot add, so this line will not be executed: ltv = ltv + [ltv(ARPU, ltvYears, MARGIN)] .

+2
source

This is actually as simple as the error message: you are not allowed to link lists and tuples.

ltvYears is a tuple. [ltv_loop] is a list.

0
source

Source: https://habr.com/ru/post/915236/


All Articles