loopCount = 0
candidateName = input("Please input the candidates name or type 'DONE' when finished:")
votes = []
while candidateName != "DONE":
departmentName = input("Please input the department that submitted votes for this candidate:")
numberOfVotes = input("Please input the number of votes for this candidate:")
votes.append([])
votes[loopCount].append(candidateName)
votes[loopCount].append(departmentName)
votes[loopCount].append(numberOfVotes)
print(votes[loopCount])
loopCount = loopCount + 1
candidateName = input("Please input the candidates name or type 'DONE' when finished:")
print(votes)
sum = 0
for i in range(0,len(votes)):
这是我目前拥有的代码。我相信我需要做的就是用for循环来完成。我要做的是循环浏览列表,并累计每个候选人的投票数。当然,如果候选人具有匹配的名称,我需要正确分配。如果有人可以,请指出我如何以这种方式处理列表的正确方向。多维列表是什么让我失望。任何提示或建议都将不胜感激。谢谢。
答案 0 :(得分:2)
所以,你要找的是一个关键的,价值结构你的数据,如python字典。 由于这是二维(候选名称和部门名称),因此您需要一个嵌套字典。
Python不允许autovification像perl一样,所以你想使用defaultdict自动允许你像字典一样使用嵌套结构。
以下是重构版本:
import java.util.Scanner;
public class StringShiftTwoLeftThenRight
{
public static void main(String[] args)
{
String word, rightShift = "", leftShift = "";
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter a word: ");
word = keyboard.nextLine();
rightShift = (word.substring((0),
(word.length()-2)));
leftShift = (word.substring((2),(word.length())));
System.out.println("\nThe String shifted two to right looks like this: " + rightShift);
System.out.println("\nThe String shifted two to left looks like this: " + leftShift);
System.out.println("\nThe String as it is looks like: " + word);
}
}
答案 1 :(得分:0)
我的看法。有更好的方式,但它的工作原理
totalvotes = {}
for vote in votes:
totalvotes[vote[0]] = totalvotes[vote[0]]+int(vote[2]) if vote[0] in totalvotes else int(vote[2])
print(totalvotes)
或更紧凑
print([(gk,sum([int(v[2]) for v in gv] )) for gk, gv in itertools.groupby( sorted(votes,key=lambda x:x[0]),lambda x: x[0])])
怪异的单行:
function my_scripts_method() {
wp_enqueue_script(
'custom-script',
get_stylesheet_directory_uri() . '/masonry.php',
array( 'jquery' )
);
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
答案 2 :(得分:0)
我会使用python词典。根据需要修改代码。
votes = [['a','d',3],['b','d',3],['a','d',2]]
unique_names_list = {}
for ballot in votes:
name = ballot[0]
department = ballot[1]
votes = ballot[2]
name_depart = name + '-' + department
if name_depart not in unique_names_list.keys():
unique_names_list[name_depart] = votes
else:
unique_names_list[name_depart] = unique_names_list[name_depart] + votes
答案 3 :(得分:0)
您只需要处理代码当前收集的结果。一种方法是这样的:
canidates = []
loopCount = 0
candidateName = input("Please input the candidates name or type 'DONE' when finished:")
votes = []
while candidateName != "DONE":
departmentName = input("Please input the department that submitted votes for this candidate:")
numberOfVotes = input("Please input the number of votes for this candidate:")
votes.append([])
votes[loopCount].append(candidateName)
votes[loopCount].append(departmentName)
votes[loopCount].append(numberOfVotes)
print(votes[loopCount])
loopCount = loopCount + 1
candidateName = input("Please input the candidates name or type 'DONE' when finished:")
results = []
#Tally the votes
for v in votes:
if v[0] in canidates:
#need to add these votes to an existing canidate.
for x in results:
#goes through each record in results and adds the record to it.
if x[0] == v[0]:
x[1] = x[1] + ", " + v[1]
x[2] = x[2] + int(v[2])
else:
#need to create a new record for the canidate.
canidates.append(v[0])
results.append([v[0],v[1],int(v[2])])
#Display the results
print ("Vote Results:")
for x in results:
print ("name: " + x[0])
print ("Departments: " + x[1])
print ("Votes Total: " + str(x[2]))
因此,当同一个canidate名称有多个条目时,您要做的就是完成并合并条目。