字符串列表最大值python

时间:2018-11-06 07:45:26

标签: python list

如何从最大的项目python字符串列表中进行检查,并将其复制到另一个列表中。 我正在尝试这个。但是我没有工作。

cad_1 = ['hello', 'hello', 'ewe', 'uwu']
cad_final = {i,j for i,j in Cad_1 if len(i)>=len(j)}
print(cad_final);          

2 个答案:

答案 0 :(得分:0)

您的列表应如下所示:

week_id     banner      sales   fis_week_id
11          Checker     540     201712
11          S           1088    201712
12          Checker     123     201717
12          S           345     201717
13          Other       444     201720
13          S           556     201720

然后,使用以下命令找到In [1111]: import operator In [1109]: Cad_1=['hello', 'hello', 'ewe', 'uwu'] In [1113]: ans = [] :     在[1114]中:ans.append(max(Cad_1,key = operator.itemgetter(1)))

并写入新列表:max,如下所示     在[1115]中:ans     Out [1115]:['ewe']

答案 1 :(得分:0)

您可以首先创建一个具有相应字符串长度的字典:

my_length_list = {i:len(i) for i in my_list}

然后得到最大的元素

import operator

max_string_len = max(my_length_list.items(), key=operator.itemgetter(1))[0]

将此值写入另一个列表

max_string_list = [max_string_len]