Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
¿Sintaxis para devolver la lista completa cuando se usa el signo menos?

Las listas de Python tienen ingeniosas capacidades de indexación/corte. Aquí hay varios ejemplos:

 x = "123456" x[:-3] '123' x[:-1] '12345' # -1 slices off last element x[:-0] # -0 slices off .. everything .. this is what i'd like to fix ''

Me gustaría cortar un número variable de elementos d :

 x[:-d]

Pero si d fuera 0 obtenemos un resultado muy diferente al deseado. Una solución es:

 d = 0 x[:-d if d else len(x)] '123456'

Eso es posible, pero ¿hay alguna alternativa [más corta]?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El problema al que se enfrenta es que -0 es lo mismo que 0 y x[0:0] es un segmento vacío.

Yo sugeriría:

 x[:len(x)-d]
over 4 years ago · Santiago Trujillo Report

0

Puede usar None , que es el valor predeterminado para los valores de inicio/parada/paso que faltan:

 x[:-d or None]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!