英文:
How to find a number +1 to the largest in a list OR fill any numerical gaps
问题
I am interacting with an API. I am using a GET (requests) to get the data I need. The response looks like this:
[{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST1',
'id': 1,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 1,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'},
{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST2',
'id': 2,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 22,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'}]
I am then parsing out just the id. Using this:
param_ids = [element['id'] for element in param_current_ids_dict]
So I'm left with:
[1, 2]
My question is this: How can I check for the next largest number to use? Ideally, I'd want to send a POST to the API with the id of 3, as my next action, since that is the next available digit after 2. Also it would be nice if it 'filled' any gaps. Say the response was:
[1, 3]
'2' would be fine in this instance.
英文:
I am interacting with an API. I am using a GET (requests) to get the data I need. The response looks like this:
[{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST1',
'id': 1,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 1,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'},
{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST2',
'id': 2,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 22,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'}]
I am then parsing out just the id. Using this:
param_ids = [element['id'] for element in param_current_ids_dict]
So I'm left with:
[1, 2]
My question is this: How can I check for the next largest number to use? Ideally, I'd want to send a POST to the API with the id of 3, as my next action, since that is the next available digit after 2. Also it would be nice if it 'filled' any gaps. Say the response was:
[1, 3]
'2' would be fine in this instance.
答案1
得分: 1
为了找到列表中尚未使用的下一个最大数字,您可以对id列表进行排序,然后遍历它们,比较每个元素与其索引加1(因为您想要下一个数字)。当找到一个元素大于其索引加1时,将该索引加1作为下一个可用的id返回。
param_current_ids_dict = [{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST1',
'id': 1,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 1,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'},
{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST2',
'id': 3,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 22,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'}]
param_ids = [element['id'] for element in param_current_ids_dict]
def next_available_id(ids):
sorted_ids = sorted(ids)
for i, id in enumerate(sorted_ids):
if id > i + 1:
return i + 1
return len(ids) + 1
next_id = next_available_id(param_ids)
print(next_id) # 在这种情况下应该打印2
希望这对您有帮助。
英文:
To find the next largest number that is not already in the list, you can sort the list of ids and iterate over them, comparing each element to its index plus 1 (since you want the next number). When you find an element that is greater than its index plus 1, return that index plus 1 as the next available id.
param_current_ids_dict = [{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST1',
'id': 1,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 1,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'},
{'AG': '**********************************',
'AB': '**********************************',
'AC': '**********************************',
'AD': '**********************************',
'AE': '**********************************',
'description': 'TEST2',
'id': 3,
'code': '**********************************',
'definer': '**********************************',
'paraJ': 22,
'G1': '****',
'B2': '****',
'A3': '****',
'F4': '****',
'C5': '****'}]
param_ids = [element['id'] for element in param_current_ids_dict]
def next_available_id(ids):
sorted_ids = sorted(ids)
for i, id in enumerate(sorted_ids):
if id > i + 1:
return i + 1
return len(ids) + 1
next_id = next_available_id(param_ids)
print(next_id) # should print 2 in this case
答案2
得分: 0
简单而有效的Python脚本:
input_list = [1, 3]
def check_id(input_list, fill_all_gaps=False):
available_numbers = [i+1 for i in range(100)]
for nr in input_list:
available_numbers.remove(nr)
if not fill_all_gaps:
return available_numbers[0]
else:
return available_numbers[:max(input_list) - len(input_list)]
如果您只想要下一个可接受的ID,您可以使用:
print(check_id(input_list))
2 # 作为整数的数字
如果您想要所有可用的ID:
input_list = [2, 5]
print(check_id(input_list, fill_all_gaps=True))
[1, 3, 4] # 作为列表的数字
英文:
Easy and effective python script:
input_list = [1, 3]
def check_id(input_list, fill_all_gaps=False):
available_numbers = [i+1 for i in range(100)]
for nr in input_list:
available_numbers.remove(nr)
if not fill_all_gaps:
return available_numbers[0]
else:
return available_numbers[:max(input_list) - len(input_list)]
If you want just the next acceptable id you can use:
print(check_id(input_list))
2 # number as integer
If you want all available id's:
input_list = [2, 5]
print(check_id(input_list, fill_all_gaps=True))
[1, 3, 4] # number/s as list
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论