Python 3.x 我的python代码只将偶数个名称添加到组中,需要它将所有名称(奇数或偶数)添加到组中
目前,我的脚本获取一个csv名称列表,根据用户的偏好(组数或每组人数)将其随机排列并排序为组。我的代码中有一个缺陷,每个组只能排序出偶数个名称。例如,如果列表中有30个人,而用户想要5个组,那么它会对每个组进行6个排序。此外,如果列表中有33个名称,并且用户想要5个组,那么它仍然只对每个组进行6个排序,而忽略其余3个名称 我是一名初学者,正在寻找一些帮助来编辑我的代码,通过csv枚举并将所有名称添加到一个组中,即使名称总数是奇数(例如,组1-3各有7个名称,组4和组5各有6个名称,等于所有33个名称) 以下是我当前的代码:Python 3.x 我的python代码只将偶数个名称添加到组中,需要它将所有名称(奇数或偶数)添加到组中,python-3.x,Python 3.x,目前,我的脚本获取一个csv名称列表,根据用户的偏好(组数或每组人数)将其随机排列并排序为组。我的代码中有一个缺陷,每个组只能排序出偶数个名称。例如,如果列表中有30个人,而用户想要5个组,那么它会对每个组进行6个排序。此外,如果列表中有33个名称,并且用户想要5个组,那么它仍然只对每个组进行6个排序,而忽略其余3个名称 我是一名初学者,正在寻找一些帮助来编辑我的代码,通过csv枚举并将所有名称添加到一个组中,即使名称总数是奇数(例如,组1-3各有7个名称,组4和组5各有6个名称,等于所有33个
import csv
import random
import pprint
import sys
#import pandas as pd
with open(input('Enter file name: \n'),'r', encoding = 'Latin-1') as csvfile: # opens csv directory file
rdr = csv.reader(csvfile)
rdr = list(rdr)
def name_generator(lst): # generator that shuffles list of names
random.shuffle(lst)
return lst
shuf_lst = name_generator(rdr) # list of shuffled names
headcount = len(shuf_lst) # how many names in the list (will later be rsvp'd names only)
def group_size_generator():
final_dict2 = {} #initiated blank dictionary
gpsz = input('How many people per group? \n')
gpct = int(headcount) // int(gpsz) # number of people per group
for x in range(int(gpct)):
final_dict2['group{0}'.format(str(x+1))] = x + 1
if len(shuf_lst) != 0:
for k, v in final_dict2.items():
workinglist = []
for y in range(int(gpsz)):
workinglist.append(shuf_lst[0])
del shuf_lst[0]
final_dict2[k] = workinglist
pprint.pprint(final_dict2)
def group_number_generator():
final_dict1 = {} # initiated blank dictionary
gpct = input('How many groups? \n')
gpsz = int(headcount) // int(gpct) #number of of people per group
print(gpsz)
for x in range(int(gpct)): # initializes the dict with group identifiers
final_dict1['group{0}'.format(str(x + 1))] = x + 1
if len(shuf_lst) != 0: # condition that appends specified number of names per group to the groups in dict
for k, v in final_dict1.items():
workinglist = []
for y in range(int(gpsz)):
workinglist.append(shuf_lst[0])
del shuf_lst[0]
final_dict1[k] = workinglist
pprint.pprint(final_dict1)
def user(): #user input
user_input = input('Choose one: \n A.) How may people per group? \n B.) How many groups? \n')
for x in user_input:
if x == 'A' or x == 'a':
return(group_size_generator())
if x == 'B' or x == 'b':
return(group_number_generator())
else:
print('Error: Appropriate option not selected. Please try again.')
print(user())
我只研究了group\u size\u生成器函数(我假设group\u number\u生成器使用类似的逻辑),您的错误实际上来自逻辑本身 1。创建组
gpct = int(headcount) // int(gpsz) # number of people per group
for x in range(int(gpct)):
final_dict2['group{0}'.format(str(x+1))] = x + 1
在这个区块中,您有固定数量的组,无论是30人还是33人
2。基于组的循环
for k, v in final_dict2.items():
workinglist = []
for y in range(int(gpsz)):
workinglist.append(shuf_lst[0])
del shuf_lst[0]
此外,您还可以循环查看组(而不是总人数)。因此,一旦所有的组都填满了,它就退出了-如果除法操作中有剩余部分,则会在shuflst
中留下剩余部分
对于初学者来说,这段代码实际上看起来很棒(干得好!)。如果你在人数上循环,而不是在组的数量上循环,你应该会很好。实际上,你也不必事先初始化你的dict组。DICT可以通过简单的赋值进行扩展:
final_dict2['6'] = workinglist
因此,虽然您的
shuf_lst
不是空的,但为final_dict2
创建一个新的键,并将工作列表分配给它。我只查看了group_size_生成器函数(我假设group_number_生成器使用类似的逻辑),而您的错误实际上来自逻辑本身
1。创建组
gpct = int(headcount) // int(gpsz) # number of people per group
for x in range(int(gpct)):
final_dict2['group{0}'.format(str(x+1))] = x + 1
在这个区块中,您有固定数量的组,无论是30人还是33人
2。基于组的循环
for k, v in final_dict2.items():
workinglist = []
for y in range(int(gpsz)):
workinglist.append(shuf_lst[0])
del shuf_lst[0]
此外,您还可以循环查看组(而不是总人数)。因此,一旦所有的组都填满了,它就退出了-如果除法操作中有剩余部分,则会在shuflst
中留下剩余部分
对于初学者来说,这段代码实际上看起来很棒(干得好!)。如果你在人数上循环,而不是在组的数量上循环,你应该会很好。实际上,你也不必事先初始化你的dict组。DICT可以通过简单的赋值进行扩展:
final_dict2['6'] = workinglist
因此,当您的shuf_lst
不为空时,为final_dict2
创建一个新键,并为其分配工作列表