Function that can apply list changes to other lists [duplicate] - python

This question already has answers here:
Sorting list based on values from another list
(20 answers)
Closed 1 year ago.
I am searching for a function that can apply position changes in a list to other lists.
For example:
liste1 = [3, 4, 1]
liste2 = ['a', 'b', 'c']
liste1.sort() #so liste1 = [1,3,4]
if liste1 = [1,3,4] :
liste2 = ['c', 'a', 'b']
I can change positions of liste2 myself, but I have 270,000 values, which is why I am searching for a function that can do it.

You could zip the lists together, sort them then unzip them.
liste1 = [3, 4, 1]
liste2 = ['a', 'b', 'c']
merged_lists = sorted(zip(liste1, liste2))
liste1, liste2 = zip(*merged_lists)
print(liste1, liste2, sep="\n")
OUTPUT
(1, 3, 4)
('c', 'a', 'b')

Related

Concentrate two lists alternating python [duplicate]

This question already has answers here:
How to zip two differently sized lists, repeating the shorter list?
(15 answers)
Closed 26 days ago.
I want to combine two lists in an alternating way in Python.
list1 = ['A']
list2 = [1, 2, 3, 4]
What I've tried:
combination = [x for y in zip(list1, list2) for x in y]
# Output:
['A', 1]
# Expected output:
['A', 1, 'A', 2, 'A', 3, 'A', 4]
How can I combine these two lists and get the expected output? (Preferably list comprehension)
One way is to use itertools cycle function:
from itertools import cycle
list1 = ['A']
list2 = [1, 2, 3, 4]
combination = [x for y in zip(cycle(list1), list2) for x in y]
This also works with
list1 = ['A', 'B']
list1*len(list2) will repeat the first list elements as many times as the length of the second list.
combination = [x for y in zip(list1*len(list2), list2) for x in y]
['A', 1, 'A', 2, 'A', 3, 'A', 4]

How to match two lists in Python with some condition using zip? [duplicate]

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 4 months ago.
I want to zip two lists. For example:
a = ['a', 'b', 'c', 'd']
b = [0, 2, 1, 4]
And I have to left in the list only objects that don't match with 0.
So for lists a and b I have to get list c:
c = ['b', 'c', 'd']
How can I do this with python? I tried using loop, but it works too long. Maybe I have to use zip() method, but I don't know exactly how to use it :(
You could combine zip with list comprehension as follows:
a = ['a', 'b', 'c', 'd']
b = [0, 2, 1, 4]
c = [x for (x,y) in list(zip(a,b)) if y != 0]
outputs: ['b', 'c', 'd']
It seems a perfect job for compress from itertools
from itertools import compress
a = ['a', 'b', 'c', 'd']
b = [0, 2, 1, 4]
print(*compress(a, b))
c = [x for x, y in zip(a, b) if y != 0]
This will work fine when both lists have the same length. If list a was longer and you'd want the extra elements out too, you could use this version:
from itertools import zip_longest
c = [x for x, y in zip_longest(a, b) if y != 0 and x is not None]

