[英]How to find if a word occurs twice, ad a number occurs once in a tuple
data = ((45, 'foot'), (21, 'basket'), (10, 'hand'), (24, 'foot'), (21, 'hand'))
def unique_data_items(data):
input data is made of ((int, string), (int, string), ...)
unique_nums = () #initialising the tuple
unique_words = ()
Add code to fill the tuples unique_nums and unique_words with
numbers and words that are unique
returns the pair (tuple) of the numbers of unique numbers and
words
我如何完成代碼,所以它可以返回出現一次的單詞和出現兩次的數字,我已經嘗試過,但不知道怎么做,謝謝
d = {}
for x,y in data:
if y not in d:
d[y] = x
else:
d[y] += x
unique = tuple(d.items())
我認為您想要的 output 代碼是:
(('foot', 69), ('basket', 21), ('hand', 31))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.