# before
my_list = ['a', 'b', 'c']
other_list = [1, 2, 3]
exchange_with(my_list, other_list)
# after
my_list == [3, 2, 1]
other_list == ['c', 'b', 'a']
So I just finished solving the problem and I thought i had solved it with my solution below which was
def exchange_with(a, b):
a , b = b , a
print(a[::-1])
print("\n")
print(b[::-1])
and the solution comes out right but when I submit it it says
Test Results:
Log (MY RESULTS)
['c', 'b', 'a']
['7', '6', '5', '4', '3', '2', '1']
====================================================
['1', '2', '3', '4', '5', '6', '7'] should equal ['c', 'b', 'a']
['a', 'b', 'c'] should equal ['7', '6', '5', '4', '3', '2', '1']
I am very confused on what I did wrong I swapped each list and reversed it but it won't let me submit my solution
from what I understand here:
# before
my_list = ['a', 'b', 'c']
other_list = [1, 2, 3]
exchange_with(my_list, other_list)
# after
my_list == [3, 2, 1]
other_list == ['c', 'b', 'a']
you have to mutate the lists and the 'Test results' will evaluate your mutation, in which case you can try:
def exchange_with(my_list, other_list):
my_list[:], other_list[:] = other_list[::-1], my_list[::-1]
how it works (should not be used in your solution):
my_list = ['c', 'b', 'a']
other_list = ['7', '6', '5', '4', '3', '2', '1']
exchange_with(my_list, other_list)
print(my_list)
print(other_list)
output:
['1', '2', '3', '4', '5', '6', '7']
['a', 'b', 'c']
Related
I have a list like this
items= ['e', '4', 'e', 'e', '4', '5', '4', '8', 'a', '8', '6', 'd', '8', 'a', 'e', '1', 'b', '6', '2', '1', '6', 'a', 'a', 'a', '2', 'b', 'd', '6', '7', '7', '9', '2']
I want to edit the list so that every 4 items in the list get merged like this
items=['e4ee', '4548', 'a86d', '8ae1', 'b621', '6aaa', '2bd6', '7792']
Edit: My mistake for wording. By not creating a new list I meant by putting the arranged elements into a separate list like this
items = ['e', '4', 'e', 'e', ...
items2 = ['e4ee', '4548', ...
You could do it like this although this does create a new list:
items = ['e', '4', 'e', 'e', '4', '5', '4', '8', 'a', '8', '6', 'd', '8', 'a', 'e', '1', 'b', '6', '2', '1', '6', 'a', 'a', 'a', '2', 'b', 'd', '6', '7', '7', '9', '2']
items = [''.join(items[i:i+4]) for i in range(0, len(items), 4)]
print(items)
Output:
['e4ee', '4548', 'a86d', '8ae1', 'b621', '6aaa', '2bd6', '7792']
If you absolutely do not want to create a new list (as stated):
result_len = len(items) // 4
for i in range(result_len):
j = (i*4)
items[i] = ''.join(items[j:(j+4)])
for i in range(len(items) - 1, result_len - 1, -1):
del items[i]
Does exactly len(items) iterations and never creates a new list.
The first loop updates the first result_len items in the list to the desired values. The second one deletes the rest of the items from it.
EDIT: Whoops, had a bug there. Now it should be correct.
I have data that exports in a string
output = '012345678910abcdefghijkl'
cleaned_output = [output[index:index + 4] for index in range(0, len(output), 4)]
cleaned_output = [cleaned_output[i][item] for i in range(0, len(cleaned_output)) for item in range(0,len(cleaned_output[i]))]
Which returns:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
However, I am looking to return the below, any ideas on where I am going wrong?
[['0', '1', '2', '3'], ['4', '5', '6', '7'], ['8', '9', '1', '0'], ['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]
You should just split your input into chunks of 4 and then convert them directly to lists:
cleaned_output = [list(output[i:i+4]) for i in range(0, len(output), 4)]
Output:
[['0', '1', '2', '3'], ['4', '5', '6', '7'], ['8', '9', '1', '0'], ['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]
The inputs for the function are
a list of characters, eg: ['a','1']
length of combinations
The function should output a list of all possible character combinations as a list.
For example, for input ['a','1'] and length of 2, the function should output:
[['a','a'],
['a','1'],
['1','a'],
['1','1']]
and if the length is 3, the output should be:
[['a','a','a'],
['a','a','1'],
['a','1','a'],
['a','1','1'],
['1','a','a'],
['1','a','1'],
['1','1','a'],
['1','1','1']]
You can use itertools.product with the repeat parameter:
from itertools import product
data = ['a', '1']
n = 3
print(list(list(p) for p in product(data, repeat=n)))
This gives an output of:
[['a', 'a', 'a'], ['a', 'a', '1'], ['a', '1', 'a'], ['a', '1', '1'],
['1', 'a', 'a'], ['1', 'a', '1'], ['1', '1', 'a'], ['1', '1', '1']]
I have a dict like this:
dict = defaultdict(list, {'a': [['1', '2', 'A', 'cat'],
['1', '3', 'A', 'dog']],
'b': [['1', '2', 'A', 'cat'],
['1', '3', 'A', 'dog']],
'c': [['1', '2', 'A', 'cat'],
['2', '2', 'A', 'snake'],
['2', '2', 'A', 'bird']]}
I'd like to get all pairwise comparisons for overlapping values using the full list for each value. (Every position in the value list must match for it to be considered a match between keys)
Since a and b share ['1', '3', 'A', 'dog'] and c doesn't, a/b: ['1', '3', 'A', 'dog'].
a, b, c, all share ['1', '2', 'A', 'cat'], a/b/c: ['1', '2', 'A', 'cat'].
Only c has ['2', '2', 'A', 'snake'], so c: ['2', '2', 'A', 'snake']
Preferred output is a dictionary combining the above, something like
combine_dict = {'a/b': ['1', '3', 'A', 'dog'], 'a/b/c': ['1', '2', 'A', 'cat'], 'c': [['2', '2', 'A', 'snake'], ['2', '2', 'A', 'bird']]}
You can use collections.defaultdict:
import collections
d = {'a': [['1', '2', 'A', 'cat'], ['1', '3', 'A', 'dog']], 'b': [['1', '2', 'A', 'cat'], ['1', '3', 'A', 'dog']], 'c': [['1', '2', 'A', 'cat'], ['2', '2', 'A', 'snake'], ['2', '2', 'A', 'bird']]}
new_d = collections.defaultdict(list)
for a, b in d.items():
for i in b:
new_d[tuple(i)].append(a)
new_r = collections.defaultdict(list)
for a, b in new_d.items():
new_r['/'.join(b)].append(list(a))
new_result = {a:b[0] if len(b) == 1 else b for a, b in new_r.items()}
Output:
{'a/b/c': ['1', '2', 'A', 'cat'], 'a/b': ['1', '3', 'A', 'dog'], 'c': [['2', '2', 'A', 'snake'], ['2', '2', 'A', 'bird']]}
Say i have
List=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
i know that if i call List[0][0] i will get [['a', 'b'], ['c', 'd'], ['e', 'f']], and so on.
Is there any built-in or external python function(suppose its func(a)) or way to get the nested list element a one level back?
So if i call func(List[0][1]) those function will return List[0] or when i call func(List[1][0][1]) those function will return List[1][0] but if i call func(List) it will return List since it's already at the root. I've been searching for this kind of problem for hours but still couldn't find the solution.
You can use the following recursive function:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Testing it out:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Test Case 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Test Case 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Test Case 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Test Case 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Result:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None