Is there a shorthand syntax for a sequence of throwaway variables?

后端 未结 3 1526
抹茶落季
抹茶落季 2021-01-22 07:42

Let\'s say you have a situation like this:

_, _, _, substring_1, _, substring_2 = some_string.split(\',\')

Is there a shorthand way of expressi

3条回答
  •  失恋的感觉
    2021-01-22 08:19

    Not a syntactic shortcut, but you might want to use the itemgetter function from the operator module:

    from operator import itemgetter
    my_pair = itemgetter(3, 5)
    substring_1, substring_2 = my_pair(some_string.split(','))
    

    Or, define my_pair to wrap the call to split as well:

    def my_pair(x):
        return itemgetter(3,5)(x.split(','))
    
    substring_1, substring_2 = my_pair(some_string)
    

提交回复
热议问题