Create dynamic names for lists [duplicate] - python

This question already has answers here:
Dictionary use instead of dynamic variable names in Python
(3 answers)
Closed 7 years ago.
From a shapefile I create a number of csv files but I don't know how many of them will be created each time. The csv files are named road_1, road_2 etc.
In these files, I have coordinates. I would like to put the coordinates of every csv files in lists.
So for road_1 I would like 3 lists:
x_1, y_1, z_1
For road_2:
x_2, y_2, z_2 etc.
I tried to name the lists in the loop where I get the coordinates with this : list+'_'+i where i is iterating through the number of files created, but i cannot concatenate a list and a string.
**
EDIT
**
Ok, some marked this topic as duplicated, fair enough. But just saying that I have to use a dictionnary doesn't answer all of my question. I had thought of using a dictionnary but my main issue is creating the name (either the name of the list, either the key of a dictionnary). I have to create this in a loop, not knowing how many I have to create. Thus, the name of the list (or key) should have a variable which must be the number of the road. And it's here that I have a problem.
As I said before, in my loop i tried to use a variable from the iteration loop to name my list but this didn't work, since it's not possible to concanate list with string. I could create an empty dictionnary with many empty key:value pairs, but I would still have to go through the keys name in the loop to add the values from the csv file in the dict.

Since it has been asked many times I wont write the code but only point you in the right direction (and maybe a different approach).
Use the glob module which will return the file names. Something like:
import glob
for csvFileNames in glob.glob("dir/*.csv"):
will return you each filename into the variable csvFileNames.
Then you simply open you csv Files with something like:
with open(csvFileNames, "r") as filesToRead:
for row in filestoRead:
#the structure of you csv File is unknown so cannot say anything here
Then its simple. Find you columns your interested in and create a dicts with the variables you need as keys. Use a counter to increment. All the information is there!

Related

Python - Reading files from Directory without single quotes

