您如何将长度较短的列表平均分配给另一个列表?
How do you evenly distribute list with less length to another list?
我有两个列表。第一个名为 location,它的长度可以从 1 到无穷大不等。
location = ['loc1,'loc2','loc3','loc4,'loc5','loc6','loc7,'loc8','loc9','loc10,'loc11','loc12','loc13,'loc14','loc15','loc16,'loc17','loc18','loc19,'loc20','loc21','loc22,'loc23','loc24','loc25','loc26','loc27',] <- Please note that length could from 1 to infinite number
第二个列表名为 auditor。它的长度通常大于 location 列表长度。如果连第一个或最后一个审计员都没有分配到其他位置,我想将所有审计员平均分配到这些位置。
auditor = ['aone','atwo','athree','afour','afive','asix','aseven','aeight','anine','aten','aeleven','atwelve','athirteen','afourteen','afitheen','asixteen,'aseventeen''] <- Please note that length could from 1 to infinite number
下面的代码在大多数情况下工作得很好,但当位置为 28 且审计员为 17 时它会失败
df2['location'] = location
df2['auditor'] = [auditor[int(df2)] for df2 in np.arange(0, len(auditor), (len(auditor)/len(df2)))]
我想要的输出是得到最可能的均匀分割列表,它必须在任何情况下都有效,只要位置大于审计员。
My_desired_output =
'aone',
'aone'
'atwo',
'atwo',
'athree',
'athree',
'afour',
'afour',
'afive',
'afive',
'asix',
'asix',
'aseven',
'aseven',
'aeight',
'aeight',
'anine',
'anine',
'aten',
'aten',
'aeleven',
'aeleven',
'atwelve',
'athirteen',
'afourteen',
'afifteen',
'asixteen',
'aseventeen']
您可以考虑使用 more-itertool
中的 chunked
函数:
from more_itertools import chunked
from math import ceil
from typing import List, Any, Tuple, Iterator
def distribute_evenly(items: List[Any], cells: List[Any]) -> Iterator[Tuple[Any, List[Any]]]:
if len(items) <= len(cells):
item_chunks = [[item] for item in items] + [[]] * (len(cells) - len(items))
else:
chunk_size = int(ceil(len(items) / len(cells)))
item_chunks = chunked(auditor, chunk_size)
return zip(cells, item_chunks)
location = ["loc1", "loc2", "loc3"]
auditor = ["aud1", "aud2", "aud3", "aud4", "aud5", "aud6", "aud7", "aud8"]
auditor_per_location = list(distribute_evenly(auditor, location))
# auditor_per_location is now [('loc1', ['aud1', 'aud2', 'aud3']), ('loc2', ['aud4', 'aud5', 'aud6']), ('loc3', ['aud7', 'aud8'])]
祝你好运!
我有两个列表。第一个名为 location,它的长度可以从 1 到无穷大不等。
location = ['loc1,'loc2','loc3','loc4,'loc5','loc6','loc7,'loc8','loc9','loc10,'loc11','loc12','loc13,'loc14','loc15','loc16,'loc17','loc18','loc19,'loc20','loc21','loc22,'loc23','loc24','loc25','loc26','loc27',] <- Please note that length could from 1 to infinite number
第二个列表名为 auditor。它的长度通常大于 location 列表长度。如果连第一个或最后一个审计员都没有分配到其他位置,我想将所有审计员平均分配到这些位置。
auditor = ['aone','atwo','athree','afour','afive','asix','aseven','aeight','anine','aten','aeleven','atwelve','athirteen','afourteen','afitheen','asixteen,'aseventeen''] <- Please note that length could from 1 to infinite number
下面的代码在大多数情况下工作得很好,但当位置为 28 且审计员为 17 时它会失败
df2['location'] = location
df2['auditor'] = [auditor[int(df2)] for df2 in np.arange(0, len(auditor), (len(auditor)/len(df2)))]
我想要的输出是得到最可能的均匀分割列表,它必须在任何情况下都有效,只要位置大于审计员。
My_desired_output = 'aone', 'aone' 'atwo', 'atwo', 'athree', 'athree', 'afour', 'afour', 'afive', 'afive', 'asix', 'asix', 'aseven', 'aseven', 'aeight', 'aeight', 'anine', 'anine', 'aten', 'aten', 'aeleven', 'aeleven', 'atwelve', 'athirteen', 'afourteen', 'afifteen', 'asixteen', 'aseventeen']
您可以考虑使用 more-itertool
中的 chunked
函数:
from more_itertools import chunked
from math import ceil
from typing import List, Any, Tuple, Iterator
def distribute_evenly(items: List[Any], cells: List[Any]) -> Iterator[Tuple[Any, List[Any]]]:
if len(items) <= len(cells):
item_chunks = [[item] for item in items] + [[]] * (len(cells) - len(items))
else:
chunk_size = int(ceil(len(items) / len(cells)))
item_chunks = chunked(auditor, chunk_size)
return zip(cells, item_chunks)
location = ["loc1", "loc2", "loc3"]
auditor = ["aud1", "aud2", "aud3", "aud4", "aud5", "aud6", "aud7", "aud8"]
auditor_per_location = list(distribute_evenly(auditor, location))
# auditor_per_location is now [('loc1', ['aud1', 'aud2', 'aud3']), ('loc2', ['aud4', 'aud5', 'aud6']), ('loc3', ['aud7', 'aud8'])]
祝你好运!