reverse a list of Ordered pair in python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a list of Ordered pair in python like [(1,45),(21,28),(43,110),(4,81)] and I want to reverse each items in list like [(45,1),(28,21),(110,43),(81,4)].
what should I do?

Just use a list comprehension with tuple unpacking, it's as simple as:
ls = [(1,45),(21,28),(43,110),(4,81)]
new_ls = [(m, n) for n, m in ls]
print(new_ls)
Output:
[(45, 1), (28, 21), (110, 43), (81, 4)]

Try this :
list_ = [(1,45),(21,28),(43,110),(4,81)]
new_list = [item[::-1] for item in list_]
Output :
[(45, 1), (28, 21), (110, 43), (81, 4)]

l = [(1,45),(21,28),(43,110),(4,81)]
sol = list(map(lambda x:x[::-1], l))
print(sol)
output
[(45, 1), (28, 21), (110, 43), (81, 4)]

Related

Add int to index of 2d list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
These are my 2 arrays:
myList = [
'AAAbbbbbbbbbbbbbbAbbbbbb',
'AbAbbbAbbbbAAAbbbbbbbAbb',
'AbAbbbbbbbbAbAAAAbbbbbbb',
'AAAbbbbbbbbAbAAbAbbbbbbA',
'bbbbbAbbbbbAAAAbAbbbbbbb',
'bbbbbbbbbbbbbbAAAbbbbbbb'
]
res = [(0, 0), (1, 11), (2, 13), (3, 0), (4, 11), (5, 14)]
res gives the position where you can find "AAA" in the array "myList". The 2 numbers in the brackets stand for the Y and X axis(Y as the index and X as the position in the string from the given index.)
My target is it to add 1 to the first number of every bracket. Then I wanna check if at the new positions that I created by adding 1(the next index but same position in string) is the string "AbA". What do I have to do to achieve this?
Btw sorry for my bad english xD
If I understand your description correctly:
>>> myList = ['AAAbbbbbbbbbbbbbbAbbbbbb', 'AbAbbbAbbbbAAAbbbbbbbAbb', 'AbAbbbbbbbbAbAAAAbbbbbbb', 'AAAbbbbbbbbAbAAbAbbbbbbA', 'bbbbbAbbbbbAAAAbAbbbbbbb', 'bbbbbbbbbbbbbbAAAbbbbbbb']
>>> res = [(0, 0), (1, 11), (2, 13), (3, 0), (4, 11), (5, 14)]
>>> [myList[(y+1)%len(myList)][x:x+3] == "AbA" for y,x in res]
[True, True, False, False, False, False]
Because of the entry (5, 14) I've added a modulo to the check, to "loop-around" to the first entry when the y-index+1 is greater than the length of myList.

Sorting a list based on the valeus of outer and inner tuple(Python) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list of the form
l = [(98, (99, 1)),(97, (97, 3)),(97, (100, 3))]
I want to sort this list based on the first value of outer tuple and the first value of inner tuple:
(98, (99, 1)). I want to get the following result:
[(98, (99, 1)),(97, (100, 3)),(97, (97, 3))]
what is the best way to do it in python?
You can create a custom sorting function:
sorted(l, key=lambda x: (x[0], x[1][0]), reverse=True)
Yields:
[(98, (99, 1)), (97, (100, 3)), (97, (97, 3))]
As it happens,
l.sort(reverse=True)
will sort the list exactly as you want.