I am wondering if there is a way to read in files from your directory without single quotes around them. My directory has several geodataframes with the same suffix. I can read them all in and append to a list but the list will contain single quotes around each DataFrame name, making it a str.
dir()
'sub_geo_group_mangrove',
'sub_geo_group_marsh',
'sub_geo_group_multipolygon',
'sub_geo_group_pan',
'sub_geo_group_reedbed'
I am using these files to insert into a loop and therefore need the file names themselves without the single quotes
Edit
The data frames are all formatted as such:
sub_geo_group_mangrove.head(2)
geometry wetland
43 MULTIPOLYGON (((12.41310 -6.10921, 12.41274 -6... mangrove
59 POLYGON ((12.30671 -6.15770, 12.30654 -6.15741... mangrove
And I am attempting to read each DataFrame into the following lines:
I am pretty sure, what you are trying to achieve here, would have been really not necessary. But I would just answer what you ask and based on what I understand about your requirements.
As, you are trying to use the dataframe names in a loop structure I am assuming that the dataframes are available in the loop's scope.
which you have shown using dir() command in your example.
From python docs:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Now let's assume the list in which your DataFrame names are present is
dfnames = ['sub_geo_group_mangrove','sub_geo_group_marsh','sub_geo_group_multipolygon',...]
Based on what I understand about the problem you mentioned:
Now to loop over them you are not able to do something like:
for df in dfnames:
#do something with this df
cause the type(df) would be string.
At this point you can do couple of things. The most optimal of that, I think would be:
for name in dfnames:
df = vars()[name]
# do something with this df
vars() without argument is similar to locals().
vars() returns a dictionary, of variables used in your local scope.
The keys in this dictionary is variable names in string type, and the values associated with them is the data associated to those variables.

Name a Dictionary after a known string [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I've seen this question a few times before, and invariably I see that people always respond by saying that this is a bad idea. What is a good way to approach this issue?
I have a code that reads in data from several csv files in a for loop, i.e for each loop iteration a different csv is read. At the end of my for loop, I make a dictionary with some data in it. All I would like to do is name this dictionary after the original csv file's name.
At the moment I have:
sessionname=str(inputList).replace(".csv",'').replace("'",'').replace("[",'').replace("]",'')
session_dict={}
What I would like is for session_dict to be named using the string in sessionname.
Therefore at the end of my for loop, I would have a number of dictionaries each with the name of its orginal csv file.
Thank you!
You can't create a variable named after a filepath, because of the slashes and the spaces, etc.
One thing you can do is to name each of your dictionaries with a number (or a unique code), and in run-time create a reference dictionary like:
ref_dict = {r'csv_file_0_path' :0, r'csv_file_1_path': 1, r'csv_file_2_path': 2}
and so on.

How to assign variable names programmatically in python? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
How to assign variable names programmatically in python?
I have a lot of objects and their names are not predefined. Currently I am reading in from a file which has names in it and I’m reading in them into a list. But from this list how do I assign the names to the variables so that variables are easy to remember and work with. I cannot hard code because :
a. I don’t want them to be fixed
b. I have a lot of names so it is not feasible to type in. (Even copy paste every name)
Use dictionaries. (And assign its keys programmatically!)
You can assign the variables names which you’ve already read into the list and then have their values read. I’m still not clear about how you want your values but assuming you have them in a list too as you have your variable names, then just zip both lists as dict:
data = dict(zip(names, values))
Now you can access each variable by its name as follows:
data['name']

Dynamically generate array elements from yaml file in Python

Given the following yaml file stored in my_yaml that contains varying sets of dictionary keys and/or class variables (denoted by self._*):
config1.json:
- [[foo, bar], [hello, world]]
config2.json:
- [[foo], [self._hi]]
From the json file, I want to populate a new list of tuples. The items in each tuple are determined by looking up dict keys in this yaml file.
So if I iterate through a dictionary called config1.json, and I have an empty list called config_list, I want to do something like:
config_list.append(tuple[i['foo']['bar],i['hello']['world']])
But if it were config2.json, I want to do something like:
config_list.append(tuple[i['foo'],self._hi])
I can do this in a less dynamic way:
for i in my_yaml['config1.json'][0]:
config_list.append(tuple([ i[my_yaml[asset][0][0]][my_yaml[asset][0][1]],i[my_yaml[asset][1][0]][my_yaml[asset][1][1]]]))
or:
for i in my_yaml['config2.json'][0]:
config_list.append(tuple([ i[my_yaml[asset][0][0]],i[my_yaml[asset][1][0]]]))
Instead I would like to dynamically generate the contents of config_list
Any ideas or alternatives would be greatly appreciated.
I think you are bit confusing things, first of all because you are referring
to a file in "From the json [sic] file" and there is no JSON file mentioned
anywhere in the question. There are mapping keys that look like
filenames for JSON files, so I hope we can assume you mean "From the value
associated with the mapping key that ends in the string .json".
The other confusing thing is that you obfuscate the fact that you want tuples
but load list nested in list nested in lists from you YAML document.
If you want tuples, it is much more clear to specify them in your YAML document:
config1.json:
- !!python/tuple [[foo, bar], [hello, world]]
config2.json:
- !!python/tuple [[foo], [self._hi]]
So you can do:
import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML(typ='unsafe')
with open('my.yaml') as fp:
my_yaml = yaml.load(fp)
for key in my_yaml:
for idx, elem in enumerate(my_yaml[key]):
print('{}[{}] -> {}'.format(key, idx, my_yaml[key][idx]))
which directly gives you the tuples you seem to want instead of lists you need to process:
config1.json[0] -> (['foo', 'bar'], ['hello', 'world'])
config2.json[0] -> (['foo'], ['self._hi'])
In your question you hard code access to the first and only
element of the sequence that are the values for the root level
mapping. This forces you to use the final [0] in your for loop. I
assume you are going to have multiple elements in those sequences, but
for a good question you should leave that out, as it is irrelevant for the
question on how to get the tuples, and thereby only obfuscating things.
Please note that you need to keep control over your input, as using
typ='unsafe' is, you guessed, unsafe. If you cannot guarantee that
use typ='safe' and register and use the tag !tuple.

Contig Extension with Python

I want to add a function to a program that creates dictionaries with dna sequences that receives a contig (incon= initial contig; dna sequence) and extends it to the right by finding overlapping parts in form of keys in dictionaries and concatenating the values with the "+" operator.
I'll give a quick example:
GATTTGAAGC as initial contig
ATTTGAAGC:A is one of many entries in the dictionary
I want the function to search for such an overlapping part (I asked this here yesterday and it worked fine by itself and with specific values but not within the function with variables) that is a key in the dictionary and concatenate the value of that key to the initial sequence (extend the contig to the right) and save the new sequence into incon then delete this dictionary-entry and repeat until there are no entries left (this part i haven't even tried yet).
First i want the function to search for keys with length of 9 with values of length 1 (ATTTGAAGC:A) and if there are no overlapping parts for keys with length 8 with values of length 2 (f.e. ATTTGAAG:TG) and so on.
Additional Info:
The Dictionary "suffixDicts" has such entries with values with length from 1 (key has length 14) to 10 (key has length 5).
"reads" is where a list of sequences is stored
When i try to do the steps one after another some work (like the search) and some don't but when i tried to built a function out of it, literally nothing happens. The function is supposed to return the smallest possible extension.
def extendContig (incon, reads, suffixDicts):
incon = reads[0]
for x in range(1,len(incon)):
for key in suffixDicts.keys():
if incon[x:] == key:
incon = incon+suffixDicts['key']
print(incon)
else:
print("n")
return()
I'm very new to Python and there probably are very dire mistakes i made and i would like them to be pointed out. I know that I'm way over my head with this but I'm understanding most parts of the already existing code now but still have problems with implementing something by myself into it, probably due to incorrect synthax. I know there are programs i could use but i would like to understand the whole thing behind it.
edit: As asked for i will add the already given functions. Some of them were already written some parts i wrote based on the given code (basically i copied it with some tweaks). Warning: It is quite a lot:
Reading the Fasta file:
Additional Info:
The Fasta file contains large amounts of sequences in the Form:
"> read 1
TTATGAATATTACGCAATGGACGTCCAAGGTACAGCGTATTTGTACGCTA
"> read 2
AACTGCTATCTTTCTTGTCCACTCGAAAATCCATAACGTAGCCCATAACG
"> read 3
TCAGTTATCCTATATACTGGATCCCGACTTTAATCGGCGTCGGAATTACT
I uploaded the file here: http://s000.tinyupload.com/?file_id=52090273537190816031
edit: edited the large blocks of code out it doesn't seem to be necessary.

Categories