sort

Python 高数篇 1

元气小坏坏 提交于 2020-01-15 04:40:49
Python 高数篇 1 零蚀 什么是机器学习 feature和label KNN模型 训练集&测试集 二维空间的KNN 什么是机器学习 机器学习就是对于数学方法的运用,利用数学模型,最总进行数据的预测,事务通过大量的数据进行训练,通过预测解决一个未知的问题。由于机器学习非常依赖硬件的性能,所以很多机器学习的功能一直被搁置到现在才出现。 输入 训练 预测 新数据 模型 历史数据 结果 feature和label feature 特征变量 label 结果标签 收集问题的相关的数据,构造数据模型,建立feature和label关系,根据模型进行预测。 feature function label KNN模型 K Nearest Neighbors基于统计学,概率论的预测模型。 数组处理案例1: import numpy as np data = np . array ( [ [ 111 , 1 ] , [ 152 , 2 ] , [ 23 , 2 ] , [ 241 , 2 ] , [ 193 , 2 ] , [ 152 , 2 ] , [ 374 , 4 ] ] ) # 取第一个 feature = data [ : , 0 ] # 取倒数第一个 label = data [ 0 : , - 1 ] # 计算每个item的距离标准值200的距离 distance = list (

C++ STL sort 函数的用法

99封情书 提交于 2020-01-14 13:39:07
sort 在 STL 库中是排序函数,有时冒泡、选择等 $\mathcal O(n^2)$ 算法会超时时,我们可以使用 STL 中的快速排序函数 $\mathcal O(n \ log \ n)$ 完成排序 sort 在 algorithm 库里面,原型如下: template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); 我们会发现 sort 有两种形式一个有三个参数,一个有两个参数,我们先讲讲两个参数的吧! sort 的前两个参数是起始地址和中止地址 如:sort(a,a+n) 表示对 a[0] ... a[n-1] 排序 代码如下: #include <algorithm> #include <cstdio> using namespace std; int main() { int n,a[1001]; scanf("%d",&n); for (int i =

排序算法(三人组加上快排)

我们两清 提交于 2020-01-14 06:21:56
冒泡排序 冒泡排序(Bubble Sort),是一种 计算机科学 领域的较简单的 排序算法 。 它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素已经排序完成。 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”。 def bubble_sort(li): for i in range(len(li)-1): for j in range(len(li)-i-1): if li[j]>li[j+1]: li[j],li[j+1]=li[j+1],li[j] return li print(bubble_sort([112,1245325,2342,323])) 选择排序 def select_sort(li): for i in range(len(li)-1): min_loc=i for j in range(i+1,len(li)): if li[j]<li[min_loc]: min_loc=j if li[min_loc]!=i: li[i],li[min_loc]=li[min_loc],li[i] return li print

glob.glob()、sort() 等一些函数的用法

泪湿孤枕 提交于 2020-01-14 01:30:51
一段代码: import glob filepaths = glob.glob(os.path.join(args.input_aus_filesdir, '*.csv')) #glob.glob(包含一个路径信息的字符串),返回匹配 pathname 的路径名列表,返回的类型是list类型 filepaths.sort() #sort()为list的内置函数,排序 data = dict() #dict()为修复字典迭代方法 1. glob.glob() :返回路径下符合条件的文件名的列表 import glob filepaths = glob.glob(包含一个路径信息的字符串) glob.glob(pathname) : 返回匹配 pathname 的路径名列表 ,其中的 元素 必须为 包含一个路径信息的字符串 。 返回的是列表 list类型 。是所有路径下的符合条件的文件名的列表。 2. list.sort() :排序,sort()为list的内置函数 list.sort(*, key=None, reverse=False) : 此方法会对列表进行原地排序,只使用 < 来进行各项间比较。 异常不会被屏蔽 —— 如果有任何比较操作失败,整个排序操作将失败(而列表可能会处于被部分修改的状态)。 sort() 接受两个仅限以关键字形式传入的参数 ( 仅限关键字参数 ):

【Leetcode】148. Sort List

