Tag: 字典

从Python字典中删除项目的最佳方法?

项目密钥未知时,从字典中删除项目的最佳方法是什么? 这是一个简单的方法: for key, item in some_dict.items(): if item is item_to_remove: del some_dict[key] 有更好的方法吗? 在迭代它的时候,从字典中突变(删除项目)有什么问题吗?

将Django模型对象转换为所有字段不变的字典

如何将django模型对象转换为所有字段的字典? 所有理想情况下都包含带有editable = False的外键和字段。 让我详细说一下。 假设我有如下的django模型: from django.db import models class OtherModel(models.Model): pass class SomeModel(models.Model): value = models.IntegerField() value2 = models.IntegerField(editable=False) created = models.DateTimeField(auto_now_add=True) reference1 = models.ForeignKey(OtherModel, related_name="ref1") reference2 = models.ManyToManyField(OtherModel, related_name="ref2") 在terminal,我做了以下几点: other_model = OtherModel() other_model.save() instance = SomeModel() instance.value = 1 instance.value2 = 2 instance.reference1 = other_model instance.save() instance.reference2.add(other_model) instance.save() 我想将其转换为下面的字典: {'created': datetime.datetime(2015, […]

.NET – 字典locking与ConcurrentDictionary

我无法find关于ConcurrentDictionarytypes的足够信息,所以我想我会在这里问一下。 目前,我使用一个Dictionary来保存所有由多个线程(从一个线程池,所以没有确切数量的线程)不断访问的用户,并且它已经同步访问。 我最近发现在.NET 4.0中有一组线程安全的集合,它看起来非常令人愉快。 我想知道,什么是“更有效和更容易pipe理”选项,因为我有一个正常的Dictionary与同步访问之间的选项,或者有一个已经线程安全的ConcurrentDictionary 。 参考.NET 4.0的ConcurrentDictionary

我在哪里可以下载英文字典数据库的文本格式?

我需要阅读文本文件的一个字,并返回其含义。 任何其他文件格式也将工作。

如何获得字典中的密钥列表?

我从来没有得到任何代码,我尝试工作。 我想要的键而不是值(还)。 使用另一个arrayscertificate是太多的工作,我也使用删除。

C#DropDownList与作为数据源字典

我想使用languageCod (en-gb)的Dictionary(列表)作为键和语言名(英文)作为要显示的文本来设置Dropdownlist (languageList)的DataTextField和DataValueField 。 相关编码: string[] languageCodsList= service.LanguagesAvailable(); Dictionary<string, string> list = new Dictionary<string, string>(languageCodsList.Length); foreach (string cod in languageCodsList) { CultureInfo cul = new CultureInfo(cod); list.Add(cod, cul.DisplayName); } languageList.DataSource = list; languageList.DataBind(); 我怎样才能设置DataTextField和DataValueField ?

使用LINQ将列表转换为字典,而不必担心重复

我有一个Person对象列表。 我想转换为一个字典,其中的关键是名和姓(连接),值是Person对象。 问题是我有一些重复的人,所以如果我使用这个代码,这将炸毁: private Dictionary<string, Person> _people = new Dictionary<string, Person>(); _people = personList.ToDictionary( e => e.FirstandLastName, StringComparer.OrdinalIgnoreCase); 我知道这听起来很奇怪,但我现在不太在乎重复的名字。 如果有多个名字,我只想抓住一个。 有没有反正我可以写上面的代码,所以它只是一个名字,不会炸毁重复?

迭代通过std :: map的顺序是否已知(并由标准保证)?

我的意思是 – 我们知道std::map的元素是根据键来sorting的。 所以,假设键是整数。 如果我使用for来从std::map::begin()迭代到std::map::end() ,那么标准保证我将通过带有键的元素迭代,按照升序sorting? 例: std::map<int, int> map_; map_[1] = 2; map_[2] = 3; map_[3] = 4; for( std::map<int, int>::iterator iter = map_.begin(); iter != map_.end(); ++iter ) { std::cout << iter->second; } 这是保证打印234还是它的实现定义? 真实的原因:我有一个std::map int键。 在非常罕见的情况下,我想遍历所有元素,大于一个具体的int值。 是的,这听起来像std::vector将是更好的select,但注意到我的“非常罕见的情况下”。 编辑 :我知道, std::map的元素是sorting..没有必要指出(大部分的答案在这里)。 我甚至写在我的问题。 当我迭代一个容器时,我正在询问迭代器和顺序。 谢谢@Kerrek SB的答案。

如何使用python-3.x中的字典格式化string?

我是使用字典格式化string的忠实粉丝。 它帮助我阅读我正在使用的string格式,并让我利用现有的字典。 例如: class MyClass: def __init__(self): self.title = 'Title' a = MyClass() print 'The title is %(title)s' % a.__dict__ path = '/path/to/a/file' print 'You put your file here: %(path)s' % locals() 但是,我不能找出python 3.x语法来做同样的事情(或者甚至可能)。 我想做以下事情 # Fails, KeyError 'latitude' geopoint = {'latitude':41.123,'longitude':71.091} print '{latitude} {longitude}'.format(geopoint) # Succeeds print '{latitude} {longitude}'.format(latitude=41.123,longitude=71.091)

将Python字典转换为列表

可能重复: 如何将Python字典转换为元组列表? 我试图将Python字典转换为Python列表,以执行一些计算。 #My dictionary dict = {} dict['Capital']="London" dict['Food']="Fish&Chips" dict['2012']="Olympics" #lists temp = [] dictList = [] #My attempt: for key, value in dict.iteritems(): aKey = key aValue = value temp.append(aKey) temp.append(aValue) dictList.append(temp) aKey = "" aValue = "" 这是我的尝试…但我不能解决什么是错的?