Python:按升序对列表进行排序并将其拆分

时间:2019-03-22 08:08:30

标签: python

例如:我有一个列表,它是:

[0,15,1,7,2,16]

我将需要将该列表输出到

[(1,7),(0,15),(2,16)]

因此,从头开始每隔两个词进行拆分,并且子列表的第二个词也按升序排列。

除了导入数学之外,是否有一种无需使用任何python库函数即可执行此操作的方法?

5 个答案:

答案 0 :(得分:1)

具有列表理解和排序方法

l1 = [0,15,1,7,2,16]
def on_sec(v):
    return v[1]
print(sorted ([(l1[e], l1[e+1]) for e in range(0, len(l1), 2)], key=on_sec))

# OUTPUT: [(1, 7), (0, 15), (2, 16)]

答案 1 :(得分:0)

尝试一下:

a = [0,15,1,7,2,16]
b = sorted([a[n:n+2] for n in range(0, len(a), 2)], key = lambda x: x[1])
print(b)    # OUTPUT : [[1, 7], [0, 15], [2, 16]]

答案 2 :(得分:0)

Numpy对于此类拆分操作非常有用。我们可以使用reshape将其分成几对,然后根据第二个元素进行排序。

import numpy as np

l = [0,15,1,7,2,16]
split = np.reshape(l, (-1,2))
sorted(split, key = lambda x: x[1])

或者,我们也可以使用numpy进行排序:

split[split[:,1].argsort()]

答案 3 :(得分:0)

a = [0,15,1,7,2,16] 
l = [] 
for i in range(0,len(a),2): 
    c = (a[i],a[i+1]) 
    l.append(c) 

l.sort() 
print(l)

答案 4 :(得分:0)

您可以通过以下键使用函数l = [0, 15, 1, 7, 2, 16] sorted(zip(l[::2], l[1::2]), key=lambda x: x[1]) # [(1, 7), (0, 15), (2, 16)]

<div class="row">
    <div class="col-lg-4">
        <form action="http://laravel-site.test/account/addresses-radio/4" method="post">
            <input type="hidden" name="_token" value="6cR6PuL173IvCG1dXwEDwybbI5BFxtMlbAeXfPwz">                                        
            <div class="form-check">
                <input type="radio" class="form-check-input" name="address_option" id="address_4" checked="">
                <label for="address_4" class="form-check-label">
                    Compex 1 <br>
                    Address 1 <br>
                    Suburb 1 <br>
                    City 1 <br>
                    Provice 1 <br>
                    Code 1 <br>
                </label>
            </div>
        </form>

        <div class="row">
            <div class="col-lg-1">
                <div class="edit-button">
                    <a href="http://laravel-site.test/account/create-address" class="btn btn-primary">Add Address</a>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <form action="http://laravel-site.test/account/addresses-radio/5" method="post">
            <input type="hidden" name="_token" value="6cR6PuL173IvCG1dXwEDwybbI5BFxtMlbAeXfPwz">                                        
            <div class="form-check">
                <input type="radio" class="form-check-input" name="address_option" id="address_5">
                <label for="address_5" class="form-check-label">
                    Address 2 <br>
                    Suburb 2 <br>
                    City 2 <br>
                    Provice 2 <br>
                    Code 2 <br>
                </label>
            </div>
        </form>

        <div class="row">
            <div class="col-lg-1">
                <div class="edit-button">
                    <a href="http://laravel-site.test/account/create-address" class="btn btn-primary">Add Address</a>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <form action="http://laravel-site.test/account/addresses-radio/6" method="post">
            <input type="hidden" name="_token" value="6cR6PuL173IvCG1dXwEDwybbI5BFxtMlbAeXfPwz">                                        
            <div class="form-check">
                <input type="radio" class="form-check-input" name="address_option" id="address_6">
                <label for="address_6" class="form-check->label">
                    Compex 3 <br>
                    Address 3 <br>
                    Suburb 3 <br>
                    City 3 <br>
                    Provice 3 <br>
                    Code 3 <br>
                </label>
            </div>
        </form>

        <div class="row">
            <div class="col-lg-1">
                <div class="edit-button">
                    <a href="http://laravel-site.test/account/create-address" class="btn btn-primary">Add Address</a>
                </div>
            </div>
        </div>
    </div>
</div>