Possible Duplicate:
Good Primer for Python Slice Notation
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(";")
Using Count() The python list method count() returns count of how many times an element occurs in list. So if we have the same element repeated in the list then the length of the list using len() will be same as the number of times the element is present in the list using the count().
Method 1: Using *set() It first removes the duplicates and returns a dictionary which has to be converted to list.
The way, I do this is usually to add all the variables to a list(var_list) and then when I'm processsing the list I do something like
for x in var_list[:5]:
print x #or do something
Yes! Use slicing:
az1, el1, az2, el2, rfsspe = data_point.split(";")[:5]
That "slices" the list to get the first 5 elements only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With