混合列表和Python中的Dict [英] Mixing List and Dict in Python
问题描述
$ options = array(
option1,
option2,
option3=>数组(meta1,meta2,meta3),
option4
)
问题是python对于不同的列表有不同的括号。 ()for tuple,[] for list,{} for dict。似乎没有什么办法混合使用,我不断收到语法错误。
我现在使用的是python 2.7。请指导如何正确执行。
非常感谢,
Rufas
更新1:
我会稍微详细说明我要做的事情。我想在这里写一个简单的python脚本来做一些API请求:
http://www.diffbot.com/products/automatic/article/
相关部分是字段查询参数。它是像...& fields = meta,querystring,images(url,caption)...等等。所以上面的数组可以写成(在PHP中)
$ fields = array(
'meta',
'querystring',
'images'=>数组('url','caption')
);
$字段将被传递给一个处理方法。结果将返回,如下所示:
$ json = diffbot-> get(article,$ url,$字段);
事情是 - 我在PHP中没有问题,但是当我尝试写在Python中,事情不像看起来那么容易...
我很确定有3个答案我的问题,当它收到一票,它是最接近我想要的。现在很奇怪,当我想选择一个接受答案时,它已经消失了:(
要概述,删除的答案表明我应该这样做:
options = [
option1,
option2,
{ option3:[meta1,meta2,meta3]},
option4
]
这样很适合我想要处理的列表中的每个项目,我只是循环遍历所有的值并检查它的类型,如果是一个字符串,处理它像正常,但是当它是一个dict / list,它会被不同的处理。
最终,我设法让它工作,我得到了我想要的。
特别感谢Igor Chubin和Hyperboreus为我提供建议和想法,以测试和发现我一直在寻找的答案。非常感谢。
谢谢!
Rufas
I was wondering how do you mix list and dict together on Python? I know on PHP, I can do something like this:
$options = array(
"option1",
"option2",
"option3" => array("meta1", "meta2", "meta3"),
"option4"
);
The problem is python have different bracket for different list. () for tuple, [] for list, and {} for dict. There don't seems to be any way to mix them and I'm keep getting syntax errors.
I am using python 2.7 now. Please advice how to do it correctly.
Much thanks,
Rufas
Update 1:
I'll slightly elaborate what I'm trying to do. I am trying to write a simple python script to do some API requests here:
http://www.diffbot.com/products/automatic/article/
The relevant part is the fields query parameters. It is something like ...&fields=meta,querystring,images(url,caption)... . So the above array can be written as (in PHP)
$fields = array(
'meta',
'querystring',
'images' => array('url', 'caption')
);
And the $fields will be passed to a method for processing. The result will be returned, like this:
$json = diffbot->get("article", $url, $fields);
The thing is - I have no problem in writing it in PHP, but when I try to write it in Python, the thing is not as easy as it seems...
I'm pretty sure there are 3 answers for my question and while it received a -1 vote, it is the closest to what I want. It is very strange now that it is gone when I want to pick that one up as "accepted answer" :(
To recap, the removed answer suggest I should do this:
options = [
"option1",
"option2",
{"option3":["meta1", "meta2", "meta3"]},
"option4"
]
And that fits nicely how I want to process each item on the list. I just loop through all values and check for its type. If it is a string, process it like normal. But when it is a dict/list, it will be handled differently.
Ultimately, I managed to make it work and I get what I want.
Special thanks to Igor Chubin and Hyperboreus for providing suggestions and ideas for me to test and discover the answer I've been looking for. Greatly appreciated.
Thank you!
Rufas
这篇关于混合列表和Python中的Dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!