Py学习  »  Python

我的python代码只向组中添加偶数个名称,需要它向组中添加所有名称(奇数或偶数)

Bee • 5 年前 • 1478 次点击  

目前,我的脚本采用一个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())
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40687
 
1478 次点击  
文章 [ 1 ]  |  最新文章 5 年前
kerwei
Reply   •   1 楼
kerwei    6 年前

我只看了group size_generator函数(我假设group number_generator使用类似的逻辑),而您的bug实际上来自逻辑本身。

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]

此外,您还可以循环查看组(而不是总人数)。因此,一旦所有的组都填满了,它就退出了,剩下的就是 shuf_lst 如果除法运算中还有余数。

这段代码对于初学者来说真的很不错(干得好!)。如果你循环访问人数,而不是组的数量,你应该没事。实际上,你也不必在下手之前初始化dict组。听写可以通过简单的赋值进行扩展:

final_dict2['6'] = workinglist

因此,当你 舒夫尔斯特 不是空的,请为 final_dict2 并将工作列表分配给它。