Python为数字分配列表

时间:2018-11-04 02:05:08

标签: python

我正尝试将一系列数字随机分配给员工列表。

例如,我有雇员John,Rick,Sally,Bill,他们需要随机分配1到26号。

因此,约翰可以分配给他1,3,20,15 瑞克可能有5、10、26,已分配等,等等。

这是我到目前为止所拥有的:

from random import shuffle
number = 26
emp = list('John', 'Sally', 'Rick', 'Bill')
shuffle(emp)

#then get stuck on how to loop through this properly

我知道不多。任何帮助或一般性指导表示赞赏。

2 个答案:

答案 0 :(得分:0)

如果只想给每个名称分配一个号码。您可以使用import QtQuick 2.0 import QtQuick.Layouts 1.11 Item { id: sideTabBar width: 70 height: parent.height property int currentIndex: -1 ListModel{ id: elements ListElement { image: "../assets/product.svg" } ListElement { image: "../assets/product.svg" } ListElement { image: "../assets/product.svg" } ListElement { image: "../assets/product.svg" } ListElement { image: "../assets/product.svg" } ListElement { image: "../assets/product.svg" } } Rectangle{ anchors.fill: parent color: "purple" } ColumnLayout{ id:sidebarLayout anchors.fill: parent spacing:2 Repeater{ model: elements SideBarElement{ id: element highlight: ix == currentIndex icon: image property int ix: index Layout.preferredHeight: 70 Layout.alignment: Qt.AlignTop MouseArea{ anchors.fill: parent onClicked: currentIndex = ix } } } Item {Layout.fillHeight: true} } } 来生成random.randint0之间的随机数,并可以通过简单的列表理解功能将每个用户的数字分配为元组,

26

如果您想为每个员工生成多个号码,则可以使用import random number = 26 emp = ['John', 'Sally', 'Rick', 'Bill'] x = [(i,random.randint(0,number+1)) for i in emp] print(x) # output, [('John', 26), ('Sally', 8), ('Rick', 2), ('Bill', 19)]

random.sample

或作为字典

import random
number = 26
number_to_generate = 3 # to generate 3 random numbers for each emp
emp = ['John', 'Sally', 'Rick', 'Bill']

x = [(i,*random.sample(range(0, number+1), number_to_generate)) for i in emp]
print(x)

# output,
[('John', 4, 3, 21), ('Sally', 3, 24, 7), ('Rick', 12, 4, 2), ('Bill', 17, 19, 11)]

答案 1 :(得分:0)

此答案假定您要在要分配所有数字且每个员工有多个数字的员工之间随机分配数字。

一种解决方案是每个员工使用一个列表。但是,为了便于以编程方式在员工之间进行选择并根据不同的员工人数来调整程序,我将所有列表都放入了词典中。

请注意,我使用的是list(1, 2, 3)语法,而不是[1, 2, 3],因为这种构造列表的语法更为常见。

# choice picks a random element from a list
from random import choice 

# emp_numbers is the collection of lists, one for each employee
emp_numbers = {'John':[], 'Sally':[], 'Rick':[], 'Bill':[]};

# emp is constructed from emp_numbers
# so that we don't have to list out the employees twice.
# We can't use emp_numbers.keys() directly as an input to choice because 
# emp_numbers.keys() doesn't support indexing since .keys() doesn't
# return a list. Choice requires indexing to function.
# Thus we convert it into a list, which does support indexing.
#
# (If you are still using python 2, then keys() does return a list,
# but converting a list to a list is harmless, so our code runs in both versions)
emp = list(emp_numbers.keys())

# now we want to distribute the numbers
numbers = 26;

for number in range(1, 26+1):
   #for each number, pick an employee.
   employee_name = choice(emp);
   #append the number to the end of that employee's list of numbers
   emp_numbers[employee_name].append(number)

print(emp_numbers)

打印出类似以下内容的内容:{'Rick': [4, 12, 21, 22, 24], 'John': [1, 5, 6, 8, 9, 10, 11, 13, 14, 17, 25, 26], 'Sally': [2, 7, 15, 16, 18, 19, 20, 23], 'Bill': [3]} `