简体   繁体   English

从字典中随机删除项目

[英]Removing items randomly from a dictionary

How do I remove random items from a dictionary in Python? 如何从Python中删除字典中的随机项?

I have to remove a specified number of items from a dictionary and so I tried to use dict.popitem which I thought was random, but it is seems it is not. 我必须从字典中删除指定数量的项目,所以我尝试使用dict.popitem ,我认为这是随机的,但它似乎不是。

As the docs say: 正如文档所说:

Remove and return an arbitrary (key, value) pair from the dictionary. 从字典中删除并返回任意(键,值)对。

For my problem, suppose I have a dictionary like (an example): 对于我的问题,假设我有一个字典(例如):

>>> d = dict(zip((string.ascii_lowercase), range(1, 10)))
>>> d
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8}

Now I need to remove some items from it (the count is specified by the user). 现在我需要从中删除一些项目(计数由用户指定)。

So I wrote this: 所以我写了这个:

>>> for _ in range(4):          # assume 4 items have to removed
...     d.popitem()
... 
('a', 1)
('c', 3)
('b', 2)
('e', 5)

But the problem with this code is, every time the script is run, popitem() pops exactly the same items. 但是这个代码的问题是,每次运行脚本时, popitem()弹出完全相同的项目。 You are free to test this, I have already tried it multiple times. 您可以自由测试,我已经多次尝试过。

So my question is: 所以我的问题是:

  • Why isn't popitem() working the way it should? 为什么popitem()不应该以它应该的方式工作? Is removing arbitrary items not random? 删除任意项目不是随机的?
  • How do I remove random items from a dictionary? 如何从字典中删除随机项?

Is this what you're talking about? 这就是你在说什么?

import random
for i in range(4):
    some_dict.pop( random.choice(some_dict.keys()) )   

popitem() is arbitrary but not random. popitem()是任意的,但不是随机的。 If you want to access a random element 如果要访问随机元素

import random
key = random.choice(d.keys())
val = d[key]
del d[key]

Removing an "arbitrary" element means that the function can remove whatever item it likes. 删除“任意”元素意味着该函数可以删除它喜欢的任何项目。 That doesn't mean that it has to be especially random about it. 这并不意味着它必须特别随意。

For real randomness you should use the random module. 对于真正的随机性,您应该使用random模块。 For example: 例如:

import random
for key in random.sample(d.keys(), 4):
   del d[key]
  • Why isn't popitem() working the way it should? 为什么popitem()不应该以它应该的方式工作? Is removing arbitrary items not random? 删除任意项目不是随机的?

Arbitrary does not mean the same thing as random. 任意并不意味着随机相同。 Arbitrary simply means that any of the items can be returned and nothing should be assumed about the order they are returned in. 任意简单地意味着可以返回任何项目,并且不应该假设它们返回的顺序。

  • How do I remove random items from a dictionary? 如何从字典中删除随机项?

I do not claim this is the best or most efficient way, but one way is: 我并不认为这是最好或最有效的方式,但有一种方法是:

import random
dict.pop(random.choice(dict.keys())

For removing a specified number of items, I would use random.sample instead of making repeated calls to dict.keys and random.choice 为了删除指定数量的项目,我会使用random.sample而不是重复调用dict.keysrandom.choice

for key in random.sample(d.keys(), n):
    del d[key] # or d.pop(key)
d = {'spam': 0,'url': 'http://www.python.org',  'title': 'Python Web Site'}
d.popitem()
print d

try this one I was trying to achieve the same { arbitrarily any item from dictionary } but it always deleted url but when I changed it to 尝试这个我试图实现相同的{任意来自字典}任何项目但它总是删除网址,但当我把它改为

d = {'spam': 0,'arl': 'http://www.python.org',  'title': 'Python Web Site'}
d.popitem()
print d

notice: 'u' of url changed to 'a' 注意:'你'的url改为'a'

title is deleted 标题已删除

I guess It deletes items from the list which are having highest ASCII value, \\ just guessing 我猜它会从列表中删除具有最高ASCII值的项目,\\只是猜测

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM