在它自己之后再次添加数组的每个成员

时间:2012-03-06 15:26:16

标签: python data-structures recursive-datastructures

如果我有这个Python数组:

mac_tags = [ "global_rtgn", "global_mogn" ]

我想要这个Python数组:

mac_tags = [ "global_rtgn", "global_rtgn", "global_mogn","global_mogn" ]

我如何以编程方式创建它?

4 个答案:

答案 0 :(得分:4)

new_mac_tags = []
for tag in mac_tags:
    new_mac_tags += [tag, tag]

from itertools import chain, izip
new_mac_tags = list(chain.from_iterable(izip(mac_tags, mac_tags)))

答案 1 :(得分:1)

>>> [a for a in mac_tags for x in range(2)]
['global_rtgn', 'global_rtgn', 'global_mogn', 'global_mogn']

答案 2 :(得分:-1)

[i for i in sorted(mac_tags+mac_tags)]

答案 3 :(得分:-1)

请注意,这更像是一种功能性的方法,可能不是纯粹的惯用python代码。

data = [[s, s] for s in [ "global_rtgn", "global_mogn" ]]

data = sum (data, [])

print data