Fix: week4 ex4

This commit is contained in:
2026-03-23 23:02:45 +08:00
parent e1d2d137c5
commit df970263d1
2 changed files with 47 additions and 3 deletions

View File

@@ -2,15 +2,26 @@ def main():
from random import randint
print('==Exercise 4==')
LIST_A = [1,2.0,'c',{"id":4}]
LIST_B = [randint(1,100) for i in range(10)]
LIST_B = [randint(1,20) for i in range(10)]
LIST_C = [*[chr(i+65) for i in range(10)],27,28.0]
print('Lists Initial Values:')
"""
updated on Mar 23th 23:00
课上写的有点问题,重写了
print(f'{i["idtf"]}: {i["value"]}' for i in [
{"idtf":"List A","value":LIST_A},
{"idtf":"List B","value":LIST_B},
{"idtf":"List C","value":LIST_C},
])
"""
print(*[f'{i["idtf"]}: {i["value"]}' for i in [
{"idtf":"List A","value":LIST_A},
{"idtf":"List B","value":LIST_B},
{"idtf":"List C","value":LIST_C},
]],sep='\n')
print('a range of items: ',LIST_C[:-3])
print('reverse print of list: ',LIST_C[::-1])
@@ -23,8 +34,18 @@ def main():
LIST_B.sort()
print('sorting List B:',LIST_B)
"""
updated on Mar 23th 22:54
课上写的有点问题,重写了
print('number of similar items in the list B: ',sum([1 if LIST_B[i] in LIST_B[i+1:] else 0 for i in range(len(LIST_B))]))
"""
TEMP_DICT_SIMCOUNTER = {}
for i in LIST_B:
if i in TEMP_DICT_SIMCOUNTER: TEMP_DICT_SIMCOUNTER[i]+=1
else: TEMP_DICT_SIMCOUNTER[i]=1
print('number of similar items in the list B: ',*[f'{i}:{TEMP_DICT_SIMCOUNTER[i]} times ' if TEMP_DICT_SIMCOUNTER[i]>1 else "" for i in TEMP_DICT_SIMCOUNTER.keys()],sep='')
print('==End Of Ex4==')
return 0