第一个列表:包含相应类别名称的列表索引
第二个列表:包含类别名称作为字符串
Intervals=[[Indexes_Cat1],[Indexes_Cat2],[Indexes_Cat3], ...]
Category_Names=["cat1","cat2","cat3",...]
所需的输出:
list=["cat1", "cat1","cat2","cat3","cat3"]
其中使用“间隔”列表放置输出列表中任何元素的索引。
Ex1:
Intervals=[[0,4], [2,3] , [1,5]]
Category_Names=["a","b","c"]
示例:输出1
["a","c","b","b","a","c"]
编辑:更多运行案例
Ex2:
Intervals=[[0,1], [2,3] , [4,5]]
Category_Names=["a","b","c"]
示例:输出2
["a","a","b","b","c","c"]
Ex3:
Intervals=[[3,4], [1,5] , [0,2]]
Category_Names=["a","b","c"]
示例:输出3
["c","b","c","a","a","b"]
我的解决方案:
创建大小为n的任何空数组。
为每个类别运行一个for循环。
output=[""]*n
for i in range(len(Category_Names)):
for index in Intervals[I]:
output[index]=Categories[i]
有更好的解决方案,还是更Python化的方式?谢谢
答案 0 :(得分:2)
我不确定是否有办法避免嵌套循环(我现在想不起来),所以看来您的解决方案很好。
一种更好的方法是使用以下类别之一构造输出数组:
output = [Category_Names[0]]*n
,然后跳过该类别开始迭代:
for i in range(1, len(Category_Names)):
如果您知道有一个类别比其他类别出现得更多,则应将其用作初始化数组的类别。
我希望这会有所帮助!
答案 1 :(得分:2)
def categorise(Intervals=[[0,4], [2,3] , [1,5]],
Category_Names=["a","b","c"]):
flattened = sum(Intervals, [])
answer = [None] * (max(flattened) + 1)
for indices, name in zip(Intervals, Category_Names):
for i in indices:
answer[i] = name
return answer
assert categorise() == ['a', 'c', 'b', 'b', 'a', 'c']
assert categorise([[3,4], [1,5] , [0,2]],
["a","b","c"]) == ['c', 'b', 'c', 'a', 'a', 'b']
请注意,在这段代码中,如果“间隔”没有覆盖从零到最大间隔数的所有整数,则您将在答案中获得None
值。假定输入是兼容的。
答案 2 :(得分:1)
您可以减少创建的字符串数量,并使用enumerate
避免使用range(len(..))
进行索引。
Intervals=[[0,4], [2,3] , [1,5]]
Category_Names=["a","b","c"]
n = max(x for a in Intervals for x in a) + 1
# do not construct strings that get replaced anyhow
output=[None] * n
for i,name in enumerate(Category_Names):
for index in Intervals[i]:
output[index]=name
print(output)
输出:
["a","c","b","b","a","c"]