Python数学建模算法与应用例题

2.2
1.aggcacggaaaaacgggaataacggaggaggacttggcacggcattacacggagg
2.cggaggacaaacgggatggcggtattggaggtggcggactgttcgggga
3.gggacggatacggattctggccacggacggaaaggaggacacggcggacataca
4.atggataacggaaacaaaccagacaaacttcggtagaaatacagaagctta
5.cggctggcggacaacggactggcggattccaaaaacggaggaggcggacggaggc

点击查看代码
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.count('t')])
b = np.array(a); print(b)
2.3
点击查看代码
L = ['abc', 12, 3.45, 'Python', 2.789]
print(L)
print(L[0])
L[0] = 'a'
L[1:3] = ['b', 'Hello']
print(L)
L[2:4] = []
print(L)
2.4
点击查看代码
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
d = [c for b in a for c in b]
print(d)
2.6
点击查看代码
T = ('abc', 12, 3.45, 'python', 2.789)
print(T)
print(T[-1])
print(T[1:3])
2.7
点击查看代码
student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}
print(student)
a = set('abcdabc')
print(a)
2.8
点击查看代码
dict1 = {'Alice': '123', 'Beth': '456', 'Cecil': 'abc'}
print(dict1['Alice'])
dict1['new'] = 'Hello'
dict1['Alice'] = '1234'
dict2 = {'abc': 123, 456: 78.9}
print(dict2[456])
2.42
点击查看代码
with open('data2_2.txt') as fp:
    L1 = []; L2 = [];
    for line in fp:
        L1.append(len(line))
        L2.append(len(line.strip()))
data = [str(num) +'\t' for num in L2]
print(L1); print(L2)
with open('data2_2.txt', 'w') as fp2:
    fp2.writelines(data)
posted @ 2024-09-07 16:27  乖不起来  阅读(8)  评论(0编辑  收藏  举报