WEEK1~5 <<
Previous Next >> WEEK6~9
解釋亂數分組之程序
1.random模組的應用
參考資料:https://www.runoob.com/python/func-number-random.html
1.嘗試利用random.sample的方式抽出組別
但是由於對python的不熟悉無法如願的打出有效的指令。
import random
team = ('1', '2', '3', '4', '5', '6', '7')
def f1():
# 方法一:(假設沒有重複元素)編立集合,將尚未選擇的元素组成新集合。
r1 = random.sample(team, 3)
r2 = []
for item in team:
if item not in r1:
r2.append(item)
print('result1: %s' % random.sample(r2, 2))
def f2():
# 方法二:(假設沒有重複元素)使用集合 set,計算差集。
r1 = random.sample(team, 3)
r2 = tuple(set(team) - set(r1))
print('result2: %s' % random.sample(r2, 2))
def f3():
# 方法三:(假設沒有重複元素)隨機選擇集合元素的下標,而不是元素值。
index_r1 = random.sample(range(len(team)), 3)
# r1 = [team[i] for i in index_r1]
remain_index = tuple(set(range(len(team))) - set(index_r1))
index_r2 = random.sample(remain_index, 2)
r2 = [team[i] for i in index_r2]
print('result3: %s' % r2)
參考https://segmentfault.com/q/1010000014061245
2.利用random.shffle的方式洗出組別
import random
#這邊我們從http://s1.mde.nfu.edu.tw:8000/?semester=1082&courseno=0767 抓取資料
groupList=[['40723104', '40723107', '40723114', '40723115', '40723118', '40723122', '40723123', '40723140', '40723149', '40723151', '40723153', '40723154', '40723155', '40623143', '40723108', '40723127', '40723132', '40723133', '40723137', '40723141', '40723143', '40723144', '40723147', '40723148', '40723150', '40723103', '40723110', '40723112', '40723120', '40723125', '40723126', '40723128', '40723130', '40723139', '40723142', '40723145', '40723146', '40423155', '40723101', '40723102', '40723106', '40723111', '40723119', '40723121', '40723124', '40723134', '40723135', '40723136', '40723138']]
#設定變數0
group=groupList[0]
#將group數列隨機排列
random.shuffle(group)
#列印數列
print(group)
#列印數列(1~12)
print(group[0:12])
#列印數列(13~24)
print(group[12:24])
#列印數列(25~36)
print(group[24:36])
#列印數列(37~49)
print(group[36:49])
詳見第二組組員https://s40723147.github.io/cd2020/content/%E4%BA%82%E6%95%B8%E5%88%86%E7%B5%84.html
補充說明
shuffle(洗牌)
import random
number_list = [7,14,21,28,35,42,49,56,63,70]
print("原始列表:",number_list)
random.shuffle(number_list) #shuffle方法
print("第一次洗牌之後的列表:",number_list)
random.shuffle(number_list)
print("第二次洗牌之後列出:",number_list)
原始列表: [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
first shuffle:之後的列表: [35, 28, 70, 14, 21, 63, 7, 49, 42, 56]
第二次洗牌之後列出: [21, 7, 70, 28, 56, 14, 63, 42, 35, 49]
原文網址:https://kknews.cc/code/z9vx6zg.html
我們可以從這了解到shuffle是一個將數列重新排列的方式
接著我們利用
#列印數列
print(group)
#列印數列(1~12)
print(group[0:12])
#列印數列(13~24)
print(group[12:24])
#列印數列(25~36)
print(group[24:36])
#列印數列(37~49)
print(group[36:49])
的方式可以以不重複的方法分出4組組員
變數
「變數」在處理資料上是非常重要的元素,它可以用來儲存我們要處理的資料,也可以用來儲存處理之後的資料,等到資料都處理完畢之後,可以隨時取出使用。
其實,變數就是在電腦記憶體裡面的一個位置空間,早期的電腦是以記憶體的位置編號(如:00A0:FFEC這樣子的型式)來表示,但是這些數字對人來說沒有意義,所以在程式語言中才會以一些英文字元、數字元以及一些符號來代替某些位置,以方便我們在程式中使用。
week4~5 meeting
這周我們分配了翻譯工作
我是負責翻譯機械設計過程MechanicalDesignProcess的一員
WEEK1~5 <<
Previous Next >> WEEK6~9