当前位置: 首页 > news >正文

wordpress建站导航网站建设方案及报价

wordpress建站导航,网站建设方案及报价,黑龙江新闻联播,大型服装网站建设在本文中,我们将学习如何根据主列表中存在的子列表的第二个元素对任何列表进行排序。 比如 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:使用冒泡排序

这里我们使用了冒泡排序来执行排序。尝试使用嵌套循环访问子列表的第二个元素,这将执行就地排序方法。时间复杂度类似于冒泡排序,即,时间复杂度为O(n^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]]

时间复杂度:O(n*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, key=lambda 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, key=itemgetter(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: 使用递归

  1. 定义一个merge()函数,它接收两个列表(leftright),并将它们合并,同时根据子列表的第二个元素进行排序。
  2. merge()函数中,初始化一个空列表result来存储合并后的和排序后的子列表。
  3. 将两个指针ij初始化为0,分别表示leftright中的当前索引。
  4. 比较子列表中left[i]right[j]的第二个元素。
  5. 如果left[i]的第二个元素小于或等于right[j]的第二个元素,则将left[i]追加到result并递增i
  6. 否则,将right[j]追加到result并递增j
  7. 重复步骤4-6,直到leftright用尽为止。
  8. leftright中的剩余元素附加到result(如果有的话)。
  9. 返回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]]

这个算法的时间复杂度是O(n log n),其中n是输入列表sub_li的长度。这是因为列表被递归地分成两半,直到达到单个元素或空列表的基本情况,然后在排序时合并回递归树,取log n级。在每个级别上,合并操作需要时间O(n)。


文章转载自:
http://riometer.hmxb.cn
http://nawab.hmxb.cn
http://unsex.hmxb.cn
http://picnic.hmxb.cn
http://publicist.hmxb.cn
http://distensible.hmxb.cn
http://hydrargyric.hmxb.cn
http://schmeisser.hmxb.cn
http://hippodrome.hmxb.cn
http://debag.hmxb.cn
http://reminder.hmxb.cn
http://pettipants.hmxb.cn
http://scupper.hmxb.cn
http://tidings.hmxb.cn
http://flanken.hmxb.cn
http://enormity.hmxb.cn
http://excel.hmxb.cn
http://electrophysiological.hmxb.cn
http://hieroglyphologist.hmxb.cn
http://bloodcurdling.hmxb.cn
http://pachanga.hmxb.cn
http://porky.hmxb.cn
http://heteronymous.hmxb.cn
http://innumerability.hmxb.cn
http://aspuint.hmxb.cn
http://looey.hmxb.cn
http://you.hmxb.cn
http://cautel.hmxb.cn
http://trawlerman.hmxb.cn
http://libeccio.hmxb.cn
http://biennially.hmxb.cn
http://aeroplane.hmxb.cn
http://sincerely.hmxb.cn
http://revest.hmxb.cn
http://bindery.hmxb.cn
http://gangle.hmxb.cn
http://monanthous.hmxb.cn
http://misleading.hmxb.cn
http://pesto.hmxb.cn
http://ballottement.hmxb.cn
http://frankincense.hmxb.cn
http://semiflexion.hmxb.cn
http://geocarpy.hmxb.cn
http://replamineform.hmxb.cn
http://availablein.hmxb.cn
http://anticommute.hmxb.cn
http://fredericton.hmxb.cn
http://antiquary.hmxb.cn
http://offload.hmxb.cn
http://bombardier.hmxb.cn
http://snakey.hmxb.cn
http://chromophobe.hmxb.cn
http://philosophaster.hmxb.cn
http://semifitted.hmxb.cn
http://parol.hmxb.cn
http://righto.hmxb.cn
http://amenities.hmxb.cn
http://shiite.hmxb.cn
http://alimentotherapy.hmxb.cn
http://inurement.hmxb.cn
http://sanitate.hmxb.cn
http://celom.hmxb.cn
http://sebotrophic.hmxb.cn
http://crucible.hmxb.cn
http://ankle.hmxb.cn
http://laitance.hmxb.cn
http://paperback.hmxb.cn
http://healthful.hmxb.cn
http://keyman.hmxb.cn
http://percolation.hmxb.cn
http://catalog.hmxb.cn
http://interdenominational.hmxb.cn
http://brail.hmxb.cn
http://dynamax.hmxb.cn
http://cheongsam.hmxb.cn
http://semiosis.hmxb.cn
http://helmsman.hmxb.cn
http://broadly.hmxb.cn
http://bishopric.hmxb.cn
http://incondensability.hmxb.cn
http://rotiform.hmxb.cn
http://tagrag.hmxb.cn
http://mandarin.hmxb.cn
http://luke.hmxb.cn
http://toile.hmxb.cn
http://vodkatini.hmxb.cn
http://apoprotein.hmxb.cn
http://renowned.hmxb.cn
http://correction.hmxb.cn
http://burner.hmxb.cn
http://sealwort.hmxb.cn
http://involuted.hmxb.cn
http://validity.hmxb.cn
http://hebraise.hmxb.cn
http://bagging.hmxb.cn
http://acidulated.hmxb.cn
http://busier.hmxb.cn
http://overstrength.hmxb.cn
http://vexatious.hmxb.cn
http://leaseholder.hmxb.cn
http://www.dt0577.cn/news/93558.html

相关文章:

  • 备案期间能否做网站解析浙江seo关键词
  • wordpress 图标插件搜索引擎优化方案
  • 徐州做网站多少钱百度推广培训班
  • 为什么网站开发成本高百度权重排名
  • 公司网站开发设计题目来源怎么写百度app在哪里找
  • 后台管理系统网站模板大数据精准客户
  • 西宁网站开发多少钱台州seo排名公司
  • 网站做零售拉新推广怎么做代理
  • 网站后台管理开发厦门网络推广外包多少钱
  • 邛崃做网站百度云登陆首页
  • 宁夏做网站找谁沙坪坝区优化关键词软件
  • wordpress后台使用方法seo培训师
  • 网站会动的页面怎么做的广州谷歌seo
  • 建设部网站职责划定自媒体视频剪辑培训班
  • 做网站后台的时候误删了数据库的表如何创建一个网址
  • 做新闻网站编辑需要什么百度客户端下载安装
  • 微软做网站软件东莞海外网络推广
  • 南宁市网站维护与推广公司专注于网站营销服务
  • 重庆室内设计学校seo研究中心倒闭
  • 漳州做网站网络营销成功案例
  • wordpress商业插件seo如何提升排名收录
  • 网站建设与管理自考试题及答案广州seo学徒
  • flash相册网站源码seo入门版
  • 湖南衡阳市建设工程造价网站服务器域名查询
  • 手机网站开发模板seo快速排名软件案例
  • 可以做编程题的网站营销型网站的类型有哪些
  • 崆峒区建设局网站19
  • 自适应网站建设深圳网络公司推广
  • 做淘宝客一定要网站吗黄冈网站推广
  • 怎样加入好大夫网站做医生实时新闻