求助 求助!!
大佬们 帮我看看这题怎么做 所以你的代码呢? 傻眼貓咪 发表于 2023-2-25 19:02所以你的代码呢?
就是不会打才求助啊 最好自己做{:10_264:} 本帖最后由 chinajz 于 2023-2-25 23:10 编辑
def get_counts(x):
counts={}
for n in x:
if n in counts:
counts += 1
else:
counts = 1
return counts
#words = input("input words: ").split()
words = ' key1 key1 key2 key3 value2 key1 key2 key1 value1 value3 key1'.split()
print ('words:',words)
valuex = ''
for i in range (len(words)):
a = words
b = words.strip('k')
if a is not b :
valuex += a + ','
value1 =valuex[:-1].split(',')
print ("value1=",value1)
value2 =get_counts(value1)
print('value2=',value2)
n = 0
swap = ''
for i in value2:
m = int(value2)
if n <= m:
n= value2
swap = i
print('max_key_value : ',swap,':',n)
运行结果:
#input words: key1 key1 key2 key3 value2 key1 key2 key1 value1 value3 key1
words: ['key1', 'key1', 'key2', 'key3', 'value2', 'key1', 'key2', 'key1', 'value1', 'value3', 'key1']
value1= ['key1', 'key1', 'key2', 'key3', 'key1', 'key2', 'key1', 'key1']
value2= {'key1': 5, 'key2': 2, 'key3': 1}
max_key_value :key1 : 5 江山是个艺术家 发表于 2023-2-25 20:53
就是不会打才求助啊
这题其实不难,建议下次自己摸索摸索,自己先试试看,不明白才可以这里发问
{:10_261:}
题解:strings = input().split()
LIST = list()
print('=' * 10) # 这是分割线
for each in strings:
if each.startswith('k'):
print(each) # <--------------------------------- (1)
LIST.append(each) # <--------------------------- (1)
print('=' * 10) # 这是分割线
SET = set(strings) # <---------------------------------- (2)
print(len(SET)) # <------------------------------------- (2)
print('=' * 10) # 这是分割线
DICT = dict()
for each in SET:
DICT = LIST.count(each) # <------------------- (3)
MAX = max(DICT.items(), key = lambda elem: elem)# <-- (3)
print(f"{MAX}:{MAX}") # <------------------------- (3)key1 key1 key2 key3 value2 key1 key2 key1 value1 value3 key1==========
key1
key1
key2
key3
key1
key2
key1
key1
==========
6
==========
key1:5 本帖最后由 元豪 于 2023-2-26 12:40 编辑
def t(a): # 用于查找出现次数最多的元素的函数
y = {} # 字典储存元素和次数
for i in a:
y = i # 储存
o = list(y) # 列表化
o.sort() # 排序
return y] # 返回出现次数最多的元素
n = input().split()
x = set(n) # 集合化
print('=' * 10)
== 'k'] # 打印k的元素
print('=' * 10)
print(len(x)) # 打印长度
print('=' * 10)
print('{}:{}'.format(y := t(n), n.count(y))) # 打印次数最多的元素和次数
页:
[1]