有人可以向我解释一下吗?这对我没有任何意义.
我将字典复制到另一个字典中并编辑第二个字典,然后更改两个字典 为什么会这样?
>>> dict1 = {"key1": "value1", "key2": "value2"}
>>> dict2 = dict1
>>> dict2
{'key2': 'value2', 'key1': 'value1'}
>>> dict2["key2"] = "WHY?!"
>>> dict1
{'key2': 'WHY?!', 'key1': 'value1'}
Run Code Online (Sandbox Code Playgroud) 如何将a的str
表示形式(dict
如下面的字符串)转换为dict
?
s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
Run Code Online (Sandbox Code Playgroud)
我不喜欢用eval
.我还能用什么?
这样做的主要原因是我写的一个同事课程,将所有输入转换为字符串.我没心情去修改他的课程来处理这个问题.
dict.items()
和之间是否有任何适用的差异dict.iteritems()
?
从Python文档:
dict.items()
:返回字典的(键,值)对列表的副本.
dict.iteritems()
:在字典(键,值)对上返回一个迭代器.
如果我运行下面的代码,每个似乎都返回对同一对象的引用.我缺少哪些微妙的差异?
#!/usr/bin/python
d={1:'one',2:'two',3:'three'}
print 'd.items():'
for k,v in d.items():
if d[k] is v: print '\tthey are the same object'
else: print '\tthey are different'
print 'd.iteritems():'
for k,v in d.iteritems():
if d[k] is v: print '\tthey are the same object'
else: print '\tthey are different'
Run Code Online (Sandbox Code Playgroud)
输出:
d.items():
they are the same object
they are the same object
they are the same object
d.iteritems():
they are the same …
Run Code Online (Sandbox Code Playgroud) 我知道我可以迭代一张地图m
,
for k, v := range m { ... }
Run Code Online (Sandbox Code Playgroud)
并寻找一个键,但有一种更有效的方法来测试一个键在地图中的存在吗?
我在语言规范中找不到答案.
在Python 2.7中,我可以将字典键,值或项目作为列表获取:
>>> newdict = {1:0, 2:0, 3:0}
>>> newdict.keys()
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
现在,在Python> = 3.3中,我得到这样的东西:
>>> newdict.keys()
dict_keys([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
所以,我必须这样做以获得一个列表:
newlist = list()
for i in newdict.keys():
newlist.append(i)
Run Code Online (Sandbox Code Playgroud)
我想知道,有没有更好的方法来返回Python 3中的列表?
给出这样的字典:
my_map = {'a': 1, 'b': 2}
Run Code Online (Sandbox Code Playgroud)
如何反转此地图以获得:
inv_map = {1: 'a', 2: 'b'}
Run Code Online (Sandbox Code Playgroud)
编者注: __CODE__
改为__CODE__
避免与内置函数冲突,__CODE__
.下面有些评论可能会受到影响.
我有一个Map,它将被多个线程同时修改.
Java API中似乎有三种不同的同步Map实现:
Hashtable
Collections.synchronizedMap(Map)
ConcurrentHashMap
据我所知,这Hashtable
是一个旧的实现(扩展过时的Dictionary
类),后来经过调整以适应Map
界面.虽然它是同步的,但似乎存在严重的可扩展性问题,并且不鼓励新项目.
但是其他两个怎么样?返回的地图Collections.synchronizedMap(Map)
和ConcurrentHashMap
s 之间有什么区别?哪一种适合哪种情况?
我创建了一个函数,它将在a中查找年龄Dictionary
并显示匹配的名称:
dictionary = {'george' : 16, 'amber' : 19}
search_age = raw_input("Provide age")
for age in dictionary.values():
if age == search_age:
name = dictionary[age]
print name
Run Code Online (Sandbox Code Playgroud)
我知道如何比较和找到我只是不知道如何显示这个人的名字的年龄.另外,KeyError
由于第5行,我得到了一个.我知道这不正确,但我无法弄清楚如何让它向后搜索.
今天,我遇到了这个dict
方法get
,它给出了字典中的一个键,返回了相关的值.
这个功能用于什么目的?如果我想找到与字典中的键相关联的值,我可以这样做dict[key]
,它返回相同的东西:
dictionary = {"Name": "Harry", "Age": 17}
dictionary["Name"]
dictionary.get("Name")
Run Code Online (Sandbox Code Playgroud) 我有一个这样的词典列表:
[{'points': 50, 'time': '5:00', 'year': 2010},
{'points': 25, 'time': '6:00', 'month': "february"},
{'points':90, 'time': '9:00', 'month': 'january'},
{'points_h1':20, 'month': 'june'}]
Run Code Online (Sandbox Code Playgroud)
我想把它变成DataFrame
像这样的熊猫:
month points points_h1 time year
0 NaN 50 NaN 5:00 2010
1 february 25 NaN 6:00 NaN
2 january 90 NaN 9:00 NaN
3 june NaN 20 NaN NaN
Run Code Online (Sandbox Code Playgroud)
注意:列的顺序无关紧要.
如何将字典列表转换为pandas DataFrame,如上所示?
dictionary ×10
python ×8
python-2.x ×2
python-3.x ×2
concurrency ×1
dataframe ×1
go ×1
go-map ×1
java ×1
list ×1
mapping ×1
pandas ×1
reference ×1
reverse ×1
string ×1