基于列表值的第一个索引将列表转换为Python中的字典

时间:2016-01-22 10:19:37

标签: python dictionary

我有一个如下所示的列表:

scope.$watch(attr.ngModel, function(newValue) {
  if (minVal) {
    valid = !isValidDate(newValue) || angular.isUndefined(minVal) || parseDate(newValue) >= minVal;
    if (!valid) {
      // If date is less than min - set it to min
      // scope.example.value = minVal;
      ctrl.$setViewValue(attr.minDate);
      ctrl.$render();
    }
  }
  if (maxVal) {
    valid = !isValidDate(newValue) || angular.isUndefined(maxVal) || parseDate(newValue) <= maxVal;
    if (!valid) {
      // If date is greater than max - set it to max
      // scope.example.value = maxVal;
      ctrl.$setViewValue(attr.maxDate);
      ctrl.$render();
    }
  }
});

需要以下列格式输出字典

['C01234' , 'pass' , 'pass' , 'C2456' , 'fail' , 'pass' , 'C53675' , 'pass' , 'pass']

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:1)

你的输出应该是一个列表字典(或者如果愿意的话是元组),即:

{'C01234': ['pass' , 'pass'],
 'C2456' : ['fail , 'pass'],
 'C53675': ['pass' , 'pass']}

defaultdict列表是产生这种情况的一种方法:

from collections import defaultdict

l = ['C01234', 'pass', 'pass', 'C2456', 'fail', 'pass', 'C53675', 'pass', 'pass']

values = {'pass', 'fail'}    # add any others to this set
d = defaultdict(list)
key = None
for s in l:
    if s not in values:
        key = s
    else:
        d[key].append(s)

>>> print(d)
defaultdict(<type 'list'>, {'C53675': ['pass', 'pass'], 'C2456': ['fail', 'pass'], 'C01234': ['pass', 'pass']})
>>> print(dict(d))    # convert to a standard dictionary
{'C53675': ['pass', 'pass'], 'C2456': ['fail', 'pass'], 'C01234': ['pass', 'pass']}

这假设值只能是passfail中的一个。如果需要,您可以通过将其添加到values集来添加其他值。

如果每个键总有2个值,那么另一种方法是使用zip()iter()的词典理解:

l = ['C01234', 'pass', 'pass', 'C2456', 'fail', 'pass', 'C53675', 'pass', 'pass']
it = iter(l)
d = {t[0]:t[1:] for t in zip(it, it, it)}

>>> print(d)
{'C53675': ('pass', 'pass'), 'C2456': ('fail', 'pass'), 'C01234': ('pass', 'pass')}

这可以推广到每个键的 n 值:

l = ['C01234', 'pass', 'pass', 'fail', 'C2456', 'fail', 'fail', 'pass', 'C53675', 'pass', 'pass', 'pass']
n = 3
d = {t[0]:t[1:] for t in zip(*[iter(l)]*(n+1))}

>>> print(d)
{'C53675': ('pass', 'pass', 'pass'), 'C2456': ('fail', 'fail', 'pass'), 'C01234': ('pass', 'pass', 'fail')}

答案 1 :(得分:0)

这样的事情,也许是:

data = ['C01234' , 'pass' , 'pass' , 'C2456' , 'fail' , 'pass' , 'C53675' , 'pass' , 'pass']
od = {} # Output dictionary.
key = None
for x in data:
  if x.startswith("C"):
    key = x
    od[key] = []
  elif key is not None:
    od[key].append(x)

这会产生:

{'C53675': ['pass', 'pass'], 'C2456': ['fail', 'pass'], 'C01234': ['pass', 'pass']}

答案 2 :(得分:0)

您可以使用切片运算符(start:end:step),步长为3以拆分列表,例如:

l = ['C01234' , 'pass' , 'pass' , 'C2456' , 'fail' , 'pass' , 'C53675' , 'pass' , 'pass']

d = dict()

for key, val1, val2 in zip(l[::3], l[1::3], l[2::3]):
    d[key] = val1, val2

答案 3 :(得分:0)

如果每个条目都有与第一个代码块完全相同的两个测试,请用三步zip()将其切片三次,并在字典理解中使用它:

d = {k:[v1, v2] for k, v1, v2 in zip(sample[::3], sample[1::3], sample[2::3])}

或者,如果您不知道每个条目将有多少测试,请使用普通循环:

d = {}
results = {'pass', 'fail'}
for item in sample:
    if item not in results:
        d[item] = []
        current = item
    else:
        d[current].append(item)