[Py023]List元素重复
2019-01-09 本文已影响21人
安哥生个信
list元素重复有两种情况,
1.按照顺序整体重复,直接*
就可以
['a','b','c'] * 2
Out[22]: ['a', 'b', 'c', 'a', 'b', 'c']
2.每个元素一次重复
[i for i in ['a','b','c'] for j in range(2)]
Out[23]: ['a', 'a', 'b', 'b', 'c', 'c']
list元素重复有两种情况,
1.按照顺序整体重复,直接*
就可以
['a','b','c'] * 2
Out[22]: ['a', 'b', 'c', 'a', 'b', 'c']
2.每个元素一次重复
[i for i in ['a','b','c'] for j in range(2)]
Out[23]: ['a', 'a', 'b', 'b', 'c', 'c']