sf = input('pick a number between 1-5: ')
string = ''
with open('dna.txt','r')as f:
count = 0
for line in f:
row = list(map(str, line.strip().split()))
Letter, number = row
n=int(number)
chunks = [row[x:x+2] for x in range(0, len(row),2)]
for x in chunks:
print(x)
if x >= sf:
string += x
[获得用户输入后,它需要在创建的列表中搜索等于或大于给定值的值,并将其添加到'string'。在文本文件中,第一列都是字母,第二列都是数字。数字列是否可能成为整数?
[这是数据的样子][1]:https://i.stack.imgur.com/VM3aJ.png
提前谢谢您:)
如果要让第二列为整数,则不希望将数据作为单个字符串返回; (str, int)
元组的列表将允许您使用正确的类型来跟踪这两个值。
我认为这更接近您的目标:
from typing import List, Tuple
def read_values_with_minimum(
path: str,
n: int
) -> List[Tuple[str, int]]:
with open(path) as f:
return [
(s, int(v))
for s, v in (
line.strip().split()
for line in f.readlines()
)
if int(v) >= n
]
if __name__ == '__main__':
sf = int(input('pick a number between 1-5: '))
assert 1 <= sf <= 5
print("\n".join(
map(str, read_values_with_minimum("dna.txt", sf))
))