我需要計算彼此相等的元素對的數量
list1 = input("Введите список: ")
list2 = list1.split()
list3 = []
counter = 0
for i in range(0, (len(list2))):
element = list2[i]
if element == "":
pass
elif list2.count(element)//2 >= 1:
counter = list2.count(element)//2
for a in range(0, (len(list2))):
if list2[a] != element:
list2 = list3.append(list2[a])
else:
list2 = list3.append("")
else:
pass
print(list2)
print(counter)
錯誤:
if list2[a] != element:
TypeError: 'NoneType' object is not subscriptable
我不明白它想從我這里得到什么以及我做錯了什么
uj5u.com熱心網友回復:
代碼也可以如下:
words = input("Введите список: ").split()
list3 = []
counter = 0
counted_words = set()
for element in words:
if element != "" and element not in counted_words:
counted_words.add(element)
c = words.count(element) // 2
if c >= 1:
counter = c
list3.extend(['' if w == element else w for w in words])
print(list3)
print(counter)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/340982.html
