任意复制列表中的每个元素

时间:2016-11-04 11:14:15

标签: python list python-3.x numpy

我想知道如何在列表中任意复制每个元素,例如

~/Paddle/demo/rowrow$ bash train.sh 
I1104 18:59:42.636052 18632 Util.cpp:151] commandline: /home/ltan/Paddle/binary/bin/../opt/paddle/bin/paddle_trainer --config=train.conf --save_dir=/home/ltan/Paddle/demo/rowrow/model --use_gpu=true --num_passes=100 --show_parameter_stats_period=1000 --trainer_count=4 --log_period=10 --dot_period=5 
I1104 18:59:46.503566 18632 Util.cpp:126] Calling runInitFunctions
I1104 18:59:46.503810 18632 Util.cpp:139] Call runInitFunctions done.
[WARNING 2016-11-04 18:59:46,847 default_decorators.py:40] please use keyword arguments in paddle config.
[INFO 2016-11-04 18:59:46,856 networks.py:1125] The input order is [source_language_word, target_language_word, target_language_next_word]
[INFO 2016-11-04 18:59:46,857 networks.py:1132] The output order is [__cost_0__]
I1104 18:59:46.871026 18632 Trainer.cpp:170] trainer mode: Normal
I1104 18:59:46.871906 18632 MultiGradientMachine.cpp:108] numLogicalDevices=1 numThreads=4 numDevices=4
I1104 18:59:46.988584 18632 PyDataProvider2.cpp:247] loading dataprovider dataprovider::process
[INFO 2016-11-04 18:59:46,990 dataprovider.py:15] src dict len : 45661
[INFO 2016-11-04 18:59:47,316 dataprovider.py:26] trg dict len : 422
I1104 18:59:47.347944 18632 PyDataProvider2.cpp:247] loading dataprovider dataprovider::process
[INFO 2016-11-04 18:59:47,348 dataprovider.py:15] src dict len : 45661
[INFO 2016-11-04 18:59:47,657 dataprovider.py:26] trg dict len : 422
I1104 18:59:47.658279 18632 GradientMachine.cpp:134] Initing parameters..
I1104 18:59:49.244287 18632 GradientMachine.cpp:141] Init parameters done.
F1104 18:59:50.485621 18632 PythonUtil.h:213] Check failed: PySequence_Check(seq_) 
*** Check failure stack trace: ***
    @     0x7f71f521adaa  (unknown)
    @     0x7f71f521ace4  (unknown)
    @     0x7f71f521a6e6  (unknown)
    @     0x7f71f521d687  (unknown)
    @           0x54dac9  paddle::DenseScanner::fill()
    @           0x54f1d1  paddle::SequenceScanner::fill()
    @           0x5543cc  paddle::PyDataProvider2::getNextBatchInternal()
    @           0x5779b2  paddle::DataProvider::getNextBatch()
    @           0x6a01f7  paddle::Trainer::trainOnePass()
    @           0x6a3b57  paddle::Trainer::train()
    @           0x53a2b3  main
    @     0x7f71f4426f45  (unknown)
    @           0x545ae5  (unknown)
    @              (nil)  (unknown)
/home/ltan/Paddle/binary/bin/paddle: line 81: 18632 Aborted                 (core dumped) ${DEBUGGER} $MYDIR/../opt/paddle/bin/paddle_trainer ${@:2}

l = ['a', 'b', 'c'] 中的重复元素会产生新列表,

l

所以'a'重复了3次,'b'一次,'c'重复了两次。每个元素的重复数量由n = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c'] 决定,例如: numpy.random.poisson

3 个答案:

答案 0 :(得分:2)

这是使用np.repeat创建数组的基于NumPy的矢量化方法 -

color

如果您需要列表作为输出,请在其中附加.tolist() -

np.repeat(l, np.random.poisson([2]*len(l)))

如果您想为每个元素至少保留一个条目,请在其中添加np.repeat(l, np.random.poisson([2]*len(l))).tolist() 的剪辑。

答案 1 :(得分:1)

将列表中的每个元素与np.random.poisson([2]*len(arr)).clip(min=1)numpy.random.poisson(2)返回的值相乘,然后将其提供给join

list

对于一次运行,这随机导致:

r = list(''.join(i*random.poisson(2) for i in l))

由于您使用['a', 'b', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c'] 两种方式,我会选择Divakar的解决方案(对于比您的示例更大的列表,执行速度更快)。

答案 2 :(得分:1)

>>> l = ['a', 'b', 'c']
>>> n = []
>>> for e in l:
...     n.extend([e] * numpy.random.poisson(2))
... 
>>> n
['a', 'a', 'b', 'c']