这是一个多肉的问题,但对于使用list和tuples的其他人来说可能会有用。我需要获取列表CALC_RECORD
,然后从列表中拉出第三个元素。输出目前看起来像这样:[('calculation', 1467938304.345363, 1.2636651992797852)]
。元组中的第三个元素是玩家回答问题所花费的时间。每次回答问题时,都会在列表中创建一个新元组。所以它看起来像这样:[('calculation', 1467938302.2010334, None), ('calculation', 1467938302.8568625, None), ('calculation', 1467938304.345363, 1.2636651992797852)]
。我需要从每个元组中提取第三个元素,并使用这些元素创建一个列表。然后该列表将用作图表中的y轴(x轴是游戏的总次数。我尝试使用带有过滤功能的lambda,但我无处可去。有什么想法吗?
注:
以下程序的完整版本中有更多游戏/功能,但它们已被移除以节省空间,因为如果可以回答问题,则可以对它们使用相同的答案。因此,将它们放在这里将是多余的。
编辑: 我试着这样做。
#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()
答案 0 :(得分:0)
如果你有一个元组列表,并且想要将每个元组的第三个元素拉出到一个新列表中,那么以下内容应该可以工作:
new_list = [t[2] for t in old_list]
或者您可以使用map
和lambda
:
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]