List with duplicated values and suffix

前端 未结 6 1454
抹茶落季
抹茶落季 2020-11-30 02:48

I have a list, a:

a = [\'a\',\'b\',\'c\']

and need to duplicate some values with the suffix _ind added this way (

6条回答
  •  时光取名叫无心
    2020-11-30 03:34

    Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:

    >>> a = ['a', 'b', 'c']
    >>> b = []
    >>> for x in a: b.extend([x, x+'_ind'])
    ... 
    >>> b
    ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
    

    * I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.

提交回复
热议问题