我们有一个名为 location 的变量。
location=[["world", 'Live'], ["alpha",'Live'], ['hello', 'Scheduled'],['alpha', 'Live'], ['just', 'Live'], ['alpha','Scheduled'], ['alpha', 'Live']]
Run Code Online (Sandbox Code Playgroud)
我想找到第一个索引并计算list["alpha",'Live']在位置中的出现次数。我尝试了以下方法:
index= [location.index(i) for i in location if i ==["alpha", 'Live'] ]
count = [location.count(i) for i in location if i ==["alpha",'Live'] ]
print('index',index)
print('count', count)
Run Code Online (Sandbox Code Playgroud)
返回:索引 [1, 1, 1] 计数 [3, 3, 3]
但有没有办法找到第一个索引,并使用列表理解同时计数。
预期输出:
索引,计数 = 1, 3