随机select两个列表的某些索引创建两个新列表
Randomly select certain indices of two lists to create two new lists
假设我有两个列表:
A = ['cat', 'dog', 'cow', 'pig', 'monkey']
B = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
现在我想通过 selecting 3 个随机索引(不替换)创建 lists A
和 list B
,同时保持 A
和 A
的值之间的关系B
.
例如,
随机 selected 索引:4, 0, 3
因此,
A = ['monkey', 'cat', 'pig']
B = ['King Kong', 'Felix', 'Trump']
有没有一种方法可以做到这一点而无需编写迭代 3 次以 select 随机索引的 for 循环?
Python 有一个示例函数,可以在不替换的情况下进行选择。您可以从指数中抽样并将样本应用于您的输入。
from random import sample
A = ['cat', 'dog', 'cow', 'pig', 'monkey']
B = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
k = 3
samp = sample(range(len(A)), k)
A_p = [A[i] for i in samp]
B_p = [B[i] for i in samp]
您可以zip
the lists, then pick 3 random pairs with random.sample
,最后再次将这些对拆分为单独的列表:
import random
pairs = list(zip(A, B)) # make pairs out of the two lists
pairs = random.sample(pairs, 3) # pick 3 random pairs
A1, B1 = zip(*pairs) # separate the pairs
这是一步一步发生的事情:
>>> list(zip(A, B))
[('cat', 'Felix'), ('dog', 'Fido'), ('cow', 'Moo'), ('pig', 'Trump'), ('monkey', 'King Kong')]
>>> random.sample(_, 3)
[('monkey', 'King Kong'), ('pig', 'Trump'), ('dog', 'Fido')]
>>> list(zip(*_))
[('monkey', 'pig', 'dog'), ('King Kong', 'Trump', 'Fido')]
您可以使用:
import numpy as np
indx = np.random.choice(len(A),3,False)
np.array(A)[indx]
Out[593]: array(['cow', 'pig', 'monkey'], dtype='<U6')
np.array(B)[indx]
Out[594]: array(['Moo', 'Trump', 'King Kong'], dtype='<U9')
我认为我最喜欢 zip 答案,但我会使用 列表理解
来采用这种方法
import random
l1 = ['cat', 'dog', 'cow', 'pig', 'monkey']
l2 = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
r = [random.randint(0, len(l1)-1) for i in range(3)]
A = [l1[i] for i in r]
B = [l2[i] for i in r]
~/python/Whosebug/9.23$ python3.7 loop.py
['monkey', 'cow', 'cat']
['King Kong', 'Moo', 'Felix']
尽管可以将循环委托给函数或将其装扮成列表理解,但您无法避免循环。
不过,您可以遍历每个列表一次。
#lets assume your randomly selected numbers are 4, 0 and 3
indices = (4,0,3)
# lets assign an ordinal to each index
indices_ordinals = [(ind, ord) for ind, ord in enumerate(indices)]
# now we can re sort the indices, since we have recorded their original sequence
indices_ordinals.sort(key = lambda x: x[0])
# now go through the lists (for simplicity we assume they have equal number of elemwnts) and fill the output dicts
csi = 0 # csi is the index that we are looking for currently
A1 = {}
B1 = {}
for i, elem in enumerate(A):
if i == indices_ordinals[csi][0]:
A1[indices_ordinals[1]] : elem
csi += 1
csi =0
for i, elem in enumerate(B):
if i == indices_ordinals[csi][0]:
B1[indices_ordinals[1]] : elem
csi += 1
#now we only need to sort A1 and B1 to get desired output
A2=[A1[i] for i in sorted(A1)]
B2=[B1[i] for i in sorted(B1)]
假设我有两个列表:
A = ['cat', 'dog', 'cow', 'pig', 'monkey']
B = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
现在我想通过 selecting 3 个随机索引(不替换)创建 lists A
和 list B
,同时保持 A
和 A
的值之间的关系B
.
例如,
随机 selected 索引:4, 0, 3
因此,
A = ['monkey', 'cat', 'pig']
B = ['King Kong', 'Felix', 'Trump']
有没有一种方法可以做到这一点而无需编写迭代 3 次以 select 随机索引的 for 循环?
Python 有一个示例函数,可以在不替换的情况下进行选择。您可以从指数中抽样并将样本应用于您的输入。
from random import sample
A = ['cat', 'dog', 'cow', 'pig', 'monkey']
B = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
k = 3
samp = sample(range(len(A)), k)
A_p = [A[i] for i in samp]
B_p = [B[i] for i in samp]
您可以zip
the lists, then pick 3 random pairs with random.sample
,最后再次将这些对拆分为单独的列表:
import random
pairs = list(zip(A, B)) # make pairs out of the two lists
pairs = random.sample(pairs, 3) # pick 3 random pairs
A1, B1 = zip(*pairs) # separate the pairs
这是一步一步发生的事情:
>>> list(zip(A, B))
[('cat', 'Felix'), ('dog', 'Fido'), ('cow', 'Moo'), ('pig', 'Trump'), ('monkey', 'King Kong')]
>>> random.sample(_, 3)
[('monkey', 'King Kong'), ('pig', 'Trump'), ('dog', 'Fido')]
>>> list(zip(*_))
[('monkey', 'pig', 'dog'), ('King Kong', 'Trump', 'Fido')]
您可以使用:
import numpy as np
indx = np.random.choice(len(A),3,False)
np.array(A)[indx]
Out[593]: array(['cow', 'pig', 'monkey'], dtype='<U6')
np.array(B)[indx]
Out[594]: array(['Moo', 'Trump', 'King Kong'], dtype='<U9')
我认为我最喜欢 zip 答案,但我会使用 列表理解
来采用这种方法import random
l1 = ['cat', 'dog', 'cow', 'pig', 'monkey']
l2 = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
r = [random.randint(0, len(l1)-1) for i in range(3)]
A = [l1[i] for i in r]
B = [l2[i] for i in r]
~/python/Whosebug/9.23$ python3.7 loop.py ['monkey', 'cow', 'cat'] ['King Kong', 'Moo', 'Felix']
尽管可以将循环委托给函数或将其装扮成列表理解,但您无法避免循环。 不过,您可以遍历每个列表一次。
#lets assume your randomly selected numbers are 4, 0 and 3
indices = (4,0,3)
# lets assign an ordinal to each index
indices_ordinals = [(ind, ord) for ind, ord in enumerate(indices)]
# now we can re sort the indices, since we have recorded their original sequence
indices_ordinals.sort(key = lambda x: x[0])
# now go through the lists (for simplicity we assume they have equal number of elemwnts) and fill the output dicts
csi = 0 # csi is the index that we are looking for currently
A1 = {}
B1 = {}
for i, elem in enumerate(A):
if i == indices_ordinals[csi][0]:
A1[indices_ordinals[1]] : elem
csi += 1
csi =0
for i, elem in enumerate(B):
if i == indices_ordinals[csi][0]:
B1[indices_ordinals[1]] : elem
csi += 1
#now we only need to sort A1 and B1 to get desired output
A2=[A1[i] for i in sorted(A1)]
B2=[B1[i] for i in sorted(B1)]