Liste des Groupes | Revenir à cl c |
On 6/14/2024 11:56 PM, Keith Thompson wrote:I consider myself more of a C developer than a Python developer, but I use Python regularly. I would say that my knowledge of the C language and standard, while not as deep as some others here, covers a far higher proportion of the language than my knowledge of Python covers of Python. But I think you can make good use of Python while knowing a smaller fraction of the language and library than for C.DFS <nospam@dfs.com> writes:> just replacing nums[i/2] by nums[i//2] would do the trick,On 6/14/2024 9:39 PM, Keith Thompson wrote:>DFS <nospam@dfs.com> writes:>
[...]During conversion, I got a Python error I don't remember seeing in the past:[...]
>
"TypeError: list indices must be integers or slices, not float"
>
because division returns a float, and some of the array addressing was
like this: nums[i/2].
C's "/" operator yields a result with the type of the operands
(after
promotion to a common type).
Python's "/" operator yields a floating-point result. For C-style
integer division, Python uses "//". (Python 2 is more C-like.)
I was surprised python did that, since every division used in the
array addressing results in an integer.
>
After casting i to an int before any array addressing, // works.
I'm surprised you needed to convert i to an int. I would think that
> as long> as i always has an int value (note Python's dynamic typing). If iis acquiring a float value, that's probably a bug, given the name.I spotted the issue. Just prior to using i for array addressing I said:
i = N/2.
The fix is set i = int(N/2)
But if you want help with your Python code, comp.lang.python is theThanks for your help, but David Brown is a Python developer and I'll ask him python questions here whenever I care to.
place to ask.
In the recent past you were involved in discussions on perl, Fortran and awk, among other off-topics.If occasional questions or discussions about other languages pop up here, people will often answer them. But for more in-depth discussions or questions, this is not the newsgroup - comp.lang.python is the place for Python questions. (You'll also probably get better answers there than I can give.)
Rules for thee but not for me?
Les messages affichés proviennent d'usenet.