2016-07-08 1 views
0

이것은 매우 어려운 질문이지만 목록 및 튜플을 사용하여 작업하는 다른 사람들에게 유용 할 수 있습니다. 목록 CALC_RECORD을 가져 와서 목록에서 세 번째 요소를 가져와야합니다. 현재 출력은 [('calculation', 1467938304.345363, 1.2636651992797852)]과 같습니다. 튜플의 세 번째 요소는 플레이어가 질문에 대답하는 데 걸린 시간입니다. 질문에 응답 할 때마다 새 튜플이 목록에 만들어집니다. 그러면 다음과 같이 보입니다 : [('calculation', 1467938302.2010334, None), ('calculation', 1467938302.8568625, None), ('calculation', 1467938304.345363, 1.2636651992797852)]. 각 튜플에서 세 번째 요소를 꺼내고 그 요소를 사용하여 목록을 만들어야합니다. 그 목록은 그래프에서 y 축으로 사용될 것입니다. (x 축은 게임이 수행 된 총 시간입니다.) 람다를 필터 함수와 함께 사용해 보았지만 아무 것도 없습니다. 어떤 아이디어입니까?리스트 작성 및 플로팅에 도움이 필요합니다.

참고 :

아래 프로그램 정식 버전에는 더 많은 게임/기능이 있지만 질문에 답변 할 수 있다면 공간을 절약하기 위해 제거되어 동일한 답변을 사용할 수 있습니다. 여기에 중복 될

편집 :. 나는이처럼하려고 노력

#EDIT: filtered_list = list(filter(lambda x: x[0] =='calculation' and x[2] != None, CALC_RECORD)) 

import random 
from random import randint 
import time 
import math 
import matplotlib.pyplot as plt 

# Number of problems for each practice/real round 
practice_round = 0 
real_round = 3 

main_record = [] 
CALC_RECORD = [] 

# (1) Calculation Game --------------------------------------------------------- 

def calculation(): 
    response_time = None 
    # Determine the min and max calculation values 
    min_calculation_value = 1 
    max_calculation_value = 10 
    # Generate the problems 
    print('\nSolve the following problem:') 
    a = random.randint(min_calculation_value, max_calculation_value) 
    b = random.randint(min_calculation_value, max_calculation_value) 
    problem_type = random.randint(1,2) 
    if problem_type == 1: 
     answer = a * b 
     print(a, '*', b) 
    elif problem_type == 2: 
     answer = a % b 
     print(a, '%', b) 
    # Get the user's answer determine what to do if correct 
    start_time = time.time() 
    user_answer = input('\nEnter your answer: ') 
    end_time = time.time() 
    if user_answer == str(answer): 
     response_time = end_time - start_time 
     print('You are correct!') 
    elif user_answer != str(answer): 
     print('You are incorrect.') 
    # Return game id, start time, and response time 
    return("calculation", start_time, response_time) 

def calculation_game(): 
    record = [] 
    # Generate two problems for a practice round 
    print("\nLet's begin with 2 practice problems.") 
    for i in range (practice_round): 
     print('\nPractice Problem', i + 1, 'of', practice_round) 
     calculation() 
    # Generate 10 problems for a real, recorded round 
    print("\nNow let's try it for real this time.") 
    for i in range (real_round): 
     print('\nProblem', i + 1, 'of', real_round) 
     # Append records for each iteration 
     record.append(calculation()) 
    main_record.extend(record) 
    CALC_RECORD.extend(record) 
    return record 

# (5) Display Data ------------------------------------------------------------- 
def display_data(): 
    print (CALC_RECORD) 
#This function is currently just being used to view the output of calc record 

---------------------------------------------------------------- 

def quit_game(): 
    print('\nThank you for playing!') 

# Main Menu -------------------------------------------------------------------- 

def menu(): 
    print("\nEnter 1 to play 'Calculation'") 
    print("Enter 2 to play 'Binary Reader'") 
    print("Enter 3 to play 'Trifacto'") 
    print("Enter 4 to view your statistics") 
    print("Enter 5 to display data") 
    print("Enter 6 to save your progress") 
    print("Enter 7 to load data") 
    print("Enter 8 to quit the game") 

def main_menu(): 

    print('Welcome--Let's Play!') 
    main_record = [] 
    user_input = '' 
    while user_input != '8': 
     menu() 
     user_input = input('\nWhat would you like to do? ') 
     if user_input == '1': 
      calculation_game() 
     if user_input == '2': 
      binary_reader_game() 
     if user_input == '3': 
      trifacto_game() 
     if user_input == '4': 
      display_statistics() 
     if user_input == '5': 
      display_data() 
     if user_input == '8': 
      quit_game() 

main_menu() 
.

new_list = [t[2] for t in old_list] 

또는 당신은 maplambda를 사용할 수 있습니다 : 당신이 튜플의 목록을 가지고, 당신은 새 목록에 각 튜플의 세 번째 요소를 뽑아하려면

+0

무엇을 사용하려고 했습니까? http://stackoverflow.com/help/mcve –

+0

을 읽으십시오. 제가 시도한 것을 올리려는 의도였습니다. 잠시 후에 나올거야. – kjf545

답변

0

, 그 다음은 작동합니다

new_list = list(map(lambda t: t[2], old_list)) 

In [11]: old_list = [('calculation', 1467938302.2010334, None), ('calculation', 1467938302.8568625, None), ('calculation', 1467938304.345363, 1.2636651992797852)] 

In [12]: old_list 
Out[12]: 
[('calculation', 1467938302.2010334, None), 
('calculation', 1467938302.8568625, None), 
('calculation', 1467938304.345363, 1.2636651992797852)] 

In [13]: [t[2] for t in old_list] 
Out[13]: [None, None, 1.2636651992797852] 

In [14]: list(map(lambda t:t[2], old_list)) 
Out[14]: [None, None, 1.2636651992797852] 

편집 :

In [15]: [t[2] for t in old_list if t[0] =='calculation' and t[2] != None] 
Out[15]: [1.2636651992797852] 
,691,363 : 당신은 당신의 초기 시도의 추가 한 코드를 기반으로, 당신은 쉽게 그래서 같은 지능형리스트와지도 및 필터 값 수210
+0

와우, 정말 고마워! – kjf545

+0

게임이리스트에서 플레이 된 횟수를 추적 할 수있는 방법이 있습니까? 나는 최소한 새로운 프로그래머이기 때문에, 나는 붙어있다. – kjf545

+0

@ kjf545이 답이 유용하다면, 위 표결, 하회 표 합계 하단의 체크 표시를 클릭하여 "수락"할 수 있습니다. –

관련 문제