Python slice first and last element in list

后端 未结 17 1477
有刺的猬
有刺的猬 2020-12-02 17:54

Is there a way to slice only the first and last item in a list?

For example; If this is my list:

>>> some_list
[\'1\', \'B\', \'3\', \'D\',          


        
17条回答
  •  庸人自扰
    2020-12-02 18:29

    These are all interesting but what if you have a version number and you don't know the size of any one segment in string from and you want to drop the last segment. Something like 20.0.1.300 and I want to end up with 20.0.1 without the 300 on the end. I have this so far:

    str('20.0.1.300'.split('.')[:3])
    

    which returns in list form as:

    ['20', '0', '1']
    

    How do I get it back to into a single string separated by periods

    20.0.1
    

提交回复
热议问题