Incase,OP正在尋找一個dictionary
(想想h e是),只是做:
>>> import random
>>> d = {}
>>> for key in set(wordlist): #note use of `set`
d[key] = random.randint(0,1000) #the (beg,end) value is your choice
>>> d
=> {'apple': 816, 'mango': 342, 'banana': 231, 'durian': 765, 'cherry': 186}
否則,如果他想在tuple
與值列表,只是做:
>>> l = []
>>> for key in wordlist:
l.append((key,random.randint(0,1000)))
>>> l
=> [('apple', 645), ('durian', 4), ('banana', 451), ('durian', 550), ('apple', 772),
('cherry', 800), ('cherry', 972), ('mango', 448), ('apple', 783), ('apple', 433),
('cherry', 733), ('durian', 210), ('banana', 656), ('apple', 196), ('apple', 25),
('apple', 395), ('apple', 98), ('banana', 589), ('apple', 695)]
#driver值
IN : wordlist = ["apple","durian","banana","durian","apple","cherry",
"cherry","mango","apple","apple","cherry","durian","banana",
"apple","apple","apple","apple","banana","apple"]
什麼*轉換爲Python *意味着什麼?你擁有的'list'已經是一個有效的Python'list'。不需要轉換。你的問題總體上很不好解釋。 –
你是什麼意思'我想轉換成python'? – Harman
Think OP意思是'dictionary' –