我如何查看元组列表是否有重复的字符串,然后 return 具有重复项的较高 int 值的元组
How can i look if a list of tuples has a duplicate string, and then return the tuple with the higher int value of the duplicates
这是我试过的代码:
def generate_combined_list(inputs: list):
data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 'float': 1.5, 'string': 'poodle'}
new_list = []
for i in inputs:
var = i
if var[1] in data_types:
for i in range(var[0]):
new_list.append(data_types[tup[1]])
return new_list
预期输出:
print(generate_combined_list([(3, 'int'), (5, 'int')]))
# expected output [1, 1, 1, 1, 1]
print(generate_combined_list([(3, 'int'), (5, 'list'), (4, 'int')]))
# expected output [[], [], [], [], [], 1, 1, 1, 1]
使用字典保存找到的所有字符串,如果当前值较大,则将其替换为当前值。
from collections import defaultdict
from copy import copy
def generate_combined_list(inputs: list):
data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 'float': 1.5, 'string': 'poodle'}
new_list = []
counts = defaultdict(lambda: 0)
for dt, count in inputs:
if dt in data_types:
counts[dt] = max(counts[dt], count)
for dt, count in counts:
new_list.extend([copy(data_types[dt]) for _ in count]
return new_list
我使用 copy(data_types[dt])
这样结果就不会包含对同一个列表、集合、字典等的多个引用
与 Barmars 解决方案类似的想法。
按类型、数量将输入排序到字典中 - 因为类型是重复的,所以只保留最高的。使用该字典的值来过滤传入列表,您就完成了:
def generate_combined_list(inputs: list):
data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1,
'float': 1.5, 'string': 'poodle'}
new_list = []
# sort by (type,value) ascending and create dict
# only largest of each type survives
keep = {v:(k,v) for (k,v) in sorted(inputs, key = lambda x : (x[1], x[0])) }
# just remember the values
# print(keep) # remove comment for some insight
keep = keep.values()
# iterate the kept values
for tup in keep:
if tup[1] in data_types:
for i in range(tup[0]):
new_list.append(data_types[tup[1]])
return new_list
print(generate_combined_list([(3, 'int'), (5, 'list'), (4, 'int')]))
输出:
[[], [], [], [], [], 1, 1, 1, 1]
这将遵循从原始列表中取出事物的顺序。
这是我试过的代码:
def generate_combined_list(inputs: list):
data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 'float': 1.5, 'string': 'poodle'}
new_list = []
for i in inputs:
var = i
if var[1] in data_types:
for i in range(var[0]):
new_list.append(data_types[tup[1]])
return new_list
预期输出:
print(generate_combined_list([(3, 'int'), (5, 'int')]))
# expected output [1, 1, 1, 1, 1]
print(generate_combined_list([(3, 'int'), (5, 'list'), (4, 'int')]))
# expected output [[], [], [], [], [], 1, 1, 1, 1]
使用字典保存找到的所有字符串,如果当前值较大,则将其替换为当前值。
from collections import defaultdict
from copy import copy
def generate_combined_list(inputs: list):
data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 'float': 1.5, 'string': 'poodle'}
new_list = []
counts = defaultdict(lambda: 0)
for dt, count in inputs:
if dt in data_types:
counts[dt] = max(counts[dt], count)
for dt, count in counts:
new_list.extend([copy(data_types[dt]) for _ in count]
return new_list
我使用 copy(data_types[dt])
这样结果就不会包含对同一个列表、集合、字典等的多个引用
与 Barmars 解决方案类似的想法。
按类型、数量将输入排序到字典中 - 因为类型是重复的,所以只保留最高的。使用该字典的值来过滤传入列表,您就完成了:
def generate_combined_list(inputs: list):
data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1,
'float': 1.5, 'string': 'poodle'}
new_list = []
# sort by (type,value) ascending and create dict
# only largest of each type survives
keep = {v:(k,v) for (k,v) in sorted(inputs, key = lambda x : (x[1], x[0])) }
# just remember the values
# print(keep) # remove comment for some insight
keep = keep.values()
# iterate the kept values
for tup in keep:
if tup[1] in data_types:
for i in range(tup[0]):
new_list.append(data_types[tup[1]])
return new_list
print(generate_combined_list([(3, 'int'), (5, 'list'), (4, 'int')]))
输出:
[[], [], [], [], [], 1, 1, 1, 1]
这将遵循从原始列表中取出事物的顺序。