izz*_*eee -1 python element list-comprehension list
这里有 3 个列表:
\n\ntest1 = [\'a\',\'a\',\'a\',\'a\',\'a\']\ntest2 = [\'b\',\'b,\'b\']\ntest3 = [\'c\',\'c\',\'c\',\'c\',\'c\',\'c\',\'c\',\'c\']\nRun Code Online (Sandbox Code Playgroud)\n\n如何输出包含最多元素的列表?下面的解决方案来自@Rifat Alptekin \xc3\x87etin:Python:如何找到其中包含最多元素的列表,但这会输出由字典定义的“字符串”。如何输出包含最多元素的列表?
\n\nlists=[test1, test2, test3]\nlistnames=["list1", "list2", "list3"]\n\nmost = listnames[np.argmax([len(l) for l in lists])]\nRun Code Online (Sandbox Code Playgroud)\n\n电流输出
\n\nmost = \'list3\'\nRun Code Online (Sandbox Code Playgroud)\n\n所需输出
\n\nmost = test3\nRun Code Online (Sandbox Code Playgroud)\n
>>> test1 = ['a','a','a','a','a']
>>> test2 = ['b', 'b', 'b']
>>> test3 = ['c','c','c','c','c','c','c','c']
>>> lists = [test1, test2, test3]
>>> max(lists, key=len)
['c', 'c', 'c', 'c', 'c', 'c', 'c', 'c']
Run Code Online (Sandbox Code Playgroud)
这为您提供了实际的列表,而不是您正在讨论的字符串名称
>>> assert max(lists, key=len) == test3
>>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
199 次 |
| 最近记录: |