return 一个列表中的索引范围以从另一个列表中获取值

return range of indices from one list to get the values from other list

我有两个大小相等的列表,一个有数据类型(连续出现):

types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]

第二个列表是关于数据的:

data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]

根据数据表,我知道"xx","tr""vigi""kl","ut","ew""fruits"等等。

我每次都需要将 data 分成两个数据集:

data1 = data[indices for type "vigi"]
data2 = data[indices for the remaining (i.e. data for "fruits" and "nothing")]

第二次会有:

data1 = data[indices for type "fruits"]
data2 = data[indices for the remaining (i.e. data for "vigi" and "nothing")]

等等..

请提供任何帮助。

您可以使用zip()函数:

types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]


data1 = [d for t, d in zip(types, data) if t == 'vigi']
data2 = [d for t, d in zip(types, data) if t != 'vigi']

print(data1)
print(data2)

打印:

['xx', 'tr']
['kl', 'ut', 'ew', 'uy', 'lp', 'eq', 'aq']

其他版本(只遍历列表一次):

types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]

data1, data2 = [], []
for t, d in zip(types, data):
    if t == 'vigi':
        data1.append(d)
    else:
        data2.append(d)

print(data1)
print(data2)

以下是如何使用 zip():

types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]

data1 = [d for d,t in zip(data,types) if t == 'vigi']
data2 = [d for d,t in zip(data,types) if t in ['fruits','nothing']]

print(data1)
print(data2)

输出:

['xx', 'tr']
['kl', 'ut', 'ew', 'uy', 'lp', 'eq', 'aq']

另一种方法是使用 enumerate():

types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]

data1 = [d for i,d in enumerate(data) if types[i] == 'vigi']
data2 = [d for i,d in enumerate(data) if types[i] in ['fruits','nothing']]

print(data1)
print(data2)

输出:

['xx', 'tr']
['kl', 'ut', 'ew', 'uy', 'lp', 'eq', 'aq']

但是这会遍历 data 列表两次,而只需要一次。相反,为了提高时间复杂度,请使用 for 循环:

types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]

data1 = []
data2 = []

for i,d in enumerate(data):
    if types[i] == 'vigi':
        data1.append(types[i])
    elif types[i] in ['fruits','nothing']:
        data2.append(types[i])

print(data1)
print(data2)

zip() 选项也是如此(请参阅@AndrejKesely 对 for 循环的回答)。

单循环解决方案的变体(参见其他答案)。依赖于布尔值在转换为 int 时将变为 01 的事实。可能被认为是 hacky,但包括娱乐...

data1, data2 = [], []
out = (data2, data1)
for t, d in zip(types, data):
    out[t == 'vigi'].append(d)

print(data1)
print(data2)

可能不完全是您想要的,但如果您将来有更复杂的要求,发布它可能会有所帮助:

>>> from itertools import groupby, count
>>> index = count()
>>> database = {key: [*group] for key, group in groupby(data, lambda x:types[next(index)])}
>>> database
{'vigi': ['xx', 'tr'],
 'fruits': ['kl', 'ut', 'ew'],
 'nothing': ['uy', 'lp', 'eq', 'aq']}

参考文献:

  1. itertools.groupby
  2. itertools.count