# python - 如何跟踪此列表中的频率？

``````import sys

print ("After inputting this data, the sum, average, maximum and minimum
number will be printed")

temperatureList = list()

weather=int(input("Enter the amount of days that you are looking at for
the weather:"))

print("Enter the high temperature for those next days: ")

for i in range(int(weather)):
k=int(input(""))
temperatureList.append(int(k))
sm=sum(temperatureList)
avg=sm/weather
print("SUM = ",sm)
print("AVERAGE = ",avg)

temperatureList.sort()
print("This is the numbers from low to high", temperatureList)
print("Maximum number in the list is:", max(temperatureList), "and the
minimum number is: ", min(temperatureList))

while True:
action = int(input("Input add if you want to add to the list again. Or
remove if you want to remove from the list, or end if you want to end this
program"))

print("Enter what you want to be added ")
print(temperatureList)
sm = sum(temperatureList)
avg = sm / weather
print("SUM = ", sm)
print("AVERAGE = ", avg)
temperatureList.sort()
print("This is the numbers from low to high", temperatureList)

elif action == remove:
if len(temperatureList) > 1:
del temperatureList[-1]
print(temperatureList)
sm = sum(temperatureList)
avg = sm / weather
print("SUM = ", sm)
print("AVERAGE = ", avg)

else:
print("Everything removed from the list")

elif action == end:
sys.exit()
``````

#### 2 个答案:

``````# Python program to count the frequency of
# elements in a list using a dictionary

def CountFrequency(my_list):

# Creating an empty dictionary
freq = {}
for item in my_list:
if (item in freq):
freq[item]  = 1
else:
freq[item] = 1

for key, value in freq.items():
print ("% d : % d"%(key, value))

# Driver function
if __name__ == "__main__":
my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]

CountFrequency(my_list)
``````

0条回复