Python中的结构 [英] structure in Python
问题描述
您好:
我有下一个结构:
[key1,value1,value2]
[''A '',1,5]
[''B'',6,7]
如何使用Python制作它?
如何更新6的值?
如何插入名为C的键?及其值?
如何删除名为B的键?
如何搜索名为A的键(参数)并获取其值?
问候
Dora and Boots说:说地图!说地图!"
stuff = {''A'':[1,5],'' B'':[6,7]}
东西[''B''] [0] + = 6
东西
{''A'':[1,5], ''B'':[12,7]}东西[''C''] = [33,44]
del stuff [''B'']
打印东西[" A" ]
[1,5]
- 马特
" Alberto Vera"写道:
我有下一个结构:
[key1,value1,value2]
如何使用Python制作它?
如何更新6的值?
如何插入名为C的密钥?及其值?
如何删除名为B的键?
如何搜索名为A的键(参数)并获取其值?
2003年10月20日星期一18:13:48 -0500,Alberto Vera< av *** @ coes.org.pe>写道:你好:
我有下一个结构:
[key1,value1,value2]
[''A'',1, 5]
[''B'',6,7]
如何使用Python制作它?
您可以使用长度为2的列表。
d = {
A: [1,5],
" B":[6,7],#last尾随逗号是可选的但是好的风格
}
如何更新6的值?
6将始终为6,数字是不可变的,你不能改变它。但你可以用键B改变列表的第一项。与
d [" B"] [0] = 2
现在
d [ B]
将返回[2,7]
如何插入名为C的密钥?和它的价值观?
d [" C"] = [8,9]
如何删除名为B的密钥?
del d [" B"]
如何搜索名为A(参数)的密钥并获取其值?
d [" A"]
您还可以使用多个赋值将变量绑定到值
(value1,value2)= d [" A"]
如果找不到密钥,则引发KeyError异常。如果您愿意,可以使用d.has_key(A)测试是否存在密钥。
。阅读
" Mapping types"在Python库参考中
http:// python .org / doc / lib / lib.html
描述了dict对象的方法。
-
Ben Caradoc-Davies< be*@wintersun.org>
http: //wintersun.org/
到达时的监禁是澳大利亚真正的移民经历。
2003年10月20日星期一18 :13:48-505,Alberto Vera, < av *** @ coes.org.pe>
写道:
你好:
我有下一个结构:
[key1,value1,value2]
[''A'',1,5]
[''B'',6,7]
我怎样才能使用Python?
使用带有列表中值的字典:listdict = {'' A':[1,5],''B'':[6,7]}
打印listdict
{''A'':[1,5],''B'' :[6,7]}
如何更新6的值?
print listdict [''B''] [0]
6 listdict [''B''] [0] = 28
print listdict [''B''] [0]
28
如何插入名为C的密钥?和它的价值观?
listdict [''C''] = [1,2,3]
print listdict
{''A'':[1,5],''C'':[ 1,2,3,''B'':[28,7]}
如何删除名为B的密钥?
del listdict [''B'']
print listdict
{''A'':[1,5],''C'':[1,2,3]}
如何搜索名为A(参数)的键并获取其值?
如果在listdict中''A'':
.... print listdict [''A'']
....
[1,5]
问候
尝试搜索Python文档或参考教程:
http://www.python.org/doc/current/tut/tut.html
-
克里斯托弗
Hello:
I have the next structure:
[key1,value1,value2]
[''A'',1,5]
[''B'',6,7]
How Can I make it using Python?
How Can I update the value of 6?
How Can I insert a key called "C" and its values?
How Can I delete a key called "B"?
How Can I search a key called "A"(parameter) and get its values?
Regards
Dora and Boots say: "Say map! Say map!"
stuff = { ''A'' : [1,5], ''B'' : [6,7] }
stuff[''B''][0] += 6
stuff {''A'': [1, 5], ''B'': [12, 7]} stuff[''C''] = [33,44]
del stuff[''B'']
print stuff["A"]
[1, 5]
- Matt
"Alberto Vera" wrote:
I have the next structure:
[key1,value1,value2]
How Can I make it using Python?
How Can I update the value of 6?
How Can I insert a key called "C" and its values?
How Can I delete a key called "B"?
How Can I search a key called "A"(parameter) and get its values?
On Mon, 20 Oct 2003 18:13:48 -0500, Alberto Vera <av***@coes.org.pe> wrote:Hello:
I have the next structure:
[key1,value1,value2]
[''A'',1,5]
[''B'',6,7]
How Can I make it using Python?
You could use a dict of lists of length 2.
d = {
"A": [1, 5],
"B": [6, 7], # last trailing comma is optional but good style
}
How Can I update the value of 6?
6 will always be 6, numbers are immutable, and you can''t change it. But you can
change the first item of the list with key "B" with
d["B"][0] = 2
now
d["B"]
will return [2, 7]
How Can I insert a key called "C" and its values?
d["C"] = [8, 9]
How Can I delete a key called "B"?
del d["B"]
How Can I search a key called "A"(parameter) and get its values?
d["A"]
You could also use multiple assignment to bind variables to the values with
(value1, value2) = d["A"]
If a key is not found, a KeyError exception is raised. If you prefer, you can
test for the existence of the key with d.has_key("A") . Read the section on
"Mapping types" in the Python Library Reference
http://python.org/doc/lib/lib.html
which describes the methods of dict objects.
--
Ben Caradoc-Davies <be*@wintersun.org>
http://wintersun.org/
Imprisonment on arrival is the authentic Australian immigration experience.
On Mon, 20 Oct 2003 18:13:48 -0500, "Alberto Vera" <av***@coes.org.pe>
wrote:
Hello:
I have the next structure:
[key1,value1,value2]
[''A'',1,5]
[''B'',6,7]
How Can I make it using Python?
Use a dictionary with the values in lists:listdict = {''A'': [1, 5], ''B'': [6, 7]}
print listdict {''A'': [1, 5], ''B'': [6, 7]}
How Can I update the value of 6? print listdict[''B''][0] 6 listdict[''B''][0] = 28
print listdict[''B''][0] 28
How Can I insert a key called "C" and its values? listdict[''C''] = [1, 2, 3]
print listdict {''A'': [1, 5], ''C'': [1, 2, 3], ''B'': [28, 7]}
How Can I delete a key called "B"? del listdict[''B'']
print listdict {''A'': [1, 5], ''C'': [1, 2, 3]}
How Can I search a key called "A"(parameter) and get its values? if ''A'' in listdict:
.... print listdict[''A'']
....
[1, 5]
Regards
Try searching the Python documentation or take the Tutorial:
http://www.python.org/doc/current/tut/tut.html
--
Christopher
这篇关于Python中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!