如何在python中将4个列表合并为1

时间:2017-08-20 17:41:03

标签: python

我的代码就像

['0', '1']
['2', '1']
['0', '2']
['1', '3']

和输出就像

[['0', '1'],['2', '1'],['0', '2'],['1', '3']]

我希望将它们变成

{{1}}

有人能帮助我吗?

3 个答案:

答案 0 :(得分:2)

您可以使用>>> cmd = ['ls', '/tmp'] >>> numbers = range(3) >>> itertools.chain(cmd, numbers, ['foo', 'bar']) <itertools.chain object at 0x0000000003943F28> >>> list(itertools.chain(cmd, numbers, ['foo', 'bar'])) ['ls', '/tmp', 0, 1, 2, 'foo', 'bar'] 将列表展平到一个列表中。

testGraph=open(input("Enter a file name:"))
result = []
for line in testGraph:
  result.append(line.split())

但是如果你想要一个列表列表,那么你可以选择附加到列表中。

AppDelegate.FinishedLaunching

答案 1 :(得分:2)

List comprehension,适合您,

testGraph = open(input("Enter a file name:")) 
result = [line.split() for line in testGraph]

执行:

In [29]: testGraph = open('abc.txt')

In [30]: [line.split() for line in testGraph]
Out[30]: [['0', '1'], ['2', '1'], ['0', '2'], ['1', '3']]

答案 2 :(得分:2)

这应该对您的代码进行最小的更改。然后,testGraph=open(input("Enter a file name:")) temp = [] for line in testGraph: temp.append(line.split()) 将拥有您想要的组合列表。

 create or replace FUNCTION  "SEND_MAIL" RETURN varchar2
    AS
     PRAGMA AUTONOMOUS_TRANSACTION;
     kontr_tema varchar2(500);
     komentar varchar2(2000);
     obl varchar2 (100);
     riz varchar2 (1);
     riz_warning varchar2(100);
     last_report_id number;
     vystup varchar2(2601);

    begin

    select RIZIKO into riz from AUDIT_REPORTS
    where REPORT_ID =
    (select max(REPORT_ID) from AUDIT_REPORTS);

    select OBLAST into obl from AUDIT_REPORTS
    where REPORT_ID =
    (select max(REPORT_ID) from AUDIT_REPORTS);

    FOR x in(select OPIS_KONTROLY  from AUDIT_REPORTS
    where REPORT_ID =
    (select max(REPORT_ID) from AUDIT_REPORTS))
    loop
    komentar := ''||x.OPIS_KONTROLY||'<br />';
    end loop;

    FOR x1 in(select KONTROLNA_TEMA  from AUDIT_REPORTS
    where REPORT_ID =
    (select max(REPORT_ID) from AUDIT_REPORTS))
    loop
    kontr_tema := ''||x1.KONTROLNA_TEMA||'<br />';
    end loop;



    vystup := '
    <html>
    <head>

     <style type="text/css">
        div{
          position:relative;
          width:750px;
          border: 1px solid black;
          border-collapse: separate;
        }
        </style>

        <style type="text/css">
        "table,th,td" {
            border: 1px solid black;
            border-collapse: separate;
            width:100% !important;
        }
        </style>

    </head>    
    <body> 

     <div> 

     <table border = "1">

           <tr>
                <th>Oblasť</th>
                <th>Riziko</th>
                <th>Kontrolná téma</th>
                <th>Komentár</th>
             </tr>

             <tr>
                <td>'||obl||'</td>
                <td>'||riz||'</td>
                <td>'||kontr_tema||'</td>
                <td>'||komentar||'</td>
             </tr>      
     </table>   


         </div>

    </body> 
    </html> ';

    return vystup;
    commit;

     end;