我正在尝试在列表中打印列表。我知道,我之前发布了一个类似的问题 - Trouble printing a list within a list,但这是同一主题中的另一个问题,所以请不要将其标记为重复。我已将两个代码合并在一起,这是最终代码。在if语句中,我试图打印用户输入的某个客户的详细信息customer_number
。代码搜索myList
,Payments.txt
已附加到[['E1234', '12/09/14', '440', 'A', '0']]
Customer number: E1234
Date of payment: 12/09/14
Payment Amount: 440
Paid Amount: 0
。代码可以完成所有这些,但问题是它多次打印出来;准确地说是6次:
print("Option A: Show a record\nOption Q: Quit")
decision = input("Enter A or Q: ")
myList = []
if decision == "A" or decision == "a":
myFile = open("Payments.txt")
customer_number = input("Enter a customer number to view their scores: ")
record = myFile.readlines()
for line in record:
myList.append(line.strip().split(','))
details = [x for x in myList if x[0] == customer_number]
if details:
print(details)
print("Customer number: ", details[0][0])
print("Date of payment: ", details[0][1])
print("Payment Amount: ", details[0][2])
print("Paid Amount: ", details[0][4])
myFile.close()
print(myList)
elif decision == "Q" or "q":
exit
这是我的代码:
library(dplyr)
test %>% group_by(names, dates) %>% filter((n()>=2 & !is.na(values)) | n()==1)
Source: local data frame [8 x 3]
Groups: names, dates [8]
names dates values
(fctr) (fctr) (dbl)
1 Mike 04-26 1
2 Mike 04-27 2
3 John 04-28 4
4 John 04-27 5
5 John 04-26 6
6 David 04-01 1
7 David 04-02 2
8 David 04-03 NA
答案 0 :(得分:3)
当您找到并打印答案时,您需要退出循环。
if details:
print(details)
print("Customer number: ", details[0][0])
print("Date of payment: ", details[0][1])
print("Payment Amount: ", details[0][2])
print("Paid Amount: ", details[0][4])
break