本小妞迷上赌 提交于 2020-01-13 13:21:07
题目地址: https://leetcode.com/problems/sort-list/ 对一个链表排序。可以使用归并排序。 public class Solution { public ListNode sortList ( ListNode head ) { if ( head == null ) { return null ; } return mergeSort ( head ) ; } private ListNode mergeSort ( ListNode head ) { if ( head == null || head . next == null ) { return head ; } ListNode l1 = head ; // 这里的mid取的是整个链表的中点,如果链表长为偶数则取的是靠前的中点 ListNode mid = findMid ( head ) ; ListNode l2 = mid . next ; // 要将链表从中间断开,才能继续归并排序 mid . next = null ; ListNode list1 = mergeSort ( l1 ) ; ListNode list2 = mergeSort ( l2 ) ; ListNode dummy = new ListNode ( 0 ) ; ListNode cur =

大一寒假培训2

匆匆过客 提交于 2020-01-13 08:50:47
nefu1481题 谁考了第k名排序 # include <bits/stdc++.h> using namespace std ; struct sa { int id ; double cj ; } a [ 200 ] ; int cmp ( const struct sa & a , const struct sa & b ) { return a . cj > b . cj ; } int main ( ) { int n , k ; cin >> n >> k ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> a [ i ] . id >> a [ i ] . cj ; } sort ( a + 1 , a + 1 + n , cmp ) ; printf ( "%d %g\n" , a [ k ] . id , a [ k ] . cj ) ; return 0 ; } nefu1482题 奇数单增序列 # include <bits/stdc++.h> using namespace std ; int cmp ( const int & j , const int & u ) { return j < u ; } int main ( ) { int n , i , k , j , a [ 502 ] , b [ 502 ]

Sorted() function in Python

微笑、不失礼 提交于 2020-01-13 06:11:35
Sorting any sequence is very easy in Python using built-in method sorted() which does all the hard work for you. Sorted() sorts any sequence (list, tuple) and always returns a list with the elements in sorted manner, without modifying the original sequence. x = [2, 8, 1, 4, 6, 3, 7] print "Sorted List returned :", print sorted(x) print "\nReverse sort :", print sorted(x, reverse = True) print "\nOriginal list not modified :", print x Output : Sorted List returned : [1, 2, 3, 4, 6, 7, 8] Reverse sort : [8, 7, 6, 4, 3, 2, 1] Original list not modified : [2, 8, 1, 4, 6, 3, 7] # List x = ['q', 'w'

c++ vector的使用,sort,find,易错点

你离开我真会死。 提交于 2020-01-13 04:40:29
闲话: c++容器,使用的最多的有vector,deque与map,各自有不同的特色。而vector也使用得最多,最方便,所有在这里写下vector最常用的sort,find函数以及一些易错点。 正文: vector的优点:申请的空间都是连续的,而且不用自己new,delete分配释放空间,减少内存溢出的风险,访问速度快,可以直接下标访问(list就不可以),对sort函数很友好。 vector的缺点:不可以向头节点添加元素(不可以用作队列),申请空间不可以太大(记得有个函数capacity可以查看最大空间)。 sort排序: eg1:运算符重载进行排序 这里使用到了运算符重载, bool operator <(node node1) ,含义是前一个node(定义的结构体)与后一个node1进行’<'运算是根据哪种规则进行排序(注意是判断真假,所以是bool型的),例子里的规则是比较前一个的序号与后一序号,序号小的排在前面。这里类似于离散数学中自己定义一个偏序关系。 # include <iostream> # include <cstdio> # include <vector> # include <algorithm> using namespace std ; template < class T > struct node { T data ; int serialNum

Leetcode 148: Sort List

徘徊边缘 提交于 2020-01-13 02:03:09
Sort a linked list in O ( n log n ) time using constant space complexity. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Output: -1->0->3->4->5 解法一:堆排序 考虑如果被排序对象不是链表的节点,而是普通数字的情况下,此题可以用块排,堆排序等搞定。考虑到链表结构的复杂性,可以选用堆排序。将每个节点依次送入堆中,然后再从堆顶依次输出并连接成新链表即可。代码如下: public static ListNode sortList(ListNode head) { Queue<ListNode> heap = new PriorityQueue<>(new Comparator<ListNode>() { @Override public int compare(ListNode node1, ListNode node2) { if(node1.val > node2.val) { return 1 ; } else if (node1.val == node2.val) { return 0 ; } else { return -1 ; } } }) ; while(head !

LeetCode 451. Sort Characters By Frequency 根据字符出现频率排序 (C++/Java)

二次信任 提交于 2020-01-12 01:19:03
题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. Example 2: Input: "cccaaa" Output: "cccaaa" Explanation: Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer. Note that "cacaca" is incorrect, as the same characters must be together. Example 3: Input: "Aabb" Output: "bbAa" Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect. Note that