获取列表的内容并将其附加到另一个列表

问题:获取列表的内容并将其附加到另一个列表

我试图了解获取列表内容并将其附加到另一个列表是否有意义。

我具有通过循环功能创建的第一个列表,该列表将从文件中获取特定行并将其保存在列表中。

然后使用第二个列表保存这些行,并在另一个文件上开始新的循环。

我的想法是在for循环完成后获取列表,将其转储到第二个列表中,然后开始一个新的循环,将第一个列表的内容再次转储到第二个列表中,但将其追加,因此第二个列表将是我循环中创建的所有较小列表文件的总和。仅在满足某些条件的情况下,才必须附加该列表。

看起来类似于以下内容:

# This is done for each log in my directory, i have a loop running
for logs in mydir:

    for line in mylog:
        #...if the conditions are met
        list1.append(line)

    for item in list1:
        if "string" in item: #if somewhere in the list1 i have a match for a string
            list2.append(list1) # append every line in list1 to list2
            del list1 [:] # delete the content of the list1
            break
        else:
            del list1 [:] # delete the list content and start all over

这有意义还是我应该选择其他路线?

我需要一种效率高,不会占用太多周期的东西,因为日志列表很长而且每个文本文件都很大。所以我认为这些清单符合目的。

I am trying to understand if it makes sense to take the content of a list and append it to another list.

I have the first list created through a loop function, that will get specific lines out of a file and will save them in a list.

Then a second list is used to save these lines, and start a new cycle over another file.

My idea was to get the list once that the for cycle is done, dump it into the second list, then start a new cycle, dump the content of the first list again into the second but appending it, so the second list will be the sum of all the smaller list files created in my loop. The list has to be appended only if certain conditions met.

It looks like something similar to this:

# This is done for each log in my directory, i have a loop running
for logs in mydir:

    for line in mylog:
        #...if the conditions are met
        list1.append(line)

    for item in list1:
        if "string" in item: #if somewhere in the list1 i have a match for a string
            list2.append(list1) # append every line in list1 to list2
            del list1 [:] # delete the content of the list1
            break
        else:
            del list1 [:] # delete the list content and start all over

Does this makes sense or should I go for a different route?

I need something efficient that would not take up too many cycles, since the list of logs is long and each text file is pretty big; so I thought that the lists would fit the purpose.


回答 0

你可能想要

list2.extend(list1)

代替

list2.append(list1)

区别在于:

>>> a = range(5)
>>> b = range(3)
>>> c = range(2)
>>> b.append(a)
>>> b
[0, 1, 2, [0, 1, 2, 3, 4]]
>>> c.extend(a)
>>> c
[0, 1, 0, 1, 2, 3, 4]

由于list.extend()接受任意迭代,因此您也可以替换

for line in mylog:
    list1.append(line)

通过

list1.extend(mylog)

You probably want

list2.extend(list1)

instead of

list2.append(list1)

Here’s the difference:

>>> a = range(5)
>>> b = range(3)
>>> c = range(2)
>>> b.append(a)
>>> b
[0, 1, 2, [0, 1, 2, 3, 4]]
>>> c.extend(a)
>>> c
[0, 1, 0, 1, 2, 3, 4]

Since list.extend() accepts an arbitrary iterable, you can also replace

for line in mylog:
    list1.append(line)

by

list1.extend(mylog)

回答 1

看看itertools.chain,这是一种快速方法,可将许多小列表视为一个大列表(或至少作为一个大可迭代对象)而无需复制较小的列表:

>>> import itertools
>>> p = ['a', 'b', 'c']
>>> q = ['d', 'e', 'f']
>>> r = ['g', 'h', 'i']
>>> for x in itertools.chain(p, q, r):
        print x.upper()

Take a look at itertools.chain for a fast way to treat many small lists as a single big list (or at least as a single big iterable) without copying the smaller lists:

>>> import itertools
>>> p = ['a', 'b', 'c']
>>> q = ['d', 'e', 'f']
>>> r = ['g', 'h', 'i']
>>> for x in itertools.chain(p, q, r):
        print x.upper()

回答 2

对于您要执行的操作,这似乎相当合理。

一个靠Python来完成更多繁重工作的简短版本可能是:

for logs in mydir:

    for line in mylog:
        #...if the conditions are met
        list1.append(line)

    if any(True for line in list1 if "string" in line):
        list2.extend(list1)
    del list1

    ....

