我的字典看起来像这样:
a = {1 : True, 2 : False, 3 : True, 4 : False, etc}
如何将值加扰以获得类似的内容?
a = {1 : True, 2 : True, 3 : False, 4 : False, etc}
答案 0 :(得分:2)
我不知道是否有更简洁的方法,但字典值上的random.shuffle
应该可以完成工作。
from random import shuffle
a = {1 : True, 2 : False, 3 : True, 4 : False}
values = list(a.values())
shuffle(values)
shuffled = dict(zip(a, values))
# {1: True, 2: False, 3: False, 4: True}
答案 1 :(得分:1)
如果通过“加扰”你的意思是随机重新排列键值配对,你可以在一行中这样做:
17:01:23.603 43598 [New I/O worker #3] RequestBodyHandler ERROR - Exception caught in RequestBodyHandler
java.nio.channels.ClosedChannelException: null
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:433) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:128) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:99) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:36) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendDownstream(DefaultChannelPipeline.java:779) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:725) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.doEncode(OneToOneEncoder.java:71) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:59) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:591) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendDownstream(DefaultChannelPipeline.java:784) ~[netty-3.10.4.Final.jar:na]
at com.typesafe.netty.http.pipelining.HttpPipeliningHandler.handleDownstream(HttpPipeliningHandler.java:88) ~[netty-http-pipelining-1.1.4.jar:na]
at org.jboss.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:591) ~[netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendDownstream(DefaultChannelPipeline.java:784) ~[netty-3.10.4.Final.jar:na]
at play.core.server.netty.NettyResultStreamer$.sendDownstream(NettyResultStreamer.scala:182) ~[play-netty-server_2.11-2.4.6.jar:2.4.6]
at play.core.server.netty.NettyResultStreamer$.play$core$server$netty$NettyResultStreamer$$nettyStreamIteratee(NettyResultStreamer.scala:140) ~[play-netty-server_2.11-2.4.6.jar:2.4.6]
at play.core.server.netty.NettyResultStreamer$$anonfun$play$core$server$netty$NettyResultStreamer$$send$1$1.streamEnum$1(NettyResultStreamer.scala:79) ~[play-netty-server_2.11-2.4.6.jar:2.4.6]
at play.core.server.netty.NettyResultStreamer$$anonfun$play$core$server$netty$NettyResultStreamer$$send$1$1.apply(NettyResultStreamer.scala:86) ~[play-netty-server_2.11-2.4.6.jar:2.4.6]
at play.core.server.netty.NettyResultStreamer$$anonfun$play$core$server$netty$NettyResultStreamer$$send$1$1.apply(NettyResultStreamer.scala:60) ~[play-netty-server_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) ~[scala-library-2.11.7.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) ~[play-iteratees_2.11-2.4.6.jar:2.4.6]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) ~[play-iteratees_2.11-2.4.6.jar:2.4.6]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.link(Promise.scala:304) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.linkRootOf(Promise.scala:289) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) ~[scala-library-2.11.7.jar:na]
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:91) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91) ~[akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72) ~[scala-library-2.11.7.jar:na]
at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:90) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) ~[akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) ~[akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library-2.11.7.jar:na]
然而,这不如使用from random import sample
a = dict(zip(a, sample(a.values(), len(a))))
那么有效。
另外,请注意,在Python 3中,shuffle
会返回一个dict_values
object,其行为与您的预期不同。因此,您需要编写以下内容:
dict.values
答案 2 :(得分:1)
a = dict(zip(a, sample(list(a.values()), len(a))))
答案 3 :(得分:0)
可能你想要这个:
>>> a = {1 : True, 2 : False, 3 : True, 4 : False}
>>> import random
>>> for key in a:
... val = random.choice([True, False])
... a[key] = val
...
>>> a
{1: True, 2: True, 3: True, 4: False}