2017-12-28 15 views
0

각각 50 개의 난수로 구성된 두 개의 목록 ("lstA", "lstB")을 만들고 싶습니다. 그런 다음 두 목록과 세 번째 목록 ("lstOvrlp")에 넣으려는 현재 겹침을 비교하려고합니다. 그런 다음 세 번째 목록의 길이를 가져 와서 네 번째 목록 ("lstC")에 넣고 싶습니다.새로운 목록 every While 루프

다음으로이 기본 프로그램을 10 번 반복하면됩니다 (나중에이 부분에 사용자 입력을 추가하겠습니다).

def main(): 

     import random 

     lstA = [] 
     lstB = [] 
     lstC = [] 



     lstA = random.sample(range(1, 101), 50) 
     lstB = random.sample(range(1, 101), 50) 

     lstOvrlp = set(lstA) & set(lstB) 

     while len(lstC) <= 10: 
      print("This is list A:", lstA) 
      print("\n") 
      print("This is list B:", lstB) 
      print("\n") 
      print("This is the overlap between the two lists:", lstOvrlp) 

      numinC = len(lstOvrlp) 
      print("Number in common: ", numinC) 
      print("\n") 

      lstC.append(numinC) 
      print(lstC) 



    main() 

문제가 오히려 프로그램이 바로 루프를 통해 임의의 숫자 매번 뱉어보다 결과 또 다시 같은 번호를 다시 사용합니다 :

내가 지금까지 가지고있는 코드입니다 "lstC"에서 같은 겹침 숫자와 결과적으로 동일한 길이. 아마도 내가 알지 못하는 임의의 다른 기능이 있지만 찾을 수 없습니다.

"lstC"가 다양한 항목으로 끝나도록 프로그램이 반복 될 때마다 "lstA"및 "lstB"에 새로운 번호 배치가 필요합니다. 나는 데이터로 작업하는 약간의 연습을 얻으려고 노력하고 있으며 결국에는 평균과 같은 정보에 대한 간단한 통계 분석을 수행 할 것이다.

여기에 나와있는 오류를 지적 할 수는 없지만 많은 정보가 있으므로 보장해 드릴 수 있습니다.

감사합니다.

+0

'random.sample' 세대 **를 ** while 루프에 넣어야합니다. 그렇지 않은 경우 (한 번에) * * 루프를 생성하지만 무작위 샘플을 다시 생성하지 않습니다. – BorrajaX

+0

변경하지 않으면 어떻게 변했습니까? –

+0

루프 내에서 lstA 및 lstB 샘플을 생성해야합니다. – Ejaz

답변

0

Python 프로그램이 순차적으로 실행됩니다. 즉, 과제를 볼 때 과제를 수행 한 다음 과제를 남깁니다. 전에 루프를 지정하면 그 루프의 모든 반복마다 값이 동일하게됩니다. 내부에이라는 과제를 지정하면 각 반복마다 변경됩니다. 네가 원하는 것은 후자 다.

보다는

lstA = random.sample(range(1, 101), 50) 
lstB = random.sample(range(1, 101), 50) 
lstOvrlp = set(lstA) & set(lstB) 
while len(lstC) <= 10: 
    ... 

당신은 아주 가까이 있었다

while len(lstC) <= 10: 
    lstA = random.sample(range(1, 101), 50) 
    lstB = random.sample(range(1, 101), 50) 
    lstOvrlp = set(lstA) & set(lstB) 
    ... 
0

을 고려! lstA = random.sample(range(1, 101), 50)lstB = random.sample(range(1, 101), 50)을 while 루프 내부로 이동하기 만하면 새 번호가 할당됩니다. 이 시도.

import random 
def main(): 


    while len(lstC) <= 10: 
     lstA = random.sample(range(1, 101), 50) 
     lstB = random.sample(range(1, 101), 50) 

     lstOvrlp = set(lstA) & set(lstB) 
     print("This is list A:", lstA) 
     print("\n") 
     print("This is list B:", lstB) 
     print("\n") 
     print("This is the overlap between the two lists:", lstOvrlp) 

     numinC = len(lstOvrlp) 
     print("Number in common: ", numinC) 
     print("\n") 

     lstC.append(numinC) 
     print(lstC) 



main() 
0

귀하의 문제는 while 루프에서 당신이 lstA, lstB, 또는 lstC을 변경하지 않는 것입니다. 대신, 다음을 시도해보십시오 :

def main(): 

    import random 

    lstA = [] 
    lstB = [] 
    lstC = [] 

    while len(lstC) <= 10: 
     lstA = random.sample(range(1, 101), 50) 
     lstB = random.sample(range(1, 101), 50) 

     lstOvrlp = set(lstA) & set(lstB) 
     print("This is list A:", lstA) 
     print("\n") 
     print("This is list B:", lstB) 
     print("\n") 
     print("This is the overlap between the two lists:", lstOvrlp) 

     numinC = len(lstOvrlp) 
     print("Number in common: ", numinC) 
     print("\n") 

     lstC.append(numinC) 
     print(lstC) 


main() 
1

나는 발전기를 사용하는 거라고 및 random.randint 다음 교차로를 얻을 수있는 세트로 목록을 비교합니다.

from random import randint 


def random_list(): 
    for _ in range(50): 
     yield randint(1, 101) 


list_c = [] 

for _ in range(10): 
    list_a = list(random_list()) 
    list_b = list(random_list()) 
    print('list_a: {}'.format(list_a)) 
    print('list_b: {}'.format(list_b)) 

    # This is actually called an intersection, not an overlap. 
    list_intersect = set(list_a) & set(list_b) 
    print('list_overlap: {}'.format(list_intersect)) 

    list_c.append(len(list_intersect)) 

print(list_c)