Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能重写它,给我一个答案列表?_Python - Fatal编程技术网

Python 我怎样才能重写它,给我一个答案列表?

Python 我怎样才能重写它,给我一个答案列表?,python,Python,我是python和编程新手,如有任何帮助,将不胜感激!我在用Python编写代码时遇到了问题。我的任务是编写一个函数,计算特定字母出现的次数除以该序列中的字母总数。 我的函数应该返回一个列表,其中包含每个序列的字母分数 def calculate_let(sequences, letter): calculate_letNew = [] for seq in sequences: len(seq) calculate_letNew = seq.co

我是python和编程新手,如有任何帮助,将不胜感激!我在用Python编写代码时遇到了问题。我的任务是编写一个函数,计算特定字母出现的次数除以该序列中的字母总数。
我的函数应该返回一个列表,其中包含每个序列的字母分数

def calculate_let(sequences, letter):
    calculate_letNew = []
    for seq in sequences:
        len(seq)
        calculate_letNew = seq.count(letter)/len(seq)
    return calculate_letNew
这是到目前为止我的代码。输出仅为一个序列提供了一个分数,即使列表
sequenceA
有四个序列

Letter_A = calculate_let(sequenceA, 'A')
print(Letter_A)
输出:
0.10273972602739725


我一直在尽我最大的努力试图解决这个问题,但老实说,我不知道从哪里开始。

我想你是想列一个这样的清单:

注意
列表。append
方法将向列表中添加一个元素

def count_lett(sequences, AminoAcid):
    count_lettNew = []
    for seq in sequences:
        count_lettNew.append(seq.count(AminoAcid)/len(seq))
    return count_lettNew

作为随机建议:

Python只使用TitleCase作为类名,因此我会选择这样编写代码(为了简单起见,保留
forloop
):


您可以使用以下工具以非常可读且简洁的方式完成:


请修正你的缩进。缩进严重的Python并不是一个好例子。
def count_lett(sequences, amino_acid):
    counts = [] # Although, you may consider something like fracs 
                # for "fractions" since these aren't actually counts
    for seq in sequences:
        counts.append(seq.count(amino_acid)/len(seq))
    return counts
def calc_freqs(sequences, letter):
    return [seq.count(letter)/len(seq) for seq in sequences]

sequenceA = 'ALABAMA', 'MISSISSIPPI', 'UTAH', 'IDAHO'
letterA = calc_freqs(sequenceA, 'A')
print(letterA)  # -> [0.5714285714285714, 0.0, 0.25, 0.2]