I have a dictionary in Python:
dict = {("s1", "a1"):1,("s1", "a2"):2,("s3", "a3"):3,("s1", "a3"):1}
Where key is a list(s, a) and value is an integer, so:
dict = {(s, a), i}
I want to pass in a specific s and return a where i is highest.
In the example above, I would expect to recieve "a2" if I passed in "s1".
So far I have the following:
print(max(dict, key=dict.get("s1"))[1])
However this is returning s3. How do I get this to work?
I would filter the dictionary first to only get items with a valid key and then find the key with the maximum value like this:
filtered_dict = {k:v for k, v in d.items() if k[0] == 's1'}
result = max(filtered_dict, key=filtered_dict.get)[1]
print(result)
Output:
a2
The structure of the data is not the ideal one for your given use-case. But if you have no other option, you need to visit every single element in the dict.
You visit each element once:
current_max = -999
result = None
for (k1,k2), value in dict.items():
if k1=="s1" and value > current_max:
result = k2
current_max = value
I need some help with python and dictionary.
So the basically idea is to create a list that will contain several values on a python dictionary.
I parse each key of the dic, and then if the number of values is > 1, I check wether these values contain a particular prefix, if so I put the values that do not have the prefix into a list.
Here is the dic:
defaultdict(<class 'list'>, {'ACTGA': ['YP_3878.3', 'HUYUII.1'], 'ACTGT': ['XP_46744.1', 'JUUIIL.2'], 'ACCTTGG': ['YP_8990.1'], 'ACCTTTT': ['YP_8992.1'], 'ATGA': ['YP_9000.1', 'YP_3222.1'], 'AAATTTGG': ['ORAAA.8', 'OTTTAX']})
and a here is the prefix_list = ["XP_","YP_"]
Let me explain it better:
I would like actually to create a new sequence_list with value content.
So the basicaly idea is going though each key and if there are > 1 values, I put the n-1 values into the sequence_list depending on some conditions.
Here is an exemple :
The first key is 'ACTGA' where there are 2 values: YP_3878.3 and HUYUII.1, then because HUYUII.1 does not have any prefix into the prefix_list, then I put it into the sequence_list:
print(sequence_list):
["HUYUII.1"]
The second key is 'ACTGT' where there are 3 values: XP_46744.1, JUUIIL.2 and JUUIIL.3, then because JUUIIL.2 and JUUIIL.3 do not have any prefix into the prefix_list, then I put them into the sequence_list:
print(sequence_list):
["HUYUII.1","JUUIIL.2","JUUIIL.3"]
The third key where n value > 1 is 'ATGAAA' where there are 3 values : 'YP_9000.1', 'YP_3222.1' and 'HUU3222.1', then because HUU3222.1 does not have any prefix into the prefix_list, then I put them into the sequence_list, AND because there is 2 values left with both prefix, I put the first one also in the sequence_list :
print(sequence_list):
["HUYUII.1","JUUIIL.2","JUUIIL.3","YP_9000.1","HUU3222.1"]
The fourth key where n value > 1 is 'AAATTTGG' where there are 2 values : 'ORLOP.8' and 'OTTTAX', then because both does not have a prefix into the prefix_list, I put the first one into the sequence_list:
print(sequence_list):
["HUYUII.1","JUUIIL.2","JUUIIL.3","YP_9000.1","HUU3222.1","ORAAA.8"]
So at the end I should get the sequence_list such as:
["HUYUII.1","JUUIIL.2","JUUIIL.3","YP_9000.1","HUU3222.1","ORAAA.8"]
Does someone have an idea? I tried something but it is quite difficult and maybe totally messy:
sequence_list=[]
for value in dedup_records.items():
if(len(value[1]))>1:
try:
length=len(value[1])
liste=value[1]
print("liste1",liste)
r = re.compile("YP_*.|XP_*.")
newlist = list(filter(r.match, liste))
if len(newlist)!=0:
print(newlist)
for i in newlist:
if i in liste:
liste.remove(i)
while len(newlist)>1:
liste.remove(newlist[0])
else:
while len(liste)>1:
liste.pop(0)
print(liste)
except :
continue
for i in liste:
sequence_list.append(i)
You can make your code much cleaner by using a function so that it is easier to read what is happening inside the loop.
Also, just personal preference, I'd suggest using list_ as a variable name instead of liste, As the misspellings can be tough to work with.
The approach is to first split every list into two groups: one with prefix, and one without prefix. After that, We just need to verify that there is at least 1 item with prefix (in which case, append every items except the last one with prefix, and append all non-prefixed items), otherwise we need to leave 1 non-prefixed item, and append all the others.
dedup_records = {'ACTGA': ['YP_3890.3', 'HUYUII.1'], 'ACTGT': ['XP_46744.1', 'JUUIIL.2','JUUIIL.3'], 'ACCTTGG': ['YP_8990.1'], 'ACCTTTT': ['YP_8992.1'], 'ATGAAA': ['YP_9000.1', 'YP_3222.1','HUU3222.1'], 'AAATTTGG': ['ORLOP.8', 'OTTTAX']}
prefix_list = ["XP_","YP_"]
def separate_items_with_prefix(list_, prefix_list):
'''separates a list into two lists based on prefix
returns two lists: one for items with prefix
another for items without prefix
'''
with_prefix = []
without_prefix = []
for item in list_:
if any(item.startswith(prefix) for prefix in prefix_list):
with_prefix.append(item)
else:
without_prefix.append(item)
return with_prefix, without_prefix
sequence_list = []
for val in dedup_records.values():
if len(val) <= 1:
continue #skip items with only upto 1 value in them
with_prefix, without_prefix = separate_items_with_prefix(val, prefix_list)
if with_prefix: #So there is at least 1 item in the list with prefix
sequence_list.extend(with_prefix[:-1])
sequence_list.extend(without_prefix)
else: #there are no items with a prefix in the list
sequence_list.extend(without_prefix[:-1])
Output:
print(sequence_list)
['HUYUII.1', 'JUUIIL.2', 'JUUIIL.3', 'YP_9000.1', 'HUU3222.1', 'ORLOP.8']
If I get youre code right, you want to achieve this:
prefix_list = ["XP_", "YP_"]
sequence_list = []
have_interesting_prefix = lambda v: any(
v.startswith(prefix) for prefix in prefix_list
)
for values in dedup_records.values():
if len(values) > 1:
sequence_list.extend(v for v in values if not have_interesting_prefix(v))
prefixed = filter(have_interesting_prefix, values)
if len(prefixed) > 1:
sequence_list.append(prefixed[0])
I need to print the keys + their values and it always prints the index of the key too, how can I fix that?
def task_3_4(something:str):
alphabet =list(string.ascii_letters)
i = 0
k=0
while i < len(alphabet):
dicts = {alphabet[i]: 0}
count = something.count(alphabet[i])
dicts[i] = count
if 0 < count:
for k in dicts:
print(k)
i = i+1
Based on the code it seems like you are trying to do some sort of counter of different characters in the string?
There is no index. your "index" is the "i" iterator you are using for your while loop. This simply makes a new key in dicts as called by dicts[i]. Thus when you call the print loop, it just iterates through and reads out I as well.
Try:
dicts[alphabet[i]] = count
Also your print function only prints out the key of the dict entry instead of the key-value pair. to do that you can try:
for k in dicts:
print(k,dicts[k])
Try reading up on the python docs for dicts.
https://docs.python.org/3/tutorial/datastructures.html
I have a dictionary that has keys with two values each. I need to update the second value as pass duplicate keys.
Clearly what I'm trying isn't working out.
if value1 not in dict.keys():
dict.update({key:(value1,value2)})
else:
dict.update({key:value1,+1)})
this just returned a diction with 1s for value 2 instead of incrementing by 1
The expression +1 doesn't increment anything, it's just the number 1
Also avoid using dict as a name because it's a Python built-in
Try structuring your code more like this:
my_dict = {} # some dict
my_key = # something
if my_key not in my_dict:
new_value = # some new value here
my_dict[my_key] = new_value
else:
# First calculate what should be the new value
# Here I'm doing a trivial new_value = old_value + 1, no tuples
new_value = my_dict[my_key] + 1
my_dict[my_key] = new_value
# For tuples you can e.g. increment the second element only
# Of course assuming you have at least 2 elements,
# or old_value[0] and old_value[1] will fail
old_value = my_dict[my_key] # this is a tuple
new_value = old_value[0], old_value[1] + 1
my_dict[my_key] = new_value
There may be shorter or smarter ways to do it, e.g. using the operator +=, but this snippet is written for clarity
I am trying to make a new dictionary from the one which I'm having.
`dict1 = {'A':{}, 'B':{}, 'C':{}, 'D':{}, 'E':{}}
for key, val in dict1.iteritems():
mytest = key
newConditionFlag = getSummary(mytest)
if newConditionFlag == 1:
make dict2`
where getSummary function is as follows:
`def getSummary(mytest):
newConditionFlag==0
bVal = mytest.CanClose()
if bVal == True:
pass
else:
newConditionFlag=1
return newConditionFlag`
If newConditionFlag == 1 for B, then my dict2 should be
dict2 = 'C':{}, 'D':{}, 'E':{}}.
In the same way if newConditionFlag == 1 for C,
dict2 = 'D':{}, 'E':{}}.
How should I do this?
I know for a simple condition if we want to access remaining values what we do is
`l = [1,2,3,4,5]
for i,j in zip(l, l[2:]):
print j`
But what should be done in case of dictionary? If we try it the same way it gives TypeError: unhashable type
The only way this could possibly make sense is to convert the dictionary to a list first. Do your processing on the list, then convert back to a dictionary.
l = sorted(dict1.items())
# do your thing
dict1 = dict(l)