如何避免从列表中重复打印随机选择的名称(Python)

时间:2014-01-20 16:15:53

标签: python-2.7 duplicate-data

这个程序打印从列表中生成的重复名称请帮我摆脱它我添加了一个运算符fr但它不能正常工作

#Subscriber Selector
import random
print "Welcome to Subscriber Picker"
sub_list = ["Ali Abbas","Ansar Abbasi","Hasan Abidi","Saadia Afzaal","Iqbal       Ahmad","Iftikhar Ahmad","Khaled Ahmed","Ahmed Tamim","Maulana Mahboob Alam","Malik Barkat Ali"]

def add_list():
    input_1 = int(raw_input("How many new users do you want to add? "))
for z in range (0,input_1):
    sub_list.append(raw_input ("Enter Name" +" "+ str(z+1) + ":"))
return
add_list()

def generator():
 input_2=int(raw_input("How many subscribers to generate? "))
 print "-----"
 index=0
 temp_list = []
 ran_name = random.randint(0, len(sub_list)-1)
 temp_list.append(sub_list[ran_name])
 while len(temp_list) < input_2:
      ran_name=random.randint(0,len(sub_list)-1)
      temp_list.append(sub_list[ran_name])
      if(temp_list[index] == temp_list[index+1]):
          temp_list.pop(index)       
      else:
          index = index + 1
 for x in temp_list:
      print x        
 print"-----"
 return

发生器()

1 个答案:

答案 0 :(得分:2)

你走了:

temp_list = random.sample( sub_list, input_2 )