I have a single dictionary that contains four keys each key representing a file name and the values is nested lists as can be seen below:
{'file1': [[['1', '909238', '.', 'G', 'C', '131', '.', 'DP=11;VDB=3.108943e02;RPB=3.171491e-01;AF1=0.5;AC1=1;DP4=4,1,3,3;MQ=50;FQ=104;PV4=0.55,0.29,1,0.17', 'GT:PL:GQ', '0/1:161,0,131:99'], ['1', '909309', '.', 'T', 'C', '79', '.', 'DP=9;VDB=8.191851e-02;RPB=4.748531e-01;AF1=0.5;AC1=1;DP4=5,0,1,3;MQ=50;FQ=81.7;PV4=0.048,0.12,1,1', 'GT:PL:GQ', '0/1:109,0,120:99']......,'008_NTtrfiltered': [[['1', '949608', '.', 'G', 'A',...}
My question is how to check only the first two elements in the list for instance "1", "909238" for each of the key if they are the same and then write them to a file. The reason I want to do this is I want to filter only common values (only the first two elements of the list) for the four files (keys).
Thanks a lot in advance
Best.
You can access to the keys of the dictionary dictio and make your comparison using :
f = open('file.txt','w')
value_to_check_1 = '1'
value_to_check_2 = '909238'
for k in dictio:
value_1 = dictio[k][0][0][0]
value_2 = dictio[k][0][0][1]
if (( value_1 == value_to_check_1) and (value_2 == value_to_check_2)):
f.write('What you want to write\n')
f.close()
If you want to do a check that imply every values of your dictionary dictio.
Maybe you want to store couples of values from dictio.
couples = [(dictio[k][0][0][0], dictio[k][0][0][1]) for k in dictio]
Then, you can do a loop and iterate over the couples to do your check.
Example you can adapt according to your need :
for e in values_to_check:
for c in couples:
if (float(e[0][0][0]) >= float(c[0]) and float(e[0][0][1]) <= float(c[1])):
f.write(str(e[0][0][0]) + str(e[0][0][1]) + '\n')
Related
I have an array:
foo = ['1', '2', '', '1', '2', '3', '', '1', '', '2']
¿Is there any efficient way to split this array into sub-arrays using '' as separator?
I want to get:
foo = [['1', '2'], ['1', '2', '3'], ['1'], ['2']]
In one line:
[list(g) for k, g in itertools.groupby(foo, lambda x: x == '') if not k]
Edit:
From the oficial documentation:
groupby
generates a break or new group every time the value of the key
function changes (which is why it is usually necessary to have sorted
the data using the same key function).
The key I generate can be True, or False. It changes each time we find the empty string element. So when it's True, g will contain an iterable with all the element before finding an empty string. So I convert this iterable as a list, and of course I add the group only when the key change
Don't know how to explain it better, sorry :/ Hope it helped
Create a list containing a single list.
output = [[]]
Now, iterate over your input list. If the item is not '', append it to the last element of output. If it is, add an empty list to output.
for item in foo:
if item == '':
output.append([])
else:
output[-1].append(item)
At the end of this, you have your desired output
[['1', '2'], ['1', '2', '3'], ['1'], ['2']]
I have a dict that contains site location names and key codes
i'm trying to pull the site location names (which are values) out of the dict and make a simple list.
I have this code which prints exactly what i want:
for s in mydict['mykey']:
print(s['site'])
site1
site2
site3
site4
but when i try to do something like this:
for s in mydict['mykey']:
mylist = list(s['site'])
or
for s in mydict['mykey']:
mylist2 = (s['site'])
i only get the last value:
mylist
['s', 'i', 't', 'e', '4']
mylist2
'site4'
Basically just looking to have the sites in a list, one per line
Use a list comprehension:
mylist = [s['site'] for s in mydict['mykey']]
This is the equivalent of:
mylist = []
for s in mydict['mykey']:
mylist.append(s['site'])
I have the following:
d1={"00f_125":["A","2","3"],
"00f_126":["1","2","3"],
"00f_127":["T","2","3"],
"00f_128":["T","2","3"]}
d2=[{"marker":"00f_125","1":"T"},
{"marker":"00f_126", "1":"G"},
{"marker":"00f_127","1":"T"}]
I would like to replace when there is only an integer present. This is the output I would like:
d3={"00f_125":["A","2","3"],
"00f_126":["G","2","3"],
"00f_127":["T","2","3"],
"00f_128":["T","2","3"]}
Is this possible? Any help would be appreciated.
This is one approach using a simple iteration
Ex:
d1={"00f_125":["A","2","3"],"00f_126":["1","2","3"],"00f_127":["T","2","3"],"00f_128":["T","2","3"]}
d2=[{"marker":"00f_125","1":"T"},{"marker":"00f_126", "1":"G"},{"marker":"00f_127","1":"T"}]
for i in d2:
if i["marker"] in d1:
if d1[i["marker"]][0] in i:
d1[i["marker"]][0] = i[d1[i["marker"]][0]]
Output:
{'00f_125': ['A', '2', '3'],
'00f_126': ['G', '2', '3'],
'00f_127': ['T', '2', '3'],
'00f_128': ['T', '2', '3']}
It's pretty simple to do in-place on d1:
for elem in d2:
marker = elem["marker"]
if "1" in d1[marker]: # is that specific int in d1?
i = d1[marker].index("1") # if so, then find its index
d1[marker][i] = elem["1"] # and replace it with your desired value
This is, of course, an oversimplification. If you have more than one key besides "marker", you might want to put all of those keys into a list within that dict (e.g.
d2=[{"marker": "00f_125", "modifications": ["1": "G", "2": ...]}, ...]
), which would make it easier to iterate through them without hardcoding the value like I did above.
If you wanted to produce a separate dict d3 without modifying the original, then you could make a shallow or deep copy of d1 before doing this.
d3 = d1.copy()
for i in d2:
key = i['marker']
if key in d3.keys():
if (d3[key][0].isdigit()):
d3[key][0] = i['1']
print(d3)
You can copy d1 to d3 and directly make changes in d3. For every list in d2, if the marker value is present in d3, then check if the first element is an integer. If it is, then replace that number with the character value.
This is the output of the above code.
d3 = {'00f_126': ['G', '2', '3'],
'00f_125': ['A', '2', '3'],
'00f_127': ['T', '2', '3'],
'00f_128': ['T', '2', '3']}
This is another generic approach without specifying the keys
for i in d2:
for key in i.keys():
if i.get(key) in d1:
lis_val = d1.get(i.get(key))
if key in lis_val:
lis_val[lis_val.index(key)] = i.get(key)
Output:
{
'00f_125': ['A','2','3'],
'00f_126': ['G','2','3'],
'00f_127': ['T','2','3'],
'00f_128': ['T','2','3']
}
I have done a large amount of searching but cannot find what I am after. I am using Iron Python.
I have a large list of strings (MyList) that I have extracted and I would like to see if there are values that contain the Items in the SearchStrings Dictionary. The searchStrings Dictionary could have over 500 items within.
MyList = ["123steel","MylistConcrete","Nothinginhere","45","56","steel","CONCRETE"]
SearchStrings = {'concrete' : 'C','CONCRETE' : 'C','Steel' : 'S', 'STEEL' : 'S'}
I need to return the index and then matching code from the SearchString.
i.e If we find 'MylistConcrete' i will know the index '1' and can return 'C'
I hope this makes sense to everyone. Let me know if you need any clarification
Thanks in Advance,
Geoff.
First of all, I'd suggest you to use string.lower() to eliminate case dependencies in the search. This will make your dictionary smaller and more manageable.
Then you can use a simple map function to create a new array with your values while preserving the index (or alter the original should you require that).
MyList = ["123steel","MylistConcrete","Nothinginhere","45","56","steel","CONCRETE"]
SearchStrings = {'concrete' : 'C', 'steel' : 'S'}
def check_search_strings(x):
for k, v in SearchStrings.items():
if k in x.lower():
return v
return None
indexes = list(map(check_search_strings, MyList))
print (indexes)
Iterate over your items in MyList and check for every item (lowercase) if any of the dict's (lowercase) keys is in it. Then replace.
This assumes that you don't have different values for identical words as keys (except for lower- / uppercase difference)
my_list = ["123steel", "MylistConcrete", "Nothinginhere", "45", "56", "steel", "CONCRETE"]
search_strings = {'concrete': 'C', 'CONCRETE': 'C', 'Steel': 'S', 'STEEL': 'S'}
for i in range(len(my_list)):
for k, v in search_strings.items():
if k.lower() in my_list[i].lower():
my_list[i] = v
break # avoids completing the loop if first item is found
print(my_list)
The result is
['S', 'C', 'Nothinginhere', '45', '56', 'S', 'C']
for m in MyList :
for k in SearchStrings :
if k.lower() in m.lower() :
print 'found', k, 'in', m, 'result', SearchStrings[k]
For example, I have this tuple:
data =(('name/score','game1', 'game2', 'game3', 'game4', 'game5'),('A','1','2','3','4','5'),('B','6','7','8','9','10'),('C','11','12','13','14','15'))
so data is a tuple that contains 4 smaller tuples that contains strings.
Actually data is a 'table' which shows the name A, B, C and D and their respective scores.
How to manipulate data, so that I extra informations I want in data?
For example,
1. how to split data into smaller tuples such as
tuple1 = ('name/score','game1', 'game2', 'game3', 'game4', 'game5')
tuple2 = ('A','1','2','3','4','5')
and so on?
How to remove the 'names', which are A, B and C in each smaller tuple?
I did it by slicing:
newtuple = tuple1[1:]
Just wondering if there is a recursive way or iterative way to do it, cause I dont really get the idea of iteration and recursion.
Is there anyway to define a function which can retrieve the data I want?
for example, I want to know to score of A in game 3, the function should return "3".
data =(('name/score','game1', 'game2', 'game3', 'game4', 'game5'),('A','1','2','3','4','5'),('B','6','7','8','9','10'),('C','11','12','13','14','15'))
The first element of your tuple is a sort of header (like in an excel file the first line).
You want to construct a dictionary of dictionaries where the first level keys are the users (A, B, C, etc.) and the second level of dictionaries have keys like game1, game2, etc. with the value representing the score reached in the given game.
D = dict((t[0], dict(zip(data[0][1:], t[1:]))) for t in data[1:])
dict(zip(data[0][1:], t[1:])) is the part where you create a dictionary from every tuple of data (starting the second tuple) as values using keys from the first tuple of data ("game1", "game2", etc.). We deliberately ignore the very first element of all tuples: "name/score" is ignored, and the user names "A", "B", etc. are also ignored.
Then we "attach" to each dictionary obtained above to a key which is the username: (t[0], dict(zip.... and we obtain a tuple.
Finally from the list of tuples we create a dictionary using the dict builtin function.
The above code will convert your input tuple of tuples to a dictionary of dictionaries like:
{'A': {'game1': '1', 'game2': '2', 'game3': '3', 'game4': '4', 'game5': '5'},
'B': {'game1': '6', 'game2': '7', 'game3': '8', 'game4': '9', 'game5': '10'},
'C': {'game1': '11', 'game2': '12', 'game3': '13', 'game4': '14', 'game5': '15'}}
To get the score of user A in game3 you write:
>>D["A"]["game3"]
3
Since you commented that you don't want to use dictionaries, here is a function that should satisfy your needs:
def get_score(D, user, game):
i = D[0].index(game)
for t in D[1:]:
if t[0] == user:
return t[i]
print get_score(data, "A", "game3")