# algorithm « List « Python Data Type Q&A

Home
Python Data Type Q&A
 1 array 2 date 3 dictionary 4 Format 5 integer 6 List 7 numpy 8 regex 9 string 10 tuple
 Python Data Type Q&A » List » algorithm

### 1. Getting the lesser n elements of a list in Python    stackoverflow.com

I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs ...

### 2. Algorithm to keep a list of percentages to add up to 100%    stackoverflow.com

(code examples are python)
Lets assume we have a list of percentages that add up to 100:

``````mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0]
``````
Some values of mylist may ...

### 3. Python: For each list element apply a function across the list    stackoverflow.com

Given [1,2,3,4,5], how can i do something like 1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 I would like to store all the results, find the minimum, and return the two numbers used to ...

### 4. Splitting up a list into parts of balanced lengths    stackoverflow.com

I need an algorithm which given a list `L` and a number `N`, returns a list of `N` smaller lists where the sublists are "balanced". Examples:

``algo(range(1, 8), 3)  -> ...``

### 5. Python program to split a list into two lists with alternating elements    stackoverflow.com

Can you make it more simple/elegant?

``````def zigzag(seq):
"""Return two sequences with alternating elements from `seq`"""
x, y = [], []
p, q ...``````

### 6. An ALGORITHM to create one list from "X" nested lists in Python    stackoverflow.com

What is the simplest way to create this list in Python? First, suppose I have this nested list:

