Python - 元组列表,我应该使用什么条件才能使其正常工作
Python - list of tuples, what conditions should I use to make it work as excepted
我有一个元组列表:
lst = [
("Tom", "Failed"),
("Tom", "Success"),
("Kas", "Success"),
("Kas", "Success"),
("Al", "Warning"),
("Al", "Success",
("Pel", "Failed"),
("Pel", "Failed")
]
我不想重复所以我使用 set():
set_lst = set(lst)
我得到:
{
('Al', 'Success'),
('Al', 'Warning'),
('Kas', 'Success'),
('Tom', 'Failed'),
('Tom', 'Success'),
('Pel', 'Failed')
}
我完全搞砸了条件并放弃了:
for x, y in set_lst:
if len({g for _, g in set_lst}) > 1:
y = "Success"
print(x, y)
elif len({g for _, g in set_lst}) <= 1:
if y == "Failed":
print(x, y)
我不知道在实际代码中应该如何处理。
我将警告和成功(如果是同一个人)视为成功;失败和成功(如果是同一个人)也算成功;失败为失败。
如果我有:
('Tom', 'Failed') 和 ('Tom', 'Success') -> 汤姆成功
('Al', 'Success') 和 ('Al', 'Warning') -> Al 成功
我除了:
Al Success
Kas Success
Tom Success
Pel Failed
元组列表可能会更改。
尝试:
lst = [
("Tom", "Failed"),
("Tom", "Success"),
("Kas", "Success"),
("Kas", "Success"),
("Al", "Warning"),
("Al", "Success"),
("Pel", "Failed"),
("Pel", "Failed"),
]
out = {}
for name, status in lst:
if name not in out or out[name] != "Success":
out[name] = status
out = list(out.items())
print(out)
打印:
[('Tom', 'Success'), ('Kas', 'Success'), ('Al', 'Success'), ('Pel', 'Failed')]
我有一个元组列表:
lst = [
("Tom", "Failed"),
("Tom", "Success"),
("Kas", "Success"),
("Kas", "Success"),
("Al", "Warning"),
("Al", "Success",
("Pel", "Failed"),
("Pel", "Failed")
]
我不想重复所以我使用 set():
set_lst = set(lst)
我得到:
{
('Al', 'Success'),
('Al', 'Warning'),
('Kas', 'Success'),
('Tom', 'Failed'),
('Tom', 'Success'),
('Pel', 'Failed')
}
我完全搞砸了条件并放弃了:
for x, y in set_lst:
if len({g for _, g in set_lst}) > 1:
y = "Success"
print(x, y)
elif len({g for _, g in set_lst}) <= 1:
if y == "Failed":
print(x, y)
我不知道在实际代码中应该如何处理。
我将警告和成功(如果是同一个人)视为成功;失败和成功(如果是同一个人)也算成功;失败为失败。
如果我有:
('Tom', 'Failed') 和 ('Tom', 'Success') -> 汤姆成功
('Al', 'Success') 和 ('Al', 'Warning') -> Al 成功
我除了:
Al Success
Kas Success
Tom Success
Pel Failed
元组列表可能会更改。
尝试:
lst = [
("Tom", "Failed"),
("Tom", "Success"),
("Kas", "Success"),
("Kas", "Success"),
("Al", "Warning"),
("Al", "Success"),
("Pel", "Failed"),
("Pel", "Failed"),
]
out = {}
for name, status in lst:
if name not in out or out[name] != "Success":
out[name] = status
out = list(out.items())
print(out)
打印:
[('Tom', 'Success'), ('Kas', 'Success'), ('Al', 'Success'), ('Pel', 'Failed')]