54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
def main():
|
|
from random import randint
|
|
print('==Exercise 4==')
|
|
LIST_A = [1,2.0,'c',{"id":4}]
|
|
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])
|
|
|
|
popped_item = LIST_C.pop(3)
|
|
print(f'Deleted 4th item(index=3)={popped_item} from list C: {LIST_C}')
|
|
|
|
LIST_C.insert(5,LIST_A)
|
|
print(f'Insert items from list A to list C start from pos5(index=4): {LIST_A}')
|
|
|
|
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
|
|
|
|
if __name__ == '__main__':
|
|
main() |