带有索引到字典的值列表

时间:2015-08-21 21:48:30

标签: python list dictionary

我想编写一个将索引和列表值转换为字典的函数,例如:(代码在python下面)。

(a, b, c, d) = range(4)
l = [5, 6, 7, 8]
dic = {}
def fun(index_list, l):
    # do something
dic = fun ([a, b, d], l)

>>> print dic
>>> {'0':5, '1':6, '3':8}

`>>> {'a':5, 'b':6, 'c':8}`

有什么想法吗?

第二个结果是可能的吗?

2 个答案:

答案 0 :(得分:0)

你不能做dplyr,因为你不应该以任何代码逻辑所必需的方式将变量名称绑定到它们的值。有令人难以置信的丑陋和脆弱的黑客可能会让你在那里,但让我们把这些工具留在棚子里。

要获取值的值,library(dplyr) ID <- c("6820","6820","17413","17413","38553","38553","52760","52760","717841","717841","717841","747187","747187","747187") date <- c("2014-06-12","2015-06-11","2014-05-01","2014-05-01","2014-06-12","2015-06-11","2014-10-24","2014-10-24","2014-05-01","2014-05-01","2014-12-02","2014-03-01","2014-05-12","2014-05-12") type <- c("ST","ST","MC","MC","LC","LC","YA","YA","YA","YA","MC","LC","LC","MC") level <-c("firsttime","new","new","active","active","active","firsttime","new","active","new","active","new","active","active") type <- factor(type, levels=c("LC", "MC", "YA", "ST")) level <- factor(level, levels=c("active", "new", "firsttime")) data <- data.frame(ID,date,type,level) df.right <- data %>% group_by(ID, date) %>% filter(type == sort(type)[1]) %>% filter(level == sort(level)[1]) AdendaAgent.addCustomFragmentContent (Context context, String actionToPerformOnUnlock, String your.fragment.name, Bundle fragmentArgs, String identifier, boolean bDisplayUntilCancelled) 很容易。

let array = [outLet0, outlet1, outlet2, outLet3, outLet4, outLet5]

for outlet in array {
  outlet.hidden = true
}

虽然现在我看到你期望outlet0.hidden = true outlet1.hidden = true outlet2.hidden = true outlet3.hidden = true outlet4.hidden = true outlet5.hidden = true 映射到{'a':5, 'b':6, 'c':8}。你可以这样做,但这是一个额外的步骤。

zip

答案 1 :(得分:0)

这很简单,但只需使用字典理解:

dic = {str(v): l[v] for v in (a, b, d)}