简体   繁体   English

找出哪个项目来自python

[英]Find out which list an item came from in python

Here is my code: 这是我的代码:

import random
from random import shuffle


a = ['cat','dog','fish','dolphin']
c = ['fridge','stove','microwave','oven']
b = ['cat','dog','fish','dolphin']
d = ['couch','chair','table','stool']

shuffle(c)
shuffle(d)

ac = a + c
bd = b + d

indices = random.sample(range(len(ac)),len(ac))
ac = list(map(ac.__getitem__, indices))
bd = list(map(bd.__getitem__, indices))

print ac
print bd

What this code does it randomize a bunch of lists so that a and b are put into the same order, and c and d are in their own randomized order. 这段代码的作用是将一堆列表随机化,从而使a和b处于相同的顺序,而c和d处于其自己的随机顺序。 What I would like to do is create an if statement that states whether the item came from list a or c. 我想做的是创建一个if语句,该语句说明该项目来自列表a还是c。 For example, if ac = from list a, then print "congruent" or something like that. 例如,如果ac = from list a,则打印“ congruent”或类似内容。 Is there any way to do this? 有什么办法吗? Thanks. 谢谢。 :) :)

Assuming 'a' and 'c' do not contain the same members, simple membership testing should work: 假设“ a”和“ c”不包含相同的成员,则简单的成员资格测试应该有效:

for el in ac:
    if el in c:
        print('element ',el,' came from list c')
    else:
        print('element ',el,' came from list a')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM