Re: list indices must be integers or slices, not str

2022-07-21 Thread Frank Millman
On 2022-07-20 4:45 PM, Chris Angelico wrote: On Wed, 20 Jul 2022 at 23:50, Peter Otten <__pete...@web.de> wrote: I found https://peps.python.org/pep-3101/ """ PEP 3101 – Advanced String Formatting ... An example of the ‘getitem’ syntax: "My name is {0[name]}".format(dict(name='Fred')) It sh

Re: list indices must be integers or slices, not str

2022-07-20 Thread Roel Schroeven
;1' >>>   >>> >>>   >>> '{x[-1]}'.format(**vars()) >>> Traceback (most recent call last): >>>     File "", line 1, in >>> TypeError: list indices must be integers or slices, not str >>>   >>> >

Re: list indices must be integers or slices, not str

2022-07-20 Thread Martin Di Paola
offtopic If you want a pure-python but definitely a more hacky implementation, you can play around with inspect.stack() and get the variables from the outer frames. # code: x = 32 y = 42 printf("Hello x={x}, y={y}", x=27) # output: Hello x=27, y=42 The implementation of printf() was never re

Re: list indices must be integers or slices, not str

2022-07-20 Thread Mats Wichmann
On 7/20/22 05:04, Frank Millman wrote: > I think the preferred style these days is f'{x[-1]}' which works." > > Unfortunately the 'f' option does not work for me in this case, as I am > using a string object, not a string literal. For that you could consider https://pypi.org/project/f-yeah/ (s

Re: list indices must be integers or slices, not str

2022-07-20 Thread Chris Angelico
On Wed, 20 Jul 2022 at 23:50, Peter Otten <__pete...@web.de> wrote: > > I found > > https://peps.python.org/pep-3101/ > > """ > PEP 3101 – Advanced String Formatting > ... > An example of the ‘getitem’ syntax: > > "My name is {0[name]}".format(dict(name='Fred')) > > It should be noted that the use

Re: list indices must be integers or slices, not str

2022-07-20 Thread MRAB
;> >>> '{x[1]}'.format(**vars()) >> '1' >> >>> >> >>> '{x[-1]}'.format(**vars()) >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: list indices must b

Re: list indices must be integers or slices, not str

2022-07-20 Thread Peter Otten
or "license" for more information. >>> >>> x = list(range(10)) >>> >>> '{x[1]}'.format(**vars()) '1' >>> >>> '{x[-1]}'.format(**vars()) Traceback (most recent call last): File "", lin

Re: list indices must be integers or slices, not str

2022-07-20 Thread Chris Angelico
On Wed, 20 Jul 2022 at 21:06, Frank Millman wrote: > I saw this from Paul Rubin - for some reason his posts appear in google > groups, but not python-list. > > "It seems to only want integer constants. x[2+2] and x[k] where k=2 > don't work either. Yes, that's for the same reason that x[spam] can

Re: list indices must be integers or slices, not str

2022-07-20 Thread Chris Angelico
>>> > >> >>> '{x[1]}'.format(**vars()) > >> '1' > >> >>> > >> >>> '{x[-1]}'.format(**vars()) > >> Traceback (most recent call last): > >> File "", l

Re: list indices must be integers or slices, not str

2022-07-20 Thread Frank Millman
;, "copyright", "credits" or "license" for more information.   >>>   >>> x = list(range(10))   >>>   >>> '{x[1]}'.format(**vars()) '1'   >>>   >>> '{x[-1]}'.format(**vars()) Traceback

Re: list indices must be integers or slices, not str

2022-07-20 Thread Frank Millman
or "license" for more information. >>> >>> x = list(range(10)) >>> >>> '{x[1]}'.format(**vars()) '1' >>> >>> '{x[-1]}'.format(**vars()) Traceback (most recent call last): File ""

Re: list indices must be integers or slices, not str

2022-07-20 Thread Chris Angelico
ense" for more information. > >>> > >>> x = list(range(10)) > >>> > >>> '{x[1]}'.format(**vars()) > '1' > >>> > >>> '{x[-1]}'.format(**vars()) > Traceback (most recent call last): >

list indices must be integers or slices, not str

2022-07-20 Thread Frank Millman
> >>> '{x[1]}'.format(**vars()) '1' >>> >>> '{x[-1]}'.format(**vars()) Traceback (most recent call last):   File "", line 1, in TypeError: list indices must be integers or slices, not str >>> Can anyone explain this