Python program to sort and count repeated no in list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Write a Python function histogram(l) that takes as input a list of integers with repetitions and returns a list of pairs as follows:
for each number n that appears in l, there should be exactly one pair (n,r) in the list returned by the function, where r is is the number of repetitions of n in l.
the final list should be sorted in ascending order by r, the number of repetitions. For numbers that occur with the same number of repetitions, arrange the pairs in ascending order of the value of the number.
For instance:
>>> histogram([13,12,11,13,14,13,7,7,13,14,12])
[(11, 1), (7, 2), (12, 2), (14, 2), (13, 4)]
>>> histogram([7,12,11,13,7,11,13,14,12])
[(14, 1), (7, 2), (11, 2), (12, 2), (13, 2)]
>>> histogram([13,7,12,7,11,13,14,13,7,11,13,14,12,14,14,7])
[(11, 2), (12, 2), (7, 4), (13, 4), (14, 4)]
The Counter object is perfect for this.
>>> from collections import Counter
>>> Counter([13,12,11,13,14,13,7,7,13,14,12])
Counter({13: 4, 12: 2, 14: 2, 7: 2, 11: 1})
Edit:
And if you want the result in a list of tuples sorted by value, you can do the following.
>>> count = Counter([13,12,11,13,14,13,7,7,13,14,12])
>>> sorted(count.items(), key=lambda c: c[1])
[(11, 1), (12, 2), (14, 2), (7, 2), (13, 4)]
Next time please share what you've tried yourself.
def make_histogram(lst):
new_lst = list(set([(i, lst.count(i)) for i in lst]))
new_lst.sort(key=lambda x: x[1])
return new_lst

Interval calculation in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am writing a program where I need to calculate the total watch time of a movie.
1st watch = (0,10)
2nd Watch =(13,18)
3rd watch =(15,23)
4th watch =(21,26)
Total movie watched=10+5+5+3=23 min
How can I implement this in Python
OK, the real challenge here is with the overlapping sequences. Sorry but your question is not very clear.
It is not optimal (see below for a better algorithm) but you can try:
l = [(0, 10), (13, 18), (15, 23), (21, 26)]
s = set()
for w in l:
s = s.union(range(*w))
d = len(s)
It should do the trick. It gives d = 23.
EDIT : better algorithm
l = [(0, 10), (13, 18), (15, 23), (21, 26)]
flat_list = sorted([(t[0], 1) for t in l] + [(t[1], -1) for t in l])
# flat_list == [(0, 1), (10, -1), (13, 1), (15, 1), (18, -1), (21, 1), (23, -1), (26, -1)]
duration = level = 0
start = None
for minute, level_inc in flat_list:
level += level_inc
if level == 0:
duration += minute - start
start = None
elif start is None and level == 1:
start = minute
assert(level == 0) # something is wrong otherwise
print("Duration is {}".format(duration))

Aligning two lists with duplicate keys [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This question is similar to "Aligning to Lists in python" question, but I have a problem with using a dictionary because of repeated numbers for potential keys.
Here is an example. Start with these 2 lists:
If I used a dictionary these would be the keys. [5,6,6,1,6,1,6,1,1,2,1,2,1,2,2,1]
[13,14,15,10,16,11,17,12,12,13,13,14,14,15,16,17]
I am able to rearrange the first list the way I want it, which is:
[5,6,6,6,6,1,1,1,1,1,1,2,2,2,2,1]
I want the second list to keep it's same alignment, to the first list and look exactly like this:
[13,14,15,16,17,10,11,12,12,13,14,13,14,15,16,17]
Notice that it matters that the list of potential keys has it's repeated values aligned by position with the corresponding values in the second list.
Like other people below your post, I don't completely understand your problem (could you be more specific about relation you want to obtain?), but maybe zip is the answer for your question:
>>> a = [5,6,6,6,6,1,1,1,1,1,1,2,2,2,2,1]
>>> b = [13,14,15,16,17,10,11,12,12,13,14,13,14,15,16,17]
>>> alignment = zip(a, b)
>>> alignment
[(5, 13), (6, 14), (6, 15), (6, 16), (6, 17), (1, 10), (1, 11), (1, 12), (1, 12), (1, 13), (1, 14), (2, 13), (2, 14), (2, 15), (2, 16), (1, 17)]
Edited:
key_list = [5,6,6,1,6,1,6,1,1,2,1,2,1,2,2,1]
values_list = [13,14,15,10,16,11,17,12,12,13,13,14,14,15,16,17]
zipped_lists = zip(key_list, values_list)
sorted_zip = sorted(zipped_lists)
pattern = [5,6,6,6,6,1,1,1,1,1,1,2,2,2,2,1]
temp_dict = {}
for key, value in sorted_zip:
if key not in temp_dict:
temp_dict[key] = [value]
else:
temp_dict[key].append(value)
final_list = []
for i in pattern:
final_list.append((i, temp_dict[i].pop(0)))
And, of course, final_list is your result.

Categories

Resources