[英]How to index a tuple.txt file using a list.txt file using python?
I would like to index a tuple.txt file using a list.txt file.我想使用 list.txt 文件索引 tuple.txt 文件。 That is, if my tuple file is a.txt file that reads somewhat like:
也就是说,如果我的元组文件是一个 .txt 文件,它的内容有点像:
[[["-0.07636114002660116", "-0.5365621532160825", "-0.39960655510421184", "0.6733612454339026"], ["0.0", "0.0", "0.0", "37.2155259"], ["-0.05958626994915151", "-0.023029990708366282", "-0.24325076433502524", "0.9288248327845068"], ["0.05958626994915151", "0.023029990708366282", "0.24325076433502524", "36.286701067215496"], ["0.09995740879332612", "-0.48667451106459764", "-0.23779637140751794", "0.5508093478212072"], ["-0.2359048187690788", "-0.07291763285985114", "-0.4050609480317191", "1.0513767303972021"], ["-0.3081573380300473", "-0.08270260281220124", "-0.2497148935020871", "1.0220121263617357"], ["0.18471536734852254", "0.04700586284011614", "0.13075317534249653", "36.28656567125096"], ["-0.05287657813840254", "-0.014190902179399766", "-0.04284846553710331", "0.0295"], ["-2.6166252598538904", "1.7701571470098587", "2.171220685416502", "3.833325363231776"]]] [[["-0.07636114002660116", "-0.5365621532160825", "-0.39960655510421184", "0.6733612454339026"], ["0.0", "0.0", "0.0", "37.2155259"], ["-0.05958626994915151", "-0.023029990708366282 ", "-0.24325076433502524", "0.9288248327845068"], ["0.05958626994915151", "0.023029990708366282", "0.24325076433502524", "36.286701067215496"], ["0.09995740879332612", "-0.48667451106459764", "-0.23779637140751794", "0.5508093478212072"], ["-0.2359048187690788", "-0.07291763285985114", "-0.4050609480317191", "1.0513767303972021"], ["-0.3081573380300473", "-0.08270260281220124", "-0.2497148935020871", "1.0220121263617357"], ["0.18471536734852254", "0.04700586284011614" , "0.13075317534249653", "36.28656567125096"], ["-0.05287657813840254", "-0.014190902179399766", "-0.04284846553710331", "0.0295"], ["-2.6166252598538904", "1.7701571470098587", "2.171220685416502", "3.833325363231776"]] ]
and I have a list.txt file that reads something like:我有一个 list.txt 文件,内容如下:
1 3 4 7 1 3 4 7
I want to create a new tuple by indexing the first tuple.txt file with the list.txt file我想通过使用 list.txt 文件索引第一个 tuple.txt 文件来创建一个新的元组
For instance, in this case, my new tuple (if I save it as new_tuple) should read:例如,在这种情况下,我的新元组(如果我将其保存为 new_tuple)应为:
new_tuple新元组
Output: Output:
[[["0.0", "0.0", "0.0", "37.2155259"], ["0.05958626994915151", "0.023029990708366282", "0.24325076433502524", "36.286701067215496"], ["0.09995740879332612", "-0.48667451106459764", "-0.23779637140751794", "0.5508093478212072"], ["0.18471536734852254", "0.04700586284011614", "0.13075317534249653", "36.28656567125096"]]] [[["0.0", "0.0", "0.0", "37.2155259"], ["0.05958626994915151", "0.023029990708366282", "0.24325076433502524", "36.286701067215496"], ["0.09995740879332612", "-0.48667451106459764", "- 0.23779637140751794", "0.5508093478212072"], ["0.18471536734852254", "0.04700586284011614", "0.13075317534249653", "36.28656567125]]]
Here are the raw.txt files in case they help.以下是 raw.txt 文件,以防万一。
tuple.txt: https://drive.google.com/file/d/1SdFVtxlUDj1XFm6wBUtNUS48dqJQBzwh/view?usp=sharing tuple.txt: https://drive.google.com/file/d/1SdFVtxlUDj1XFm6wBUtNUS48dqJQBzwh/view?usp=sharing
list.txt: https://drive.google.com/file/d/1AUSzV5kV3aEL8AhkW-PfKsCiVZ9iyk22/view?usp=sharing list.txt: https://drive.google.com/file/d/1AUSzV5kV3aEL8AhkW-PfKsCiVZ9iyk22/view?usp=sharing
I have little to no idea how to begin this.我几乎不知道如何开始。 Theoretically this should be possible, however, I am not sure how to begin writing a code that is pythonic enough to get the job done.
理论上这应该是可能的,但是,我不确定如何开始编写足以完成工作的 Python 代码。 The actual files I want to use the code on are much larger than the files I have used in my examples above.
我想要使用代码的实际文件比我在上面的示例中使用的文件大得多。 Therefore, an efficient pythonic code would be very helpful.
因此,高效的 Pythonic 代码将非常有帮助。
You need to know how to:您需要知道如何:
This is one way you could do that:这是您可以做到的一种方法:
import ast
tuples = ast.literal_eval(open('tuple.txt').read())[0]
indexes = [int(i) for i in open('list.txt').read().split(' ')]
open('new_tuple.txt','w').write(str([tuples[i] for i in indexes])+'\n')
Test:测试:
>>> import ast
>>> tuples = ast.literal_eval(open('tuple.txt').read())[0]
>>> indexes = [int(i) for i in open('list.txt').read().split(' ')]
>>> open('new_tuple.txt','w').write(str([tuples[i] for i in indexes])+'\n')
319
>>>
$ cat new_tuple.txt
[['0.0', '0.0', '0.0', '37.2155259'], ['0.05958626994915151', '0.023029990708366282', '0.24325076433502524', '36.286701067215496'], ['0.09995740879332612', '-0.48667451106459764', '-0.23779637140751794', '0.5508093478212072'], ['0.18471536734852254', '0.04700586284011614', '0.13075317534249653', '36.28656567125096']]
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.