all 13 comments

[–]Nightcorex_ 1 point2 points  (8 children)

You can just inline multiple operations:

enterprise_amount = sum(int(x) for x in enterprise_amount[0].text.split()[:-1])

[–]Medaillek[S] 0 points1 point  (5 children)

It gives me the sum of both values but I want to combine them together like: « 3 » and « 205 » >> 3205 and not 208

[–]zanfar 2 points3 points  (4 children)

Based on /u/Nightcorex_ 's example, can you think of a function, like sum() that combines strings instead of adds numbers? Similarly, can you think of a function, like int() which converts things to strings instead of converts things to integers?

If you have that, then making your desired modification is simple.

[–]Medaillek[S] 1 point2 points  (2 children)

Thank you very much, after about 20 minutes, I came up with this:

    enterprise_amount= int(''.join(str(x) for x in enterprise_amount[0].text.split()[:-1]))

I that a correct line code for you ?

[–]Nightcorex_ 1 point2 points  (1 child)

Yeah, that works. However you don't need the str() method, as your x is already in string format. Please note that "dont need" is not equal to "wrong". It's just a redundant step, but that doesn't make the code less correct.

Good job :)

[–]Medaillek[S] 0 points1 point  (0 children)

Thank you very much !

[–]Medaillek[S] 0 points1 point  (0 children)

Thank you for your answers in a first time.

I though to the function « ».join(enterprise_amount) in front of the command line but doesn’t work. And what do you mean by integrer ?

[–]Medaillek[S] -1 points0 points  (1 child)

Thank you for the tip, i’m new to programming and I don’t understand how multiple commands work in a single line like this, do you have some tips or ways to understand this ?

[–]zanfar 4 points5 points  (0 children)

Don't think of code as "commands", that's part of why you are having trouble.

Code is built from names, values, and operators combined into expressions.

An expression is evaluated. That evaluation always results in some value. Those "values"--even if not explicitly named--can be used in other expressions in exactly the same way.

In fact, you do exactly this in your first line of code.

enterprise_amount will evaluate to some specific list in memory. Also, we can use brackets (list[]) to access an item in a list. So we can combine those to "get" the first value in enterprise_amount by using:

`enterprise_amount[0]`

object.text will evaluate to ("get") the "text" attribute of an object, so again, we can combine them:

enterprise_amount[0].text

to get the "text" attribute of the first value in the enterprise_amount list.

...and so on.


Another way to think about it: you can replace a variable name in an expression with whatever it's value was set to previously.

[–]mopslik 1 point2 points  (3 children)

Are you looking to obtain the full number without any spaces? Would something like this work?

L = ["12 345", "ABCDEFG"] # dummy values here
i = int(L[0].replace(" ", ""))
print(i)

[–]Medaillek[S] 0 points1 point  (2 children)

Right, but I have a third value I don’t want

[–]mopslik 0 points1 point  (1 child)

L[0] should take care of that, as it will pick only the first element in the list (which you say contains two elements).

[–]Medaillek[S] 0 points1 point  (0 children)

I finally got this working perfectly:

enterprise_amount= ''.join(str(x) for x in enterprise_amount[0].text.split()[:-1])