this is the list into a list of tuple.
tup_new_lst = [[('A', '10'), ('B', '28D'), ('C', '14')],
[('B', '49C'), ('C', 'T26')],
[('A', '24K'), ('C', 'B28'), ('D', '54C')]]
Here i need separate the value base on follow format:
A: ['10','NaN','24K']
B: ['28D','49C','NaN']
C: ['14','T26','B28']
D: ['NaN','NaN','54C']
In this list of list. there in second list there No A's tuple.
I have done this following code for identify A's one:
a_lst = []
for tup_lst in tup_new_lst:
for a in tup_lst:
if a[0] == 'A':
a_lst.append(a[1])
else:
a_lst.append('NaN')
print(a_lst)
Output is:
['10', 'NaN', 'NaN', '24K', 'NaN']
desired output:
['10','NaN','24K']
Anyone please give support.
You may add a boolean to record if you found your reserched element in the list and append NaN to the list if not found as there:
a_lst = []
for tup_lst in tup_new_lst:
found = False
for a in tup_lst:
if a[0] == 'A':
a_lst.append(a[1])
found = True
if not found:
a_lst.append('NaN')
print(a_lst)
Related
An example interview question I have seen come up several times is
Given a mapping of employee_id to boss_id (for direct chain of command
only), return how many employees each boss indirectly manages.
I have found several solutions in SQL but can't find any examples of pythonic approaches.
More detailed problem definition:
Imagine that we have a dict containing the employee_id and the boss_id. In the example below C is manager of A, C is also manager of B, F is manager of C and so on. F is the manager of themselves and therefore the root of the hierarchy (i.e. the CEO). Each employee directly reports to exactly one manager.
emp_boss_map = dict([
("A","C"),
("B","C"),
("C","F"),
("D","E"),
("E","F"),
("F","F")])
Write a function to build a dictionary of the quantity of employees under each employee, not just their direct reports.
Output should be the following for example above:
{A:0,B:0,C:2,D:0,E:1,F:5}
How would one approach this problem in Python?
UPDATE:
Upon #prune 's suggestion I reversed the dict using
newdict = {}
for key, value in emp_boss_map.items():
for string in value:
newdict.setdefault(string, []).append(key)
Which returns
{'C': ['A', 'B'], 'E': ['D'], 'F': ['C', 'E', 'F']}
This is a closure problem on a tree. First and foremost, invert (reverse) the dictionary. From then, it's a straightforward count of nodes in each sub-tree, a recursive algorithm on each individual node: recur on each child, sum their return values, add 1 for each child, and that's the total direct reports for the current node.
Does that get you moving?
full_report = {}
def report_count(node):
global full_report
report_list = newdict[node]
count = len(report_list) # start with quantity of direct reports
# Find number of reports for each of those reports
for report in report_list:
count += report_count(report)
full_report[node] = count # add entry for this node to global count list
return count + len(report_list) # return this count to supervisor
I've left a lot of details open for you, such as finding all of the root nodes (from the original dictionary) and perhaps finding something better than a global variable for the master list.
Does that get you to the final solution?
You can solve this problem by reversing a dictionary and traversing it recursively till the end.
result_count=[]
def get_me_the_children(root):
if reverse_d.get(root,0) == 0:
return
for _ in reverse_d.get(root,0):
if _== root:
continue
else:
get_me_the_children(_)
print(_,end=" ")
result_count.append(_)
if __name__ == "__main__":
input_d = {"A":"A","B":"A","C":"B","D":"B","E":"D","F":"E"}
reverse_d={}
result_count_dict = {}
for key,values in input_d.items():
reverse_d.setdefault(values,[])
reverse_d[values].append(key)
#reverse_d = {'A': ['A', 'B'], 'B': ['C', 'D'], 'D': ['E'], 'E': ['F']}
for key,value in input_d.items():
result_count=[]
print(key,end=": ")
get_me_the_children(key)
result_count_dict[key] = len(result_count)
print("")
print(result_count_dict) #{'A': 5, 'B': 4, 'C': 0, 'D': 2, 'E': 1, 'F': 0}
"""output:
A: C F E D B
B: C F E D
C:
D: F E
E: F
F:
"""
def m1(dict):
new_dict = {}
keys_list = {keys for keys in dict.keys()}
for key, val in dict.items():
if key==val:
continue
if val not in new_dict.keys():
new_dict[val] = 1
else:
new_dict[val] = new_dict[val] + 1
if key in new_dict.keys():
new_dict[val] = new_dict[val] + new_dict[key]
for keys in keys_list:
if keys not in new_dict.keys():
new_dict[keys]=0
print(new_dict)
emp_boss_map={'A': 'C', 'B': 'C', 'C': 'F', 'D': 'E', 'E': 'F', 'F': 'F'}
m1(emp_boss_map)
I have a list of lists in the following format:
[['a'],['1'],['2'],['3'], ['b'],['4'],['5'],['6']]
My desired output is:
[['a', '1'], ['a', '2'], ['a','3'],['b', '4'],['b', '5'],['b', '6']]
or even better would be:
{'a':['1','2','3'], 'b':['4','5','6']}
Essentially, the "number values" are never the same size (think that a could include 1 2 and 3, and b could include 4 5 6 7 and 8, etc)
What would be the easiest way of doing this? Using regex?
Thanks
You can use a for loop and check if the element is a digit or not:
d = {}
for i in lst:
if not i[0].isdigit(): # Check if element is a digit. If not, add a key with the current value of i[0]
d[i[0]] = []
current = i[0]
else:
d[current].append(i[0])
Output:
>>> d
{'a': ['1', '2', '3'], 'b': ['4', '5', '6']}
This is assuming everything in the list is a string
I currently have a list of lists that looks like this:
My_List = [[This, Is, A, Sample, Text, Sentence] [This, too, is, a, sample, text] [finally, so, is, this, one]]
Now what I need to do is "tag" each of these words with one of 3, in this case arbitrary, tags such as "EE", "FF", or "GG" based on which list the word is in and then reassemble them into the same order they came in. My final code would need to look like:
GG_List = [This, Sentence]
FF_List = [Is, A, Text]
EE_List = [Sample]
My_List = [[(This, GG), (Is, FF), (A, FF), (Sample, "EE), (Text, FF), (Sentence, GG)] [*same with this sentence*] [*and this one*]]
I tried this by using for loops to turn each item into a dict but the dicts then got rearranged by their tags which sadly can't happen because of the nature of this thing... the experiment needs everything to stay in the same order because eventually I need to measure the proximity of tags relative to others but only in the same sentence (list).
I thought about doing this with NLTK (which I have little experience with) but it looks like that is much more sophisticated then what I need and the tags aren't easily customized by a novice like myself.
I think this could be done by iterating through each of these items, using an if statement as I have to determine what tag they should have, and then making a tuple out of the word and its associated tag so it doesn't shift around within its list.
I've devised this.. but I can't figure out how to rebuild my list-of-lists and keep them in order :(.
for i in My_List: #For each list in the list of lists
for h in i: #For each item in each list
if h in GG_List: # Check for the tag
MyDicts = {"GG":h for h in i} #Make Dict from tag + word
Thank you so much for your help!
Putting the tags in a dictionary would work:
My_List = [['This', 'Is', 'A', 'Sample', 'Text', 'Sentence'],
['This', 'too', 'is', 'a', 'sample', 'text'],
['finally', 'so', 'is', 'this', 'one']]
GG_List = ['This', 'Sentence']
FF_List = ['Is', 'A', 'Text']
EE_List = ['Sample']
zipped = zip((GG_List, FF_List, EE_List), ('GG', 'FF', 'EE'))
tags = {item: tag for tag_list, tag in zipped for item in tag_list}
res = [[(word, tags[word]) for word in entry if word in tags] for entry in My_List]
Now:
>>> res
[[('This', 'GG'),
('Is', 'FF'),
('A', 'FF'),
('Sample', 'EE'),
('Text', 'FF'),
('Sentence', 'GG')],
[('This', 'GG')],
[]]
Dictionary works by key-value pairs. Each key is assigned a value. To search the dictionary, you search the index by the key, e.g.
>>> d = {1:'a', 2:'b', 3:'c'}
>>> d[1]
'a'
In the above case, we always search the dictionary by its keys, i.e. the integers.
In the case that you want to assign the tag/label to each word, you are searching by the key word and finding the "value", i.e. the tag/label, so your dictionary would have to look something like this (assuming that the strings are words and numbers as tag/label):
>>> d = {'a':1, 'b':1, 'c':3}
>>> d['a']
1
>>> sent = 'a b c a b'.split()
>>> sent
['a', 'b', 'c', 'a', 'b']
>>> [d[word] for word in sent]
[1, 1, 3, 1, 1]
This way the order of the tags follows the order of the words when you use a list comprehension to iterate through the words and find the appropriate tags.
So the problem comes when you have the initial dictionary indexed with the wrong way, i.e. key -> labels, value -> words, e.g.:
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> [d[word] for word in sent]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'
Then you would have to reverse your dictionary, assuming that all elements in your value lists are unique, you can do this:
>>> from collections import ChainMap
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> d_inv = dict(ChainMap(*[{value:key for value in values} for key, values in d.items()]))
>>> d_inv
{'h': 2, 'c': 3, 'a': 1, 'x': 3, 'b': 2, 'd': 1}
But the caveat is that ChainMap is only available in Python3.5 (yet another reason to upgrade your Python ;P). For Python <3.5, solutions, see How do I merge a list of dicts into a single dict?.
So going back to the problem of assigning labels/tags to words, let's say we have these input:
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> sent = 'a b c a b'.split()
First, we invert the dictionary (assuming that there're one to one mapping for every word and its tag/label:
>>> d_inv = dict(ChainMap(*[{value:key for value in values} for key, values in d.items()]))
Then, we apply the tags to the words through a list comprehension:
>>> [d_inv[word] for word in sent]
[1, 2, 3, 1, 2]
And for multiple sentences:
>>> sentences = ['a b c'.split(), 'h a x'.split()]
>>> [[d_inv[word] for word in sent] for sent in sentences]
[[1, 2, 3], [2, 1, 3]]
I have a .csv file which looks like below:
A,3
B,2
C,5
D,1
I want to store the above values in a dictionary where an alphabet is the key. My code is below:
reader11 = csv.reader(open('file.csv'))
for row in reader11:
if row and row[0]:
excount[row[0]]=[i for i in row[1] if i]
print excount.items()
excount is:
[('A', ['3']), ('B', ['2']), ('C', ['5']), ('D', ['1'])]
The numbers are stored as strings in excount. How do I store them as numbers(like below)?
[('A', [3]), ('B', [2]), (C, [5]), ('D', [1])]
You can pass a string to the built-in int() function to turn it into a number. Something like this could work:
excount[row[0]] = [int(i) for i in row[1] if i]
For more about the int function, refer to the Python documentation.
I have this piece of code in Python:
if 'a' in my_list and 'b' in my_list and 'c' in my_list:
# do something
print my_list
Is there a more pythonic way of doing this?
Something like (invalid python code follows):
if ('a', 'b', 'c') individual_in my_list:
# do something
print my_list
if set("abc").issubset(my_list):
# whatever
The simplest form:
if all(x in mylist for x in 'abc'):
pass
Often when you have a lot of items in those lists it is better to use a data structure that can look up items without having to compare each of them, like a set.
You can use set operators:
if set('abc') <= set(my_list):
print('matches')
superset = ('a', 'b', 'c', 'd')
subset = ('a', 'b')
desired = set(('a', 'b', 'c'))
assert desired <= set(superset) # True
assert desired.issubset(superset) # True
assert desired <= set(subset) # False