简体   繁体   中英

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. That is, if my tuple file is a.txt file that reads somewhat like:

[[["-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:

1 3 4 7

I want to create a new tuple by indexing the first tuple.txt file with the list.txt file

For instance, in this case, my new tuple (if I save it as new_tuple) should read:

new_tuple

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"]]]

Here are the raw.txt files in case they help.

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

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. 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.

You need to know how to:

  • read a file as a string,
  • convert a string to a literal,
  • index a list with another list (here I used a "list-comprehension"),
  • convert a list to a string,
  • write to a file.

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']]
$ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM