- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以,我正在阅读《从 Python 开始》这本书。我到了一个地步,没有教我任何排序算法,一个练习要求我按以下方式对包含日期和价格的文本文件进行排序。
年年月日:价格
即
04-05-1993:1.068
04-12-1993:1.079
04-19-1993:1.079
04-26-1993:1.086
05-03-1993:1.086
在我的沮丧中,我创建了两个列表。一个包含日期,一个包含价格。
然后我继续使用我想出的以下函数算法对它们进行排序。
我正在从列表中找到最低价格的索引,我将价格添加到 min_to_max_prices_algorithms。
def sort_algorithm(dates,prices):
# Create two lists for storing the results.
min_to_max_prices = []
min_to_max_dates = []
# List I will use to change the prices from str to float.
price_list = []
# Set the date_list to dates
date_list = dates
# Change the prices to float numbers.
for index in range(len(prices)):
price_list.append(float(prices[index]))
# Algorithm
for count in range(len(price_list)):
# Find the minimum price and append it to min_to_max_prices list.
min_to_max_prices.append(min(price_list))
# Use the index of the minimum price to add the equivalent
# date to min_to_max_dates list.
min_to_max_dates.append(dates[price_list.index(min(price_list))])
# Remove the ones added from the lists we search in.
del date_list[price_list.index(min(price_list))]
del price_list[price_list.index(min(price_list))]
这种算法有名字吗?什么是更好的解决方案?很抱歉,我无法在任何地方找到问题的答案。
最佳答案
这看起来像 Selection Sort ,除非您为结果使用不同的列表,而该算法会就地对列表进行排序(即不需要临时列表)。该算法的工作原理如下:
def selection_sort(lst):
for i in range(len(lst) - 1):
for j in range(i + 1, len(lst)):
if lst[i] > lst[j]:
lst[i], lst[j] = lst[j], lst[i]
因此,基本上,外循环的每次迭代都会在剩余的未排序元素中找到最小元素。
选择排序很简单,但不是一个好的排序算法,因为它的时间复杂度是O(n^2)
。 , 虽然你可以做得和 O(nlogn)
一样好(see some examples on wikipedia)。
关于python - 从 Python 开始 - 练习 8.14 排序算法。这个已经有名字了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292687/
古今中外,人其实最珍惜自己的姓名,分量可谓举足轻重,几乎构成一个人最重要的人生价值载体,同理,一个网站的标题也就像网站的名字一样,标题的定性决定着这个网站的路怎么走,SEO的工作将会围绕着这些进行展
我需要强制执行 LASTNAME/FIRSTNAME Something like Smith/John 这种模式。 字符可以是字母数字(小写/大写)也包括特殊字符,如 ë 等。 图案: 基本上规
我有两个字段:first 和 last 我正在尝试使用 multi_match模糊匹配全名: "multi_match": { "query": name, "fields": [
国王i | 王妃i 青梅。 | 竹马。 我要逆天 | 我要爆炸 以前呢i | 后来呢i 抱紧我i | 深吻我i 心悦 | 钟意 依赖 | 共生 阿怪i | 阿乖i 花岛丶 | 草浅丶
我是Delphi的新手。每次运行事物时,都会出现此错误: adoquery1:未找到参数“名字” procedure TForm1.Button1Click(Sender: TObject); beg
我是 Akka 新手,我正在尝试制作一个简单的项目,即有鱼的水族馆。问题是我不知道如何获取我在这一行中声明的 Actor 的名字 final ActorRef fish1 = system.actor
以下是要求:我。如果有名字、中间名和姓氏,则仅显示名字和姓氏二.如果有第一个和最后一个,则显示第一个和最后一个三.如果只有名字,则直接显示 我有以下代码: String fullName = "fir
我使用此 PHP 正则表达式来检查字段是否包含名称(至少包含名字/姓氏,然后是可选的其他中间名或缩写)的 true/false。 $success = preg_match("/([\x{00c0}-
当使用 django allauth 进行社交帐户注册时,allauth 自动将用户名定义为从 3rdparty 获取的 first_name。因此,例如:如果 facebooks John Cucu
我想尝试在 Javascript 中检测一个人名字的不同部分,并将它们剪掉,以便我可以将它们传递给其他东西。 名称可以以任何格式出现——例如:- 维多利亚·米·莱恩小姐 Victoria C J Lo
这是我读过的教程中的查询 CREATE TABLE Employee ( id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, depa
我从包含用户名的文本框中输入了用户输入 输入可以如下所示: var input = "Doe, John M"; 但是,输入可能要复杂得多。像: var input = "Doe Sr, John M
在 Javascript 中,我尝试将全名(名字、中间名和姓氏)输入到字段中,单击提交按钮后将输出到三个单独的字段:字符长度、中间名和 3缩写。到目前为止,我已经到达字符字段,但我对如何获取两个空格之
我尝试通过 java 解析 XML DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder
formatName 方法应该接受名为 name 的字符串,其中包含格式为“First Last”的名称。你可能会认为字符串中只有一个空格。该方法应该创建并返回一个包含名称的字符串格式为“最后,第一个
我是 node.js 的新手。我正在尝试创建一个注册页面并使用 mysql 将输入的值保存在数据库中。 我可以通过在请求 header 上传递参数来做到这一点。但是,在请求 header 中传递参数始
我需要一个函数来根据以下字段检查重复条目:first_name、last_name 和 tel我尝试过浏览论坛,但似乎找不到帮助。 prepare($sql); $stmt->bind_param('
我有下表: First Name Bryce Marcellin Caroline Kerry Roberto Mary Carol Warren Bonnie Terry L
所以我制作了一个代码让用户输入他们的名字和姓氏;然后交换两个位置。 例如。 Bulota Babi 成为 Babi, Bulota 这是我的代码 public class trial { pu
我想知道你是否有两列让我们说 first name. last name. 您在数据库中存储的内容。 您能否在数据库中创建一个“动态”列“全名”,自动根据名字和姓氏创建姓名? firstname
我是一名优秀的程序员,十分优秀!