Python:遍历list的list

Python: iterate over a list of list

我有一个列表列表,如下所示,其中动物名称作为键,ID、状态作为值。一只动物可以有多个值,每个 ID 重复多次,状态为 pass/fail。许多动物都有许多这样的记录行。我正在尝试遍历 python 中的每一行并想要计算 - 对于动物来说所有的 ID 都被传递了。如果 ID 中没有 Failed 状态,则该 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'))].......................

尝试:

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']}

将成功存储在 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

首先,为了遍历您的列表并将所有项目作为唯一变量名称保存在一个很好的 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']}