python 冒泡排序加入判断

#!/usr/bin/env python

#coding:utf-8

import types,sys

# 简单的排序

l=[1,9,2,3,-1,724,219,888]

for i in range(len(l)):

for j in range(i,len(l)):

if l[j] > l[i]:

l[i],l[j]=l[j],l[i]

print l

# 定义为函数并加入判断的排序

def sort(list_sort):

if type(list_sort) == types.ListType: # 判断输入的是否为列表

if len(list_sort)==0: # 判断列表长度

print "list is empty"

else:

for i in range(len(list_sort)): # 开始排序

for j in range(i,len(list_sort)):

if list_sort[j] > list_sort[i]:

list_sort[i] , list_sort[j] = list_sort[j] , list_sort[i]

print list_sort

sort([1,2,4,-1,99])

sort("1") # 不是列表不执行

原文地址:http://blog.51cto.com/19941018/2066522

时间: 2024-08-07 22:40:14

python 冒泡排序加入判断的相关文章

python 值比较判断,np.nan is np.nan 却 np.nan != np.nan ,pandas 单个数据框值判断nan

pandas中DataFrame,Series 都有 isnull()方法,而数据框却没有,用了就会报错:AttributeError: 'float' object has no attribute 'isnull' 怎么判断单个框是否为 np.nan Python常规的判断,==,和is, 这对None是有效的 None is NoneOut[49]: True None == NoneOut[50]: True 而对,np.nan,只能用is da1pd.ix[6000996,u'团队']

Python的if判断与while循环

1.if判断 Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句 else: 执行语句 Python中使用缩进代替c语言中的大括号,来告诉程序所执行的内容. 缩进--推荐四个空格 (使用2个.3个空格或者tab都是可以得) 不要tab与空格混用不同软件对空格的显示逻辑总是一样的,但是对于tab却五花八门.有的软件把Tab展开成空格,有的不会展开.有的Tab宽度是4,有的宽度是8,这些不一致会使得代码混乱,尤其是靠缩进表示块结构的Python. 其中"判断

Python中的判断、循环 if...else,while

if...else语句: a=3; b=3; if a == b :print(a,b)elif a <= b :print(str(a) + " is less than " + str(b))else :print(str(a) + " is greater than " + str(b)) ################################### n = 3if (n >= 0 and n <= 8) or (n >= 1

python学习——如何判断输入是数字

笨办法学python第35节 该节主要是讲分支与函数,主要遇到的问题是python中如何判断输入是数字. 首先原代码如下: from sys import exit def gold_room(): print "This room is full of gold. How much do you take?" next = raw_input("> ") if "0" in next or "1" in next: h

python之条件判断、循环和字符串格式化

1. python的条件判断:if和else 在条件判断中可以使用算数运算符 等于:== 不等于:!= 大于:> 小于:< 大于等于:>= 小于等于:<= 示例1: username=input('请输入用户名:')passwd=input('请输入密码:')if username == 'mpp' and passwd == '123': print('登录成功')else: print('用户名或密码错误') 示例2:if里可以嵌套if,也可以使用elif score=int(

python代码中判断版本

在python代码中判断python版本: if sys.version_info < (3, 0): lib.make_flows.argtypes = [c_char_p, c_char_p, c_int, c_int, c_int] lib.make_flows(avi, newpath, 0, 0, 10) else: in_path = bytes(avi, 'utf8') out_path = bytes(newpath, 'utf8') lib.make_flows(in_path

python selenuim如何判断下拉框是否加载出来,超过时间不再等待

s_flag = True time_start = time.time() while s_flag: doc = etree.HTML(unicode.encode(driver.page_source, encoding='utf-8')) from_list = doc.xpath("""//*[@id="ext-gen267"]/div""") if len(from_list) == 0: time_end = t

Python冒泡排序(6)

前面写了5种冒泡排序,实质只有2种冒泡方式. 第1种是采用相邻两数比较的方式进行排序,第2种是采用与固定位数字比较的方式进行排序. 第1种方式中两个比较的数所处的位置每次都是不同的,第2种方式中两个比较的数所处的位置有一个是不变的. 第1种方式中可以通过添加判断标识的方式减少排序轮次. 例如如下代码中添加了bf标识 Python代码: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] print("排序前: %s\r\n" %lst) # 轮 for i in

Python中如何判断文件是否存在?

通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错.所以最好在做任何操作之前,先判断文件是否存在. 本文为大家介绍三种判断文件或文件夹是否存在的方法,分别使用 os模块 . Try语句 . pathlib模块 ,一起来看看吧,希望对大家学习python有所帮助. 1.使用os模块 os模块中的 os.path.exists() 方法用于检验文件是否存在. · 判断文件是否存在 import osos.path.exists(test_file.txt)#True o