简体   繁体   English

从字典中检索列表时如何创建列表

[英]how do i create a list when retrieving a list from a dictionary

I am looking up a set of email addresses grouped together in a dictionary using the get() function我正在使用 get() function 在字典中查找一组 email 地址

the get function returns得到 function 返回

['[“one.yahoo.com”,”two.yahoo.com”,"three.yahoo.com"]']

I have tried several methods to remove the single quotes so that I end up with a list like this我尝试了几种方法来删除单引号,以便最终得到这样的列表

email = [ [“one.yahoo.com”,”two.yahoo.com”,"three.yahoo.com"] ]

the dictionary looks like this字典看起来像这样

dict= { 'Warehouse':'[“x@yahoo.com”,”y@yahoo.com”,”z@yahoo.com”]', 'Bottling':'[“one.yahoo.com”,”two.yahoo.com”,"three.yahoo.com"]'} dict= { '仓库':'[“x@yahoo.com”,”y@yahoo.com”,”z@yahoo.com”]', '装瓶':'[“one.yahoo.com”,”两个.yahoo.com”,"三个.yahoo.com"]'}

Try this:尝试这个:

import ast # This contains a function to convert str to list
email = ['["one.yahoo.com", "two.yahoo.com", "three.yahoo.com"]']
email = str(email) # Converts to str type.
email = email.replace("'", "") # Replaces the apostrophes with blanks.
email = ast.literal_eval(email) # Converts to list. 
print(email)

Output is: Output 是:

[["one.yahoo.com", "two.yahoo.com", "three.yahoo.com"]]

You can try this:你可以试试这个:

email = ['["one.yahoo.com","two.yahoo.com","three.yahoo.com"]']

email = email[0].replace('"','')
email = email[1:-1].split(",")

Hope this may be of help.希望这可能会有所帮助。

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

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