假设有一个列表
[1,9,7,3,6]
。我想产生一个新的已排序列表,该列表小于其中一个整数,例如整数为7,则新列表应该是:[1,3,6]
。[1,3,6]
my_list = [1,9,7,3,6]
result = sorted([x for x in my_list if x < 7])
oldList = [1,9,7,3,6]
myInt = 7
newList = sorted([x for x in oldList if x < myInt])
列表有一个排序方法:
old_list = [1,9,7,3,6]
new_list = [x for x in old_list if x < 7]
new_list.sort()
试试这个:
num = #some number
new_list = sorted(old_list)[:sorted(old_list).index(num)]
OR替代方案
num = 7
somelist = [3,6,7,1,2,8,9,4,5,12]
somelist.sort()
somelist[:somelist.index(num)]
OUTPUT:
[0, 1, 2, 3, 4, 5, 6]
sorted([i for i in [1,9,7,3,6] if i < 7])
sorted(i for i in [1,9,7,3,6] if i < 7)
aa = filter(lambda x: x < 7, [1,9,7,3,6])
aa.sort()
print aa
OUTPUT:
[1, 3, 6]
mylist = [11, 16, 9, 6, 3, 15, 1, 18, 7, 10, 13, 5, 12, 2, 0, 4, 19, 14, 17, 8]
[x for x in sorted(mylist) if x<7]
结果:
[0, 1, 2, 3, 4, 5, 6]
new_list = sorted(x for x in old_list if x < my_int)
- Burhan Khalid