Python 随机选择两个列表的某些索引以创建两个新列表
假设我有两个列表:Python 随机选择两个列表的某些索引以创建两个新列表,python,python-3.x,list,indices,Python,Python 3.x,List,Indices,假设我有两个列表: A = ['cat', 'dog', 'cow', 'pig', 'monkey'] B = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong'] 现在我想通过选择3个随机索引(不替换)创建列表A和列表B,同时保持A和B的值之间的关系 比如说, 随机选择的索引:4,0,3 所以, A = ['monkey', 'cat', 'pig'] B = ['King Kong', 'Felix', 'Trump'] 有没有一种方法可以在不编
A = ['cat', 'dog', 'cow', 'pig', 'monkey']
B = ['Felix', 'Fido', 'Moo', 'Trump', 'King Kong']
现在我想通过选择3个随机索引(不替换)创建列表A
和列表B
,同时保持A
和B
的值之间的关系
比如说,
随机选择的索引:4,0,3
所以,
A = ['monkey', 'cat', 'pig']
B = ['King Kong', 'Felix', 'Trump']
有没有一种方法可以在不编写for循环的情况下执行此操作,for循环会迭代3次以选择随机索引?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]
您可以选择列表,然后使用选择3个随机对,最后再次将这些对拆分为单独的列表:
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')
将numpy导入为np
indx=np.随机选择(len(A),3,False)
np.数组(A)[indx]
Out[593]:数组(['cow','pig','monkey',],dtype='我想我最喜欢zip答案,但我会使用列表理解的方法
尽管可以将循环委托给函数或将其装扮成列表,但无法避免循环。
不过,您可以在每个列表上循环一次
#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)]
是的,有一种方法可以做到这一点,但是作为一个新来的人,你应该检查提问策略,因为它将帮助你获得答案。如果你用一些你已经尝试过的代码提问会容易得多,因为这将允许mw给你一个具体的答案。你能包括一些代码吗?你看过python的随机库吗?谢谢!这正是我要找的
~/python/stackoverflow/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)]