如何在Python的for循环中有条件地构建一个列表

huangapple go评论65阅读模式
英文:

How to conditionally build a list in a for loop in python

问题

sequence_C = []
for i in sequence:
    if i == "A":
        sequence_C.append("T")
    elif i == "T":
        sequence_C.append("A")
    elif i == "C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")
print(sequence_C)
英文:

How could I make sequence_C with the values after the if print as one list like sequence

sequence=["A","C","G","T","T","A","G","C","T","A","A","C","G"]
for i in sequence:
    sequence_C=[]
    if i=="A":
        sequence_C.append("T")
    elif i=="T":
        sequence_C.append("A")
    elif i=="C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")
    print(sequence_C)

答案1

得分: 1

只初始化sequence_C一次,在循环开始之前。

sequence_C = []
for i in sequence:
    if i == "A":
        sequence_C.append("T")
    elif i == "T":
        sequence_C.append("A")
    elif i == "C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")
print(sequence_C)

你可以更容易地使用字典和列表推导来解决这个问题,就像这样:

mapping = {'T': 'A', 'A': 'T', 'G': 'C', 'C': 'G'}
sequence_C = [mapping[item] for item in sequence]

这样做的好处是减少了重复的代码。

英文:

Only initialise sequence_C once, before the loop starts.

sequence_C = []
for i in sequence:
    if i=="A":
        sequence_C.append("T")
    elif i=="T":
        sequence_C.append("A")
    elif i=="C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")
print(sequence_C)

You can more easily solve this using a dictionary and a list comprehension, like so:

mapping = {'T': 'A', 'A': 'T', 'G': 'C', 'C': 'G'}
sequence_C = [mapping[item] for item in sequence]

There's a lot less duplicated code like this!

答案2

得分: 1

代码部分不翻译:

The only thing that should happen to sequence_C inside the loop is to have values appended to it. Do not initialize it to [] in the loop, and do not print the value of sequence_C in the loop.

翻译:

循环内唯一应该发生的事情是将值附加到sequence_C。不要在循环中初始化它为[],也不要在循环中打印sequence_C的值。

The simplest thing to do, though, is to create a translation table.

翻译:

不过,最简单的方法是创建一个翻译表。

英文:

The only thing that should happen to sequence_C inside the loop is to have values appended to it. Do not initialize it to [] in the loop, and do not print the value of sequence_C in the loop.

sequence=["A","C","G","T","T","A","G","C","T","A","A","C","G"]

sequence_C = []

for i in sequence:
    if i=="A":
        sequence_C.append("T")
    elif i=="T":
        sequence_C.append("A")
    elif i=="C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")

print(sequence_C)

The simplest thing to do, though, is to create a translation table.

sequence = "ACGTTAGCTAACG"
sequence_C = sequence.translate(str.maketrans("ATCG", "TAGC"))

答案3

得分: 0

你可以在循环的每次运行时创建一个新列表。将其提取出来,以便将元素附加到单个列表中。

sequence = ["A", "C", "G", "T", "T", "A", "G", "C", "T", "A", "A", "C", "G"]
sequence_C = []
for i in sequence:
    if i == "A":
        sequence_C.append("T")
    elif i == "T":
        sequence_C.append("A")
    elif i == "C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")
print(sequence_C)

但有更好的方法。字典可以将一个值映射到另一个值。使用这种方法代替。

s_map = {"A": "T", "T": "A", "C": "G"}  # 默认映射到 "C"
sequence_C = [s_map.get(s, "C") for s in sequence]
print(sequence_C)
英文:

You create a new list on each run of the loop. Pull that out to keep appending to a single list.

sequence=["A","C","G","T","T","A","G","C","T","A","A","C","G"]
sequence_C=[]
for i in sequence:
    if i=="A":
        sequence_C.append("T")
    elif i=="T":
        sequence_C.append("A")
    elif i=="C":
        sequence_C.append("G")
    else:
        sequence_C.append("C")
print(sequence_C)

But there is a better way. Dictionaries map one one value to another. Use that instead.

s_map = {"A":"T", "T":"A", "C":"G"} # w/ default "C"
sequence_C = [s_map.get(s, "C") for s in sequence]
print(sequence_C)

huangapple
  • 本文由 发表于 2023年2月26日 23:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定