``````oldList = [ [{'letter':'a'}], [{'letter':'b'}], [{'letter':'c'}] ]
``````
I want a function to spit out:
``newList = [ ...``

### 7. algorithm for list identification and parsing    stackoverflow.com

I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of ...

### 8. How can I sort a coordinate list for a rectangle counterclockwise?    stackoverflow.com

I need to sort a coordinate list for a rectangle counterclockwise, and make the north-east corner the first coordinate. These are geographic coordinates (i.e. Longitude, Latitude) in decimal form.1 For example, here ...

### 9. Python - Simple algorithmic task on lists (standard question for a job-interview)    stackoverflow.com

There are 2 input lists L and M, for example:

``````L =  ['a',     'ab',  'bba']
M = ['baa', 'aa',  'bb']
``````
How to obtain 2 non-empty output lists ...

### 10. Pythonic way of summing lists and lists of lists    stackoverflow.com

I'm trying to find a neat way of summing a list and a list of lists in the same function, so far I've got:

``````import operator
"""
Fails late for item = ['a', ...``````

### 11. Python: Get items at depth? (set library?)    stackoverflow.com

I have a nested list something like this:

``````PLACES = (
('CA', 'Canada', (
('AB', 'Alberta'),
...``````

### 12. Take the intersection of an arbitrary number of lists in python    stackoverflow.com

Suppose I have a list of lists of elements which are all the same (i'll use `int`s in this example)

``````[range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
``````
What would be a nice and/or efficient way to ...

### 13. Show me some cool python list comprehensions    stackoverflow.com

One of the major strengths of python and a few other (functional) programming languages are the list comprehension. They allow programmers to write complex expressions in 1 line. ...

### 14. Optimize algorithm for creating a list of items rated together, in Python    stackoverflow.com

given a list of purchase events (customer_id,item)

``````1-hammer
1-screwdriver
1-nails
2-hammer
2-nails
3-screws
3-screwdriver
4-nails
4-screws
``````
i'm trying to build a data structure that tells how many times an item was bought with another item. Not bought at the same time, ...

### 15. Determining the minimum of a list of n elements    stackoverflow.com

I'm having some trouble developing an algorithm to determine the minimum of a list of n elements. It's not the case of finding the minimum of an array of length n, ...

### 16. overlapping segments    stackoverflow.com

i have a huge list of two-element tuples which are coordinates of segments (start, end). In this way in a list below

``list = [ (1,4), (2, 3), (10, 20), (18, 45) ...``

### 17. I have a Python list of the prime factors of a number. How do I (pythonically) find all the factors?    stackoverflow.com

I'm working on a Project Euler problem which requires factorization of an integer. I can come up with a list of all of the primes that are the factor of a ...

### 18. Lowest common multiple for all pairs in a list    stackoverflow.com

I have some code that calculates the lowest common multiple for a list of numbers. I would like to modify this code to return a list of values that represents ...

### 19. Pythonic way to check if a list is sorted or not    stackoverflow.com

Is there a pythonic way to check if a list is already sorted in AESC or DESC. listtimestamps=[1,2,3,5,6,7] something like listtimestamps.isSorted() that returns True or False. EDIT: I want to input a list of ...

### 20. Sort distributed couples from two lists    stackoverflow.com

Having two lists, I want to get all the possible couples. (a couple can be only one element from list 1 and another from list 2) If I do a double "foreach" ...

### 21. Determine if a Python list is 95% the same?    stackoverflow.com

This question asks how to determine if every element in a list is the same. How would I go about determining if 95% of the elements in a list ...

### 22. How to get the smallest list of duplicate items from list of lists?    stackoverflow.com

I'm a Python newbie, I worked with list for 2 months and I have some questions. I have some list and they have duplicate items. I can get duplicate items between ...

### 23. Fast algorithm to calculate delta of two list    stackoverflow.com

I have two list of album names, ordered by some score.

``````albums_today = ['album1', 'album2', 'album3']
albums_yesterday = ['album2', 'album1', 'album3']
``````
How can I calculate the change of list order and get something like
``{'album1':1, ...``

### 24. List of minimal pairs from a pair of lists    stackoverflow.com

Given two lists of integers, generate the shortest list of pairs where every value in both lists is present. The first of each pair must be a value from the first ...

### 25. Python Sort List    stackoverflow.com

I'm currently python and quite new to this language. Sort List Based on Two CriteriaGiven a list of words, return a list with the same words in order of length (longest to ...

### 26. efficient comparison of values in a python list    stackoverflow.com

I have a list made out of tuples such as the one below:

``````(2809.3994479562093, 1032.5989696312365, 0.0), {'level': '2', 'id': '1'})
``````
having a sett of different values for the level key in the ...

### 27. Python Lists - How this connection is made?    stackoverflow.com

I'm reading about algorithms in Python but I'm also new in Python. I just need to understand how this is made:

``````a, b, c, d, e, f, g, h = range(8)
N = [
[b, ...``````

### 28. How to partition a list in smaller lists so that the odds of one element encountering another element is evenly distributed?    stackoverflow.com

I'm struggling with an algorithm to divide a group of contestants into smaller groups to make up rounds. Take for example a group of 20 people, which I want to divide ...

### 29. Calculating the similarity of two lists    stackoverflow.com

I have two lists: eg. a = [1,8,3,9,4,9,3,8,1,2,3] and b = [1,8,1,3,9,4,9,3,8,1,2,3] Both contain ints. There is no meaning behind the ints (eg. 1 is not 'closer' to 3 than it is to 8). I'm trying ...

### 30. splitting list in chunks of balanced weight    stackoverflow.com

I need an algorithm to split a list of values into such chunks, that sum of values in every chunk is (approximately) equals (its some variation of Knapsack problem, ...

### 31. partition of a list using dynamic programming    stackoverflow.com

I have posted a bit here related to a project I have been trying to work on and I keep hitting design problems and have to design from scratch. So ...

### 32. How to pick the largest number in a matrix of lists in python?    stackoverflow.com

I have a list-of-list-of-lists, where the first two act as a "matrix", where I can access the third list as

``````list3 = m[x][y]
``````
and the third list ...

### 33. The best way recognizing similarity of list lengths    stackoverflow.com

I have a dict containing lists under its keys:

``````dct = {'a': [1, 2, 3],
'b': [1, 2, 3, 4],
...``````

### 34. How to efficiently compare two unordered lists (not sets) in Python?    stackoverflow.com

``````a = [1, 2, 3, 1, 2, 3]
b = [3, 2, 1, 3, 2, 1]
``````
a & b should be considered equal, because they have exactly the same elements, only in different ...

### 35. Pascal's Triangle with iterative algorithm via list comprehension in Python    stackoverflow.com

Just for the sake of exercise I'm trying to find a way to express Pascal's Triangle with Python list comprehension, and do that in iterative way. I'm representing Pascal's ...

### 36. pythonic way to sort a list of lists by the last item of the inner list    stackoverflow.com

I have a list like this

``````[[x,y,1],[w,u,4],[m,n,3] ... [p,q,5]]
``````
I need to sort the outer list by the third (last) element of the inner list, the desired result would be:
``````[[x,y,1],[m,n,3],[w,u,4] ... [p,q,5]]
``````
What's the ...

### 37. Python topological sort using lists indicating edges    stackoverflow.com

Given lists: [1, 5, 6], [2, 3, 5, 6], [2, 5] etc. (not necessarily in any sorted order) such that if x precedes y in one list, then x precedes ...

 java2s.com  | Contact Us | Privacy Policy Copyright 2009 - 12 Demo Source and Support. All rights reserved. All other trademarks are property of their respective owners.