Extending lists of lists [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
How do I iterate through two lists in parallel?
(8 answers)
How do I concatenate two lists in Python?
(31 answers)
Closed 6 months ago.
I'm trying to extend two lists of lists in python, so that item 1 of the first list of lists extends with item 1 of the second list of lists, and so forth.
I'm new to this and self-taught, so could be missing something very simple, but I can't find an answer anywhere.
This is what I feel the code should be, but obviously not working.
list1[x].extend(list2[x]) for x in list1
What I'm trying to achieve is this:
list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [[a,b,c],[d,e,f],[g,h,i]]
output = [[1,2,3,a,b,c],[4,5,6,d,e,f],[7,8,9,g,h,i]]
Any ideas?
You can use zip:
list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [['a','b','c'], ['d','e','f'], ['g','h','i']]
output = [sub1 + sub2 for sub1, sub2 in zip(list1, list2)]
print(output)
# [[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
Use zip() to loop over the two lists in parallel. Then use extend() to append the sublist from list2 to the corresponding sublist in list1.
for l1, l2 in zip(list1, list2):
l1.extend(l2)
print(list1)
list1[x].extend(list2[x]) does achieve the result you want for one pair of lists, if list1[x] is [1, 2, 3] and list2[x] is [a, b, c], for example.
That means x has to be an index:
x = 0
list1[x].extend(list2[x])
# now the first list in `list1` looks like your desired output
To do that for every pair of lists, loop over all possible indexes x – range(upper) is the range of integers from 0 (inclusive) to upper (exclusive):
for x in range(len(list1)):
list1[x].extend(list2[x])
# now `list1` contains your output
To produce a new list of lists instead of altering the lists in list1, you can concatenate lists with the + operator instead:
>>> [1, 2, 3] + ['a', 'b', 'c']
[1, 2, 3, 'a', 'b', 'c']
output = []
for x in range(len(list1)):
output.append(list1[x] + list2[x])
And finally, the idiomatic Python for this that you’ll get around to someday is:
output = [x1 + x2 for x1, x2 in zip(list1, list2)]
I have simply used list comprehension with list addition, while zipping the correlated lists together
list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [['a','b','c'], ['d','e','f'], ['g','h','i']]
list3 = [l1+l2 for l1, l2 in zip(list1, list2)]
print(list3)
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]

use threshold on a list of numbers to subset corresponding elements from other list of objects

I have two lists of lists which share the same dimension (same length, each list in the list have the same number of elements)
list1 looks like this:
list1 = [[1,2,3],[2,3,4],[4,5,2],[4,0,9]]
list2 looks like this:
list2 = [['a''b','c'],['a','d','e'],['a','f','b'],['p','o','a']]
let's say I have a threshold x = 3
I want to filter out elements from list 2 which have the same position of those elements in list1 which fall below x = 3
so for instance for x = 3 I would want to obtain:
list3 = [[],['e'],['a','f'],['p','a']
how can I do so?
thank you for your help
You can use zip to walk the two lists (and sublists) together and filter the values in list2 that satisfy the condition in the corresponding position in list1:
out = [[j for i, j in zip(li1, li2) if i>3] for li1, li2 in zip(list1, list2)]
Output:
[[], ['e'], ['a', 'f'], ['p', 'a']]
I would use the Python zip function to "merge" the two lists.
list1 = [[1,2,3],[2,3,4],[4,5,2],[4,0,9]]
list2 = [['a','b','c'],['a','d','e'],['a','f','b'],['p','o','a']]
threshold = 3
list3 = [
[char for value, char in zip(sub_one, sub_two) if value > threshold]
for sub_one, sub_two in zip(list1, list2)
]
print(list3)
This outputs
[[], ['e'], ['a', 'f'], ['p', 'a']]
The outer zip merges list1 and list2 into:
[([1, 2, 3], ['a', 'b', 'c']),
([2, 3, 4], ['a', 'd', 'e']),
([4, 5, 2], ['a', 'f', 'b']),
([4, 0, 9], ['p', 'o', 'a'])]
while the inner zip merges e.g. [1,2,3] and ['a','b','c'] into [(1, 'a'), (2, 'b'), (3, 'c')]. Then, the inner list comprehension can use a simple filtering with if value > threshold.

Sorting Key in Python3 [duplicate]

This question already has answers here:
Sort list of lists ascending and then descending [duplicate]
(3 answers)
Closed 3 years ago.
I have a list of lists as myList = [['a',1],['b',4],['a',5], ['b',6]].I want to sort this list alphabetically with first element as the key which I can do as myList.sort(key=lambda x:x[0]) which changes myList to [['a', 1], ['a', 5], ['b', 4], ['b',6]]. But if the first elements of the inner lists are the same, I want to sort it in descending order of the second element of the inner list and get the result as [['a', 5], ['a', 1], ['b', 6], ['b',4]]. What is the cleaner way to do it? I could have done this in Javascript as
myList.sort((a,b) => {
if(a[0] == b[0]){
return b[1] > a[1]
} else {
return b[0] < a[0]
}
})
How do I achieve this in python?
Use a tuple as a key:
myList.sort(key=lambda x:(x[0], -x[1]))
# myList = [['a', 5], ['a', 1], ['b', 6], ['b', 4]]

Categories

Resources