如何对大量列表进行排序以获得最长列表中的前10个

时间:2016-04-27 14:04:26

标签: python list

所以我有一个包含大约400,000个列表的文本文件,大部分都是这样的。

100005  127545  202036  257630  362970  376927  429080
10001   27638   51569   88226   116422  126227  159947  162938  184977  188045
191044  246142  265214  290507  296858  300258  341525  348922  359832  365744
382502  390538  410857  433453  479170  489980  540746
10001   27638   51569   88226   116422  126227  159947  162938  184977  188045
191044  246142  265214  290507  300258  341525  348922  359832  365744  382502

到目前为止,我有一个逐行排列的for循环,并将当前行转换为临时数组列表。

如何创建包含整个文件中元素最多的列表的前十个列表。

这是我现在的代码。

file = open('node.txt', 'r')

adj = {}
top_ten = []
at_least_3 = 0

for line in file:
    data = line.split()
    adj[data[0]] = data[1:]

这就是列表之一

['99995', '110038', '330533', '333808', '344852', '376948', '470766', '499315']

5 个答案:

答案 0 :(得分:2)

# collect the lines
lines = []

with open("so.txt") as f:
    for line in f:
        # split each line into a list
        lines.append(line.split())

# sort the lines by length, descending
lines = sorted(lines, key=lambda x: -len(x))

# print the first 10 lines
print(lines[:10])

答案 1 :(得分:1)

为什么不使用collections来显示前10名呢?即:

import re
import collections

file = open('numbers.txt', 'r')
content = file.read()
numbers = re.findall(r"\d+", content)
counter = collections.Counter(numbers)
print(counter.most_common(10))

Ideone Demo

答案 2 :(得分:1)

如果想要计算并找到计数最高的那个,我会想到collections.Counter

public class LispListTester
{
public static void main(String[] args)
{
    LispList list1 = new EmptyList();
    System.out.println("[" + list1 + "]");
    System.out.println("Expected: []");

    LispList list2 = new NonEmptyList("A", new EmptyList());
    System.out.println(list2);
    System.out.println("Expected: A");

    LispList list3 = new NonEmptyList("A", new NonEmptyList("B",
            new NonEmptyList("C", new EmptyList())));
    System.out.println(list3);
    System.out.println("Expected: A B C");

    LispList list4 =    LispList.NIL.cons("E").cons("D").cons("C").cons("B").cons("A");
    System.out.println(list4);
    System.out.println("Expected: A B C D E");
    }
}

输出(使用样本文件数据):

from collections import Counter

lists = Counter()
with open('node.txt', 'r') as file:
    for line in file:
        values = line.split()
        lists[tuple(values)] = len(values)

print('Length  Data')
print('======  ====')
for values, length in lists.most_common(10):
    print('{:2d}      {}'.format(length, list(values)))

答案 3 :(得分:0)

使用for循环和max()可能吗?你说你有一个for循环,它将值放入临时数组中。从那里你可以使用“max()”来挑选最大值并将其放入列表中。

作为一个开放的for循环,类似于将max()附加到新列表:

newlist = []

for x in data:
    largest = max(x)
    newlist.append(largest)

或者作为列表理解:

newlist = [max(x) for x in data]

然后,你必须在新列表上执行相同的过程,直到达到所需的前10个场景。

编辑:我刚刚意识到我误解了你的问题。您希望获得具有最多元素的列表,而不是最高值。确定。

len()对此很好。

for x in data:
    if len(templist) > x:
        newlist.append(templist)

这会给你当前最高的,你可以从那里创建一个前10个长度列表或临时列表本身,或两者。

答案 4 :(得分:0)

如果您的数据真的与每个数字长度相同,那么我会创建一个字典,其中key = line,value = length,获取字典中的顶部值/密钥对,然后瞧。听起来很容易。

相关问题