想找可以在家做的手工活去什么网站,中国画廊企业网站模板,台州市建设工程质量检测中心网站,设计公司资质申请在本文中#xff0c;我们将学习如何根据主列表中存在的子列表的第二个元素对任何列表进行排序。
比如 Input : [[‘rishav’, 10], [‘akash’, 5], [‘ram’, 20], [‘gaurav’, 15]] Output : [[‘akash’, 5], [‘rishav’, 10], [‘gaurav’, 15], [‘ram’, 20]] Input …在本文中我们将学习如何根据主列表中存在的子列表的第二个元素对任何列表进行排序。
比如 Input : [[‘rishav’, 10], [‘akash’, 5], [‘ram’, 20], [‘gaurav’, 15]] Output : [[‘akash’, 5], [‘rishav’, 10], [‘gaurav’, 15], [‘ram’, 20]] Input : [[‘452’, 10], [‘256’, 5], [‘100’, 20], [‘135’, 15]] Output : [[‘256’, 5], [‘452’, 10], [‘135’, 15], [‘100’, 20]] 方法1使用冒泡排序
这里我们使用了冒泡排序来执行排序。尝试使用嵌套循环访问子列表的第二个元素这将执行就地排序方法。时间复杂度类似于冒泡排序即时间复杂度为On^2。
def Sort(sub_li):l len(sub_li)for i in range(0, l):for j in range(0, l-i-1):if (sub_li[j][1] sub_li[j 1][1]):tempo sub_li[j]sub_li[j] sub_li[j 1]sub_li[j 1] temporeturn sub_li# Input list
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]# Printing the list
print(Sort(sub_li))输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]方法2使用sort()方法
当通过该方法排序时元组的实际内容被改变并且就像前面的方法一样执行就地排序。
def Sort(sub_li):# reverse None (Sorts in Ascending order)# key is set to sort using second element of# sublist lambda has been usedsub_li.sort(key lambda x: x[1])return sub_li# Input list
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]# Printing the sub list
print(Sort(sub_li))输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]时间复杂度On*logn
方法3使用sorted()方法进行
sorted()对列表进行排序并始终返回一个包含元素的列表而不修改原始序列。
def Sort(sub_li):# reverse None (Sorts in Ascending order)# key is set to sort using second element of# sublist lambda has been usedreturn (sorted(sub_li, keylambda x: x[1]))# Input list
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]# Printing resultant list
print(Sort(sub_li))输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]方法4 使用OrderedDict
from collections import OrderedDictdef Sort(sub_li):# create an ordered dictionarysub_li_dict OrderedDict()for i in sub_li:sub_li_dict[i[1]] i# sorting the dictionary by keysorted_dict sorted(sub_li_dict.items())# extracting the values from the sorted dictionarysort_sub_li [value for key, value in sorted_dict]return sort_sub_li# Driver Code
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]
print(Sort(sub_li))输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]上述方法实现了将子列表元素存储为键值对的有序字典。然后字典按键排序并返回列表其中元素根据子列表中的第二个元素排序。
方法5 使用operator模块中的itemgetter()
from operator import itemgetterdef sort_tuples(sub_li):# itemgetter(1) returns a function that can be used to retrieve the# second element of a tuple (i.e., the element at index 1)# this function is used as the key for sorting the sublistsreturn sorted(sub_li, keyitemgetter(1))# Input list
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]# Printing resultant list
print(sort_tuples(sub_li))
输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]方法6 使用numpy的argsort()
import numpy as np# Define the input list
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]# Converting the list to a NumPy array
sub_arr np.array(sub_li)# Extracting the second column and convert it to integers
values sub_arr[:, 1].astype(int)# Sort the array by the second column (index 1)
sorted_arr sub_arr[values.argsort()]# Converting the sorted array back to a list
sorted_li sorted_arr.tolist()# Printing sorted list
print(sorted_li)输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]方法7 使用递归
定义一个merge函数它接收两个列表left和right并将它们合并同时根据子列表的第二个元素进行排序。在merge函数中初始化一个空列表result来存储合并后的和排序后的子列表。将两个指针i和j初始化为0分别表示left和right中的当前索引。比较子列表中left[i]和right[j]的第二个元素。如果left[i]的第二个元素小于或等于right[j]的第二个元素则将left[i]追加到result并递增i。否则将right[j]追加到result并递增j。重复步骤4-6直到left或right用尽为止。将left和right中的剩余元素附加到result如果有的话。返回result作为合并和排序的列表。
def merge(left, right):# Empty list to store merge and sorted listresult []i 0j 0while i len(left) and j len(right):if left[i][1] right[j][1]:result.append(left[i])i 1else:result.append(right[j])j 1result.extend(left[i:])result.extend(right[j:])return result# Recursive function to sort sub list
def sort_recursive(sub_li):if len(sub_li) 1:return sub_limid len(sub_li) // 2left sub_li[:mid]right sub_li[mid:]left sort_recursive(left)right sort_recursive(right)return merge(left, right)# Input sub list
sub_li [[rishav, 10], [akash, 5], [ram, 20], [gaurav, 15]]# Calling function and printing sub list
print(sort_recursive(sub_li))输出
[[akash, 5], [rishav, 10], [gaurav, 15], [ram, 20]]这个算法的时间复杂度是On log n其中n是输入列表sub_li的长度。这是因为列表被递归地分成两半直到达到单个元素或空列表的基本情况然后在排序时合并回递归树取log n级。在每个级别上合并操作需要时间On。 文章转载自: http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.hwpcm.cn.gov.cn.hwpcm.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.qpqcq.cn.gov.cn.qpqcq.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.gpsrk.cn.gov.cn.gpsrk.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.pwqyd.cn.gov.cn.pwqyd.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn