from collections import defaultdict
from random import shuffle
artist = ['소녀시대','소녀시대','방탄소년단','방탄소년단','샤이니','샤이니']
music = ['소원을 말해봐','다시 만난 세계','IDOL','작은것들을 위한 시','데리러가','루시퍼']
box = defaultdict(list)
for i, arti in enumerate(artist) :
box[arti].append(music[i])
a = []
arti = []
for key, value in box.items() :
arti.append(key)
for song in value :
a.append(song)
while 1 :
count = 0
shuffle(a)
for i in range(1,len(a)) :
for k in range(len(arti)) :
if (a[i-1] in box[arti[k]]) and (a[i] in box[arti[k]]) :
count += 1
if count == 0 :
break
print(a)
------------------------------------------------------------------------------------------------
from collections import defaultdict
from random import shuffle
artist = ['소녀시대','소녀시대','방탄소년단','방탄소년단','샤이니','샤이니']
music = ['소원을 말해봐','다시 만난 세계','IDOL','작은것들을 위한 시','데리러가','루시퍼']
# 딕셔너리 생성
box = defaultdict(list)
for i, arti in enumerate(artist) :
box[arti].append(music[i])
# 중복 삭제된 가수 리스트 생성
arti = list(set(artist))
# music 리스트 shuffle 후 앞 뒤로 분석하여 조건 맞으면 출력
while 1 :
count = 0
shuffle(music)
for k in range(len(arti)) :
for i in range(1,len(music)) :
if (music[i-1] in box[arti[k]]) and (music[i] in box[arti[k]]) :
count += 1
if count == 0 :
break
print(music)