Saturday, 2 September 2017

python - Getting the first x item from a list










I have a string and I'm splitting in a ; character, I would like to associate this string with variables, but for me just the first x strings is useful, the other is redundant;



I wanted to use this code below, but if there is more than 4 coma than this raise an exception. Is there any simply way?




az1, el1, az2, el2, rfsspe = data_point.split(";")  

Answer



Yes! Use slicing:



az1, el1, az2, el2, rfsspe = data_point.split(";")[:5]


That "slices" the list to get the first 5 elements only.



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...