[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]

/prog/ - Programming

Programming

Name
Email
Subject
REC
STOP
Comment *
File
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶Show post options & limits]
Confused? See the FAQ.
Options

Allowed file types:jpg, jpeg, gif, png, webp,webm, mp4, mov
Max filesize is16 MB.
Max image dimensions are15000 x15000.
You may upload5 per post.


33e233 No.5138

Discuss and share Python related talks here.

This includes all versions just be explicit about it.

____________________________
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

0c559b No.5190

Gimme some sources to learn python

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

647e4f No.5196

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

97f363 No.5378

Hey, I'm learning Python, specifically Python 3.6. I came across an issue involving tuples, specifically with:

tuple.index(x)

Whenever x is a variable defined by an input, it gives me a value error saying it is not in the tuple. Is there some work-around?

I can provide the original code and error message as needed.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

43df32 No.5379

>>50378

if i remember then input is a string and needs to be convertet to an int if you want to use it with index

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

475d53 No.5391

>>5378

https://docs.python.org/3/library/stdtypes.html#index-19

"""8. index raises ValueError when x is not found in s. Not all implementations support passing the additional arguments i and j. These arguments allow efficient searching of subsections of the sequence. Passing the extra arguments is roughly equivalent to using s[i:j].index(x), only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice."""

While this is poor design, it is documented behavior. The workaround is:


>>> import operator
>>> import functools
>>> def seqindex (seq, item):
... return next (
... map (operator.itemgetter (0),
... filter (operator.itemgetter (1),
... enumerate (
... map (functools.partial (operator.eq, item),
... seq)))), -1)
...
>>> seqindex ((1, 2, 3), 3)
2
>>> seqindex ((1, 2, 3), 4)
-1
>>>

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

97f363 No.5393

>>5378

OP here, fixed the code, works fine now. I appreciate the help.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

791c13 No.5445

>>5378

Was it a type thing? Did you have a tuple with integers in it, nad were trying to find the index using a string variable?

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.



[Return][Go to top][Catalog][Nerve Center][Random][Post a Reply]
Delete Post [ ]
[]
[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]