标签:学习笔记
选择排序:nums = [1, 2, 6, 7, 8, 9, 3, 4, 5]
for i in range(len(nums)):
maxindex = i
for j in range(i + 1, len(nums)):
if nums[j] < nums[maxindex]:
maxindex = j
if i != maxindex:
nums[i], nums[maxindex] = nums[maxindex], nums[i]
print(nums)
nums = [7, 19, 15, 12, 15, 5, 13, 14, 8, 15]
length = len(nums)
for i in range(length // 2):
maxindex = i
minindex = length - i - 1
minorigin = length - i - 1
for j in range(i + 1, length - i):
if nums[j] > nums[maxindex]:
maxindex = j
if nums[j-1] < nums[minindex]:#正序比较 或者逆序比较都可以 length - j - 1
minindex = j - 1
if i != maxindex:
nums[i], nums[maxindex] = nums[maxindex], nums[i]
if i == minindex: #如果i就是最小值索引,则执行此if,因为上一行索引i的值与最大值已经进行了交换
minindex = maxindex
if minorigin != minindex:
nums[minorigin], nums[minindex]=nums[minindex], nums[minorigin]
print(nums)
nums = [7, 19, 15, 12, 15, 5, 13, 14, 8, 15]
length = len(nums)
for i in range(length // 2):
maxindex = i
minindex = length - i - 1
minorigin = length - i - 1
for j in range(i + 1, length - 1):
if nums[j] > nums[maxindex]:
maxindex = j
if nums[length - j - 1] < nums[minindex]:
minindex = length - j - 1
if minindex == maxindex: #!
break
if i != maxindex:
nums[i], nums[maxindex] = nums[maxindex], nums[i]
if i == minindex:
minindex = maxindex
if minorigin != minindex:
nums[minorigin], nums[minindex] = nums[minindex], nums[minorigin]
print(nums)
nums =[1, 1, 1, 1, 1, 1, 1, 1, 2]
length = len(nums)
for i in range(length // 2):
maxindex = i
minindex = length - i - 1
minorigin = length - i - 1
for j in range(i + 1, length - 1):
if nums[j] > nums[maxindex]:
maxindex = j
if nums[length - j - 1] < nums[minindex]:
minindex = length - j - 1
if minindex == maxindex: #!
break
if i != maxindex:
nums[i], nums[maxindex] = nums[maxindex], nums[i]
if i == minindex:
minindex = maxindex
if minorigin != minindex and nums[minorigin] != nums[minindex]: #!
nums[minorigin], nums[minindex] = nums[minindex], nums[minorigin]
print(nums)
num = input(‘>>>‘)
d = {}
for c in num:
if not d.get(c):
d[c] = 1
continue
d[c] += 1
print(d)
d ={}
for c in num:
if c not in d.keys():
d[c] = 1
else:
d[c] += 1
print(d)
import random
n = 100
nums = [0] * n
for i in range(n):
nums[i] = random.randint(-1000, 1000)
print(nums)
t = nums.copy()
t.sort()
print(t)
d = {}
for x in nums:
if x not in d.keys():
d[x] = 1
else:
d[x] += 1
print(d)
d1 = sorted(d.items())
print(d1)
import random
alphabet = ‘abcdefghijklmnopqrstuvwxyz‘
words = []
for _ in range(100):
words.append(‘‘.join(random.choice(alphabet) for _ in range(2)))
d = {}
for x in words:
d[x] = d.get(x, 0) + 1
print(d)
d1 = sorted(d.items(), reverse = True)
print(d1)
[i ** 2 for i in range(1,11)]
lst = [1,4,9,16,2,5,10,15]
[lst[i] + lst[i + 1] for i in range(len(lst) - 1)]
[print(‘{}*{}={:<{}}{}‘.format(j, i, j * i,2 if j ==1 else 3, ‘‘ if i != j else ‘\n‘),end = ‘‘)for i in range(1,10) for j in range(1,i + 1)]
import random
[(‘{:>04}‘.format(i) + ‘.‘ + ‘‘.join(‘abcdefghijklmnopqrstuvwxyz‘[random.randint(0,25)] for _ in range(10))) for i in range(1,101)]
标签:学习笔记
原文地址:http://blog.51cto.com/11281400/2095514