我一直在解决这个问题,但除了最后一部分之外,我已经解决了很多问题。我需要为列表迭代一些循环,以便它填满整个表。我正在做的是尝试结合文本文件
for example pulsars1=
1
2
3
4
5
and signals1=
M
I
N
N
A
所以我希望列表看起来像
1M, 2I, 3N etc..
我几乎得到了它,只是它只执行此列表的前两个条目.. 它看起来像这样:
This program proccess data from the Purdue Pulsar Laboratory
============================================================
It reads the data from 2 files containing the pulsar name and signal strength,
then combines them and displays the results.
Pulsar name file: pulsars1.txt
Signal strength: signals1.txt
Analyzing data from pulsars1.txt and signals1.txt files...
Reading from pulsars1.txt ...
Reading from signals1.txt ...
Combining values...
The combined BOOYA data includes 3 values.
1M
所以你可以看到它正在梳理第一行,但其余部分却没有这样做。 这是我现在的代码。
# defining the read function
def read(pulsar_name,signal_strength):
#opening and reading data from first line
pulsars = open(pulsar_name,"r").readlines()
#opening and reading data from second line
signals = open(signal_strength,"r").readlines()
#creating a new empty list
astro_list = []
#appending pulsar values to list
for all_pulse in range(0,len(signals)):
astro_list.append(pulsars)
#appending signal data to list
for all_signal in range(0,len(signals)):
astro_list.append(signals)
for i in range(0,len(astro_list)):
return(pulsars[i].rstrip()+signals[i])
#defining the main function
def main():
#displaying a description of what the program does
purpose = "This program proccess data from the Purdue Pulsar Laboratory"
underheading = "=" * len(purpose)
print(purpose)
print(underheading)
print("It reads the data from 2 files containing the pulsar name and signal strength, \nthen combines them and displays the results.")
#accepting inputs from the user about file names
pulsar_name = input("\nPulsar name file: ")
signal_strength = input("Signal strength: ")
#calling
astro_list = read(pulsar_name,signal_strength)
read(pulsar_name,signal_strength)
#reading values
print("\nAnalyzing data from" , pulsar_name, "and", signal_strength, "files...")
print(" ","Reading from" ,pulsar_name,"...")
print(" ","Reading from" ,signal_strength,"...")
print(" ","Combining values...")
#displaying the top part of the table/ counting the number of elements that are in the list
print("\nThe combined BOOYA data includes" ,len(astro_list), "values.")
print(read(pulsar_name,signal_strength))
if __name__ == '__main__':
main()
任何建议?谢谢
答案 0 :(得分:1)
在这里使用zip
可能是一个好主意。如果您确定,您的两个文本文件始终包含相同数量的条目,zip
会这样做,但为了更灵活,您可以使用itertools.zip_longest
:
from itertools import zip_longest
def main():
#input of pulsar_name and signal_strength as described by you, e.g. you end up with
#pulsar_name = "test1.txt"
#signal_strength = "test2.txt"
#read file and remove whitespaces and newline escape code
pulsar_list = [line.strip(" \n") for line in open(pulsar_name, "r")]
signal_list = [line.strip(" \n") for line in open(signal_strength, "r")]
#combine the two list and mark those entries without a counterpart
astro_list = list(zip_longest(pulsar_list, signal_list, fillvalue = "-missing-"))
#print out the list
for i, item in enumerate(astro_list):
print("Entry #", i, "for astrolist is ", item, "and consists of pulsar data",
pulsar_list[i], "and signal strength", signal_list[i])
main()
输出循环只是向您展示如何访问所有三个列表中的数据的示例。如果pulsar
和signal
不具有相同的长度,则输出无法正常工作。我可以用不同的方式编写它来考虑长度不等的列表,但由于这只是一个例子,我不想让它变得比必要的复杂。