Related
If I have a list of pairs, such as
[(egg,man),(egg,dog),(cat,cactus),(cactus,elephant),(giraffe,chocolate),(tea,boat),(sky,lizard),(sky,tree),(helicopter,lizard)]
How would I retrieve the most amount of pairs possible without any single element being in more than one pair? What I would want to retrieve is something like this:
(egg,man),(cat,cactus),(giraffe,chocolate),(tea,boat),(sky,lizard)
So that every pair only contains unique elements, and I get the most possible.
This worked for me:
words = [('egg','man'),('egg','dog'),('cat','cactus'),('cactus','elephant'),('giraffe','chocolate'),('tea','boat'),('sky','lizard'),('sky','tree'),('helicopter','lizard')]
new_words = []
count = 0
for i in range(len(words)):
for k in range(len(words[i])):
if words[i][k] not in [item for t in new_words for item in t]:
count += 1
if count == 2:
new_words.append(words[i])
count = 0
The list new_words is the list that you want.
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 2 years ago.
I want to convert this list of list to list of strings here.
The idea behind converting is to remove the duplicates from the output.
Here is the original list: (The output from json.loads(file.json))
I have further simplified the dictionary output and got here a list of keys.
> [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg',
> u'xyzh'], u'xyzd', [u'xyza'],[u'xyxv']]
Now the list of keys contain another list inside it in some keys and duplicates as well.
I tried to convert this list of lists to a list of strings using something like
> [','.join(x) for x in list01]
But that did not work for me as i also got some single characters in the output as well like ['xyza'. 'x', 'y',...]
How will I be able to parse the list data here to get an output something like
['xyza', 'xyzb', 'xyzc',...,'xyzx']
since you have a list of list and string in it and you want list of string with no duplicate present.
create a resultant list, and then iterate through the parent list and check the type of each object, if it of type list then iterate through the sublist and check that string in sublist is present in the resultant list or not, if present then ignore else if not present then add it to list, same goes for the parent list iterate when it is not of type list.
res = [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg', u'xyzh'], u'xyzd', [u'xyza'],[u'xyxv']]
result =[]
for i in res:
if isinstance(i, list):
for j in i:
if j not in result:
result.append(j)
else:
if i not in result:
result.append(i)
print(result)
output:
['xyza', 'xyzb', 'xyzc', 'xyzd', 'xyze', 'xyzf', 'xyzg', 'xyzh','xyxv']
if you want to make it little faster, then instead of result as list , you can create it as a dictionary and without checking the condtition if string already present or not, you just update the dictionary and once you iterate through whole list, convert the dictionary keys to list and that is your answer.
You can try this:
mylist = [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg', u'xyzh'],` u'xyzd', [u'xyza'],[u'xyxv']]
new_list = []
for i in mylist:
if isinstance(i, list):
for j in i:
new_list.append(j)
else:
new_list.append(i)
remove duplicates:
new_list = list(set(new_list))
output:
['xyzc', 'xyzf', 'xyze', 'xyzg', 'xyxv', 'xyza', 'xyzh', 'xyzb', 'xyzd']
Not all items are lists itself so this should be checked. Also, turn to a set and back to a list for unique values:
l = [[u'xyza'], [u'xyzb'], [u'xyzc', u'xyzd', u'xyze', u'xyzf', u'xyzg', u'xyzh'], u'xyzd', [u'xyza'],[u'xyxv']]
new_list = []
for sublist in l:
if isinstance(sublist, list):
for item in sublist:
new_list.append(item)
else:
new_list.append(sublist)
new_list = list(set(new_list))
>>> new_list
['xyzc', 'xyza', 'xyzd', 'xyzb', 'xyzh', 'xyxv', 'xyzg', 'xyzf', 'xyze']
Let's call your list of lists the variable values
def to_str_list(values):
vars = list()
for x in values:
while type(x[0]) == list:
x = x[0]
for s in x:
if s not in vars:
vars.append(s)
return vars
[d['value'] for d in l]
If value might be missing, you can use
[d['value'] for d in l if 'value' in d]
I'm a beginner here and could really use some help.
I have to determine whether all elements in a two-dimensional list is unique. For example, if given a two-dimensional list my_list = [[1,2,2],[4,5,2],[7,2,9]], I have to write a code that would say, "this list does not have all unique elements" because of the multiple 2s. I have to write code using nested loops.
Here is what I have so far:
my_list = [[1,2,2],[4,5,2],[7,2,9]]
for row in my_list:
for num in row:
if row.count(num) > 1:
print("Duplicate")
else:
print("No duplicate", num)
This code can detect the duplicate 2 in the first list of my_list, but not the second list.
To do it without flattening the list of lists first, you can use a set that keeps track of items that has been "seen", so that you can determine that there is a duplicate as soon as the current item in the iteration is already in the set:
seen = set()
for sublist in my_list:
for item in sublist:
if item in seen:
print('Duplicate')
break
seen.add(item)
else:
continue
break
else:
print('No duplicate')
You need to flatten the list of list and find for duplicates. You can flatten a list of lists using itertools.chain.from_iterable
from itertools import chain
my_list = [[1,2,2],[4,5,2],[7,2,9]]
flat=list(chain.from_iterable(my_list)
if len(flat)==len(set(flat)):
print('No dups')
else:
print('Dups found')
Edit: Using for-loops without flattening
count={}
dups=False
for lst in my_list:
for k in lst:
count[k]=count.setdefault(k,0)+1
if count[k]>1:
dups=True
break
if dups:
print("dups found")
break
else:
print('No dups')
If you need a function to check that two dimensional array/list has duplicates with only nested loops:
def two_dim_list_has_duplicates(my_list):
unique = set()
for item in my_list:
for i in item:
if i in unique:
return True
seen.add(item)
return False
First, collect all the elements in a one list:
all_elements = [y for x in liste for y in x]
To check whether all elements are unique:
len(all_elements) == len(set(all_elements))
For a list of non-unique elements:
list(set([x for x in all_elements if all_elements.count(x)!=1]))
But if you insist on using nested loops, you still need to keep a unique list for this check. Example:
my_list = [[1,2,2],[4,5,2],[7,2,9]]
uniques = []
for row in my_list:
for num in row:
if num in uniques:
print("Duplicate",num)
else:
print("No duplicate", num)
uniques.append(num)
Output:
No duplicate 1
No duplicate 2
Duplicate 2
No duplicate 4
No duplicate 5
Duplicate 2
No duplicate 7
Duplicate 2
No duplicate 9
I have a dictionary {x: [a,b,c,d], y: [a,c,g,f,h],...}. So the key is one variable with the value being a list (of different sizes).
My goal is to match up each list against every list in the dictionary and come back with a count of how many times a certain list has been repeated.
I tried this but does not seem to work:
count_dict = {}
counter = 1
for value in dict.values():
count_dict[dict.key] = counter
counter += 1
You could map the lists to tuples so they can be used as keys and use a Counter dict to do the counting:
from collections import Counter
count = Counter(map(tuple, d.values()))
I am trying to iterate through a dictionary where each key contains a list which in turn contains from 0 up to 20+ sub-lists. The goal is to iterate through the values of dictionary 1, check if they are in any of the sublists of dictionary 2 for the same key, and if so, add +1 to a counter and not consider that sublist again.
The code looks somewhat like this:
dict1={"key1":[[1,2],[6,7]],"key2":[[1,2,3,4,5,6,7,8,9]]}
dict2={"key1":[[0,1,2,3],[5,6,7,8],[11,13,15]],"key2":[[7,8,9,10,11],[16,17,18]]}
for (k,v), (k2,v2) in zip(dict1.iteritems(),dict2.iteritems()):
temp_hold=[]
span_overlap=0
for x in v:
if x in v2 and v2 not in temp_hold:
span_overlap+=1
temp_hold.append(v2)
else:
continue
print temp_hold, span_overlap
This does obviously not work mainly due to the code not being able to check hierarchally through the list and sublists, and partly due to likely incorrect iteration syntax. I have not the greatest of grasp of nested loops and iterations which makes this a pain. Another option would be to first join the sublists into a single list using:
v=[y for x in v for y in x]
Which would make it easy to check if one value is in another dictionary, but then I lose the ability to work specifically with the sublist which contained parts of the values iterated through, nor can I count that sublist only once.
The desired output is a count of 2 for key1, and 1 for key2, as well as being able to handle the matching sublists for further analysis.
Here is one solution. I am first converting the list of lists into a list of sets. If you have any control over the lists, make them sets.
def matching_sublists(dict1, dict2):
result = dict()
for k in dict1:
assert(k in dict2)
result[k] = 0
A = [set(l) for l in dict1[k]]
B = [set(l) for l in dict2[k]]
for sublistA in A:
result[k] += sum([1 for sublistB in B if not sublistA.isdisjoint(sublistB) ])
return result
if __name__=='__main__':
dict1={"key1":[[1,2],[6,7]],"key2":[[1,2,3,4,5,6,7,8,9]]}
dict2={"key1":[[0,1,2,3],[5,6,7,8],[11,13,15]],"key2":[[7,8,9,10,11],[16,17,18]]}
print(matching_sublists(dict1, dict2))