(True for line in list1 if "string" in line)在迭代list并发出True每当发现匹配。一旦找到第一个元素,就会any()使用短路评估来返回。将的内容附加到末尾。TrueTruelist2.extend()list1

That seems fairly reasonable for what you’re trying to do.

A slightly shorter version which leans on Python to do more of the heavy lifting might be:

for logs in mydir:

    for line in mylog:
        #...if the conditions are met
        list1.append(line)

    if any(True for line in list1 if "string" in line):
        list2.extend(list1)
    del list1

    ....

The (True for line in list1 if "string" in line) iterates over list and emits True whenever a match is found. any() uses short-circuit evaluation to return True as soon as the first True element is found. list2.extend() appends the contents of list1 to the end.


回答 3

您也可以使用’+’运算符组合两个列表(例如a,b)。例如,

a = [1,2,3,4]
b = [4,5,6,7]
c = a + b

Output:
>>> c
[1, 2, 3, 4, 4, 5, 6, 7]

You can also combine two lists (say a,b) using the ‘+’ operator. For example,

a = [1,2,3,4]
b = [4,5,6,7]
c = a + b

Output:
>>> c
[1, 2, 3, 4, 4, 5, 6, 7]

回答 4

回顾以前的答案。如果您有一个清单,[0,1,2]而另一个清单是,[3,4,5]并且您想将它们合并,则它变成[0,1,2,3,4,5],您可以使用chainingextending并且应该知道区别,以便根据需要明智地使用它。

扩展清单

使用listclasses extend方法,可以将一个列表中的元素复制到另一个列表中。但是,这将导致额外的内存使用,这在大多数情况下应该没问题,但如果要提高内存效率,可能会导致问题。

a = [0,1,2]
b = [3,4,5]
a.extend(b)
>>[0,1,2,3,4,5]

链表

相反,您可以用来itertools.chain连接许多列表,这将返回一个iterator可用于迭代列表的所谓的列表。这将提高内存效率,因为它不会复制元素,而只是指向下一个列表。

import itertools
a = [0,1,2]
b = [3,4,5]
c = itertools.chain(a, b)

创建一个迭代器,该迭代器从第一个可迭代对象返回元素,直到耗尽为止,然后继续进行下一个可迭代对象,直到所有可迭代对象都耗尽为止。用于将连续序列视为单个序列。

To recap on the previous answers. If you have a list with [0,1,2] and another one with [3,4,5] and you want to merge them, so it becomes [0,1,2,3,4,5], you can either use chaining or extending and should know the differences to use it wisely for your needs.

Extending a list

Using the list classes extend method, you can do a copy of the elements from one list onto another. However this will cause extra memory usage, which should be fine in most cases, but might cause problems if you want to be memory efficient.

a = [0,1,2]
b = [3,4,5]
a.extend(b)
>>[0,1,2,3,4,5]

Chaining a list

Contrary you can use itertools.chain to wire many lists, which will return a so called iterator that can be used to iterate over the lists. This is more memory efficient as it is not copying elements over but just pointing to the next list.

import itertools
a = [0,1,2]
b = [3,4,5]
c = itertools.chain(a, b)

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.


回答 5

使用map()reduce()内置功能

def file_to_list(file):
     #stuff to parse file to a list
     return list

files = [...list of files...]

L = map(file_to_list, files)

flat_L = reduce(lambda x,y:x+y, L)

最小的“用于循环”和优雅的编码模式:)

Using the map() and reduce() built-in functions

def file_to_list(file):
     #stuff to parse file to a list
     return list

files = [...list of files...]

L = map(file_to_list, files)

flat_L = reduce(lambda x,y:x+y, L)

Minimal “for looping” and elegant coding pattern :)


回答 6

如果我们有如下列表:

list  = [2,2,3,4]

将其复制到另一个列表的两种方法。

1。

x = [list]  # x =[] x.append(list) same 
print("length is {}".format(len(x)))
for i in x:
    print(i)
length is 1
[2, 2, 3, 4]

2。

x = [l for l in list]
print("length is {}".format(len(x)))
for i in x:
    print(i)
length is 4
2
2
3
4

If we have list like below:

list  = [2,2,3,4]

two ways to copy it into another list.

1.

x = [list]  # x =[] x.append(list) same 
print("length is {}".format(len(x)))
for i in x:
    print(i)
length is 1
[2, 2, 3, 4]

2.

x = [l for l in list]
print("length is {}".format(len(x)))
for i in x:
    print(i)
length is 4
2
2
3
4