合集-python
摘要:程序文件ex2_2.py import numpy as np a=[] with open('data2_2.txt') as f: for (i, s) in enumerate(f): a.append([s.count('a'), s.count('c'), s.count('g'),s.c
阅读全文
摘要:程序文件ex2_3.py L = ['abc', 12, 3.45, 'Python', 2.789] print(L) #输出完整列表 print(L[0]) #输出列表的第一个元素 L[0] = 'a' #修改列表的第一个元素 L[1:3] = ['b', 'Hello'] #修改列表的第二、三
阅读全文
摘要:程序文件ex2_4.py a=[[1,2,3],[4,5,6],[7,8,9]] d=[c for b in a for c in b] print(d)
阅读全文
摘要:程序文件ex2_5_1.py import os fn=[filename for filename in os.listdir('D:\Programs\Python\Python37') if filename.endswith(('.exe','.py'))] print(fn)
阅读全文
摘要:程序文件ex2_5_2.py from numpy.random import randint import numpy as np a=randint(10,20,16) #生成16个[10,20)上的随机整数 ma=max(a) ind1=[index for index,value in en
阅读全文
摘要:程序文件ex2_6.py T = ('abc', 12, 3.45, 'Python', 2.789) print(T) #输出完整元组 print(T[-1]) #输出元组的最后一个元素 print(T[1:3]) #输出元组的第二、三元素
阅读全文
摘要:程序文件ex2_7.py student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'} print(student) a = set('abcdabc') print(a) #每次输出是不一样的,如输出:{'d', 'b', 'a', 'c'}
阅读全文
摘要:程序文件ex2_8.py dict1 ={'Alice': '123', 'Beth': '456', 'Cecil': 'abc'} print(dict1['Alice']) #输出123 dict1['new'] = 'Hello' #增加新的键值对 dict1['Alice'] = '123
阅读全文
摘要:程序文件ex2_10.py Dict={'age':18, 'score':[98,97], 'name':'Zhang', 'sex':'male'} for item in Dict: #遍历输出字典的“键” print(item) print(" " ) for item in Dict.it
阅读全文
摘要:程序文件ex2_11_1.py import string import random x=string.ascii_letters+string.digits y=''.join([random.choice(x) for i in range(1000)]) choice()用于从多个元素中随机
阅读全文
摘要:程序文件ex2_11_2.py import string, random, collections #依次加载三个模块 x=string.ascii_letters+string.digits y=''.join([random.choice(x) for i in range(1000)]) c
阅读全文
摘要:程序文件ex2_12_2.py def factorial(n): #定义阶乘函数 r = 1 while n > 1: r *= n n -= 1 return r def fib(n): #定义输出斐波那契数列函数 a, b = 1, 1 while a < n: print(a, end='
阅读全文
摘要:程序文件ex2_12_3.py from ex2_12_2 import factorial, fib print('%d!=%d'%(5,factorial(5))) fib(200)
阅读全文
摘要:程序文件ex2_14.py f=lambda x, y, z: xyz L=lambda x: [x2, x3, x**4] print(f(3,4,5)); print(L(2))
阅读全文
摘要:程序文件ex2_16.py from random import sample from numpy.random import randint a=sample(range(10),5) #在[0,9]区间上选择不重复的5个整数 b=randint(0,10,5) #在[0,9]区间上生成5个随机
阅读全文
摘要:程序文件ex2_18.py from ex2_12_2 import * print(factorial(6)) fib(300)
阅读全文
摘要:程序文件ex2_20.py x1="abcde" x2=list(enumerate(x1)) for ind,ch in enumerate(x1): print(ch)
阅读全文
摘要:程序文件ex2_22.py a = filter(lambda x: x>10,[1,11,2,45,7,6,13]) b = filter(lambda x: x.isalnum(),['abc', 'xy12', '***']) isalnum()是测试是否为字母或数字的方法 print(lis
阅读全文
摘要:程序文件ex2_24.py s1=[str(x)+str(y) for x,y in zip(['v']*4,range(1,5))] s2=list(zip('abcd',range(4))) print(s1); print(s2)
阅读全文
摘要:程序文件ex2_26.py import numpy as np a = np.ones(4, dtype=int) #输出[1, 1, 1, 1] b = np.ones((4,), dtype=int) #同a c= np.ones((4,1)) #输出4行1列的数组 d = np.zeros(
阅读全文
摘要:程序文件ex2_28.py import numpy as np a = np.arange(16).reshape(4,4) #生成4行4列的数组 b = np.floor(5np.random.random((2, 4))) c = np.ceil(6np.random.random((4, 2
阅读全文
摘要:程序文件ex2_30.py import numpy as np a = np.array([[0, 3, 4], [1, 6, 4]]) b = a.sum() #使用方法,求矩阵所有元素的和 c1 = sum(a) #使用内置函数,求矩阵逐列元素的和 c2 = np.sum(a, axis=0)
阅读全文
摘要:import numpy as np import math from scipy.optimize import minimize, Bounds def func(x): return sum(math.sqrt(x[i]) for i in range(100)) def con(x): re
阅读全文
摘要:把max放进约束条件,不妨假定小于等于10000 linprog from scipy.optimize import linprog c=[0,0,3,2] A=[[-1,-1,0,0],[0,0,-1,-1],[3,0,2,0],[0,3,0,2],[100,90,80,70]] b=[[-30
阅读全文
摘要:import cvxpy as cp import numpy as np import pandas as pd from scipy.optimize import minimize import sympy as sp sp.init_printing(use_unicode=True) im
阅读全文
摘要:import numpy as np from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata import matplotlib.pyplot as plt t0 = np.linspace(700,
阅读全文
摘要:import numpy as np from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata import matplotlib.pyplot as plt np.random.seed(114514)
阅读全文
摘要:import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_latex=True) from scipy.integrate import odeint import matplotlib.pyplot
阅读全文
摘要:import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_latex=True) from scipy.integrate import odeint import matplotlib.pyplot
阅读全文
摘要:edges = [ ("Pe", "T", 13), ("Pe", "N", 68), ("Pe", "M", 78), ("Pe", "L", 51), ("Pe", "Pa", 51), ("T", "N", 68), ("T", "M", 70), ("T", "L", 6
阅读全文
摘要:import heapq def prim(graph, start): num_nodes = len(graph) visited = [False] * num_nodes min_heap = [(0, start, -1)] mst_cost = 0 mst_edges = [] whil
阅读全文
摘要:initial_costs = [2.5, 2.6, 2.8, 3.1] salvage_values = [2.0, 1.6, 1.3, 1.1] maintenance_costs = [0.3, 0.8, 1.5, 2.0] dp = [[float('inf')] * 2 for _ in
阅读全文

浙公网安备 33010602011771号