Python:遍历列表列表

时间:2015-08-22 00:29:16

标签: python list iteration

我有一个列表列表,如下所示,动物名称为密钥,ID为“状态”。一只动物可以具有多个值,并且每个ID以通过/未通过状态重复多次。对于许多动物来说,有很多这样的记录。我试图迭代python中的每一行,并想要计算 - 对于动物,所有ID都通过。如果动物身份中没有失败状态,则该身份证将被视为通过。 逻辑上我理解它将使用两个for循环但不能通过python代码。谢谢你的帮助。

[(u'Tiger', (u'PRO-16', u'Passed')),
 (u'Tiger', (u'PRO-16', u'Failed')),
 (u'Tiger', (u'PRO-17', u'Failed')),
 (u'Tiger', (u'PRO-17', u'Passed')),
 (u'Monkey', (u'PRO-18', u'Passed')),
 (u'Monkey', (u'PRO-18', u'Failed'))
 (u'Monkey', (u'PRO-19', u'Passed')),
 (u'Monkey', (u'PRO-20', u'Failed')),
 (u'Elephant', (u'PRO-21', u'No Run')),
 (u'Elephant', (u'GR-01', u'Passed'))].......................

3 个答案:

答案 0 :(得分:0)

尝试:

ids = {}
for line in animalinfo:
   if line[1][1]!="Failed":
       if line[0] in ids:
          ids[line[0]].append(line[1][0])
       else:
           ids[line[0]]= [line[1][0]]

它会给你:

{u'Tiger': [u'PRO-16', u'PRO-17'], u'Monkey': [u'PRO-18', u'PRO-19'], u'Elephant': [u'PRO-21', u'GR-01']}

答案 1 :(得分:0)

将成功结果存储在set中,将失败存储在另一个set中。浏览数据并根据其是否包含失败将每个条目放入正确的set。最后,从成功中减去失败,并且你留下了没有失败匹配的所有成功(我直接打印了这个结果)。

data = [(u'Tiger', (u'PRO-16', u'Passed')),
 (u'Tiger', (u'PRO-16', u'Failed')),
 (u'Tiger', (u'PRO-17', u'Failed')),
 (u'Tiger', (u'PRO-17', u'Passed')),
 (u'Monkey', (u'PRO-18', u'Passed')),
 (u'Monkey', (u'PRO-18', u'Failed')),
 (u'Monkey', (u'PRO-19', u'Passed')),
 (u'Monkey', (u'PRO-20', u'Failed')),
 (u'Elephant', (u'PRO-21', u'No Run')),
 (u'Elephant', (u'GR-01', u'Passed'))]

succeeded = set()
failed = set()
for item in data:
    if item[1][1] != 'Failed':
        succeeded.add((item[0], item[1][0]))
    else:
        failed.add((item[0], item[1][0]))

结果:

>>> print(*(succeeded-failed), sep='\n')
('Elephant', 'GR-01')
('Monkey', 'PRO-19')
('Elephant', 'PRO-21')

您也可以使用defaultdict

对动物进行分组
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for k,v in succeeded-failed:
...     d[k].append(v)
...
>>> for k in d:
...     print(k + ': ' + ', '.join(d[k]))
...
Monkey: PRO-19
Elephant: PRO-21, GR-01

答案 2 :(得分:0)

首先,为了遍历您的列表并将所有项目作为唯一变量名称保存在一个不错的for循环中:

data = *your list above*
for animal, (test_id, status) in data:
    *CODE HERE*

在仅使用本机python数据结构的情况下解决特定问题的一种非常快速和肮脏的方法

results = {}
bad = {}
for animal, (test_id, status) in data:
    # Add the animal to the dict if it does not exist yet
    if animal not in results:
        results[animal] = []

    # if current test failed
    if (status == 'Failed'):

        # Remove test if we marked it as passed before
        if (test_id in results[animal]):
            results[animal].remove(test_id)

        # Ad the test to the 'bad' list
        if animal not in bad:
            bad[animal] = []
        bad[animal].append(test_id)

    # if the animal is not on the bad list at all add it to the good list
    elif animal not in bad and test_id not in results[animal]:
        results[animal].append(test_id)

    # if the current test is not in the animals bad list, add it to the good list
    elif test_id not in bad[animal] and test_id not in results[animal]:
        results[animal].append(test_id)

print('Tests that passed: {}'.format(results))
print('bad: {}'.format(bad))

此代码包含您的数据输出:

Tests that passed: {u'Tiger': [], u'Monkey': [u'PRO-19'], u'Elephant': [u'PRO-21', u'GR-01']}
bad: {u'Tiger': [u'PRO-16', u'PRO-17'], u'Monkey': [u'PRO-18', u'PRO-20']}