Finding clusters in a python list [duplicate] - python

This question already has answers here:
Detecting consecutive integers in a list [duplicate]
(8 answers)
Grouping / clustering numbers in Python
(3 answers)
Closed 6 years ago.
I have a sorted python list which looks like:
myList = [1,2,3,7,8,9,12,13,14,15]
The consecutive elements with difference 1 form the cluster. I need to dig these clusters out.
In the above example the clusters would be 1,2,3; 7,8,9; 12,13,14,15
Is there a way other than to write a for loop and see where the difference between consecutive elements is > 1
Edit: The list I have is large so I want to avoid using a for loop as much as I can unless that is the only way out

Related

Python data structure where largest element is always kept first [duplicate]

This question already has answers here:
Does python have a sorted list?
(9 answers)
What do I use for a max-heap implementation in Python?
(19 answers)
Closed 6 months ago.
I need a data structure like a dict where I can insert elements with a key and then efficiently take out largest element by value when I need it.

Removing Python characters from a list [duplicate]

This question already has answers here:
Slice every string in list in Python
(3 answers)
What is the purpose of the two colons in this Python string-slicing statement?
(1 answer)
Closed 4 years ago.
If i had a list as an example:
a = ['Hello_1.txt', 'Hello_2.txt']
In Python is it possible to somehow remove the first 5 characters ('Hello')
and the last 3 characters ('txt') from each of the items in the list ?
You could use a list-comprehension and string slicing:
[s[5:-3] for s in a]
which gives what you describe (not sure this is the neatest output though!)
['_1.', '_2.']

What does [[]] do ? [duplicate]

This question already has answers here:
Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
(1 answer)
Why does appending to one list also append to all other lists in my list of lists? [duplicate]
(4 answers)
Python: fastest way to create a list of n lists
(5 answers)
Closed 4 years ago.
I don't know python I am working through 'Exercises in Programming Style' and translating to javascript. I can understand most of the python but this line flabbergasts me .
# Let's use the first 25 entries for the top 25 words
data = data + [[]]*(25 - len(data))
some context: the challenge is to use self imposed memory limitations represented here by the data array. So here she just cleared out data that is no longer used to make room for the 25 most frequent word. What is she doing here ?

How to create N list with a funtion(Not by writing it) that holds a dynamic name such as list1, list2, list3,... listn? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I'm a rookie at Python so I don't know much about the limits of the language, but it seems that you can't write a function or a program that creates a list on its own.
I'm working with binary lists. I'm working with 7 bits so, as you can guess, I need 128 different lists to store all possibilities.
Let me know if its possible, or if not please let me know why and what else could work for my needs.
def list_creator:
n=128
"create a new_list n times with 7 of length"
"Should display something like this"
list1=[None]*7
list2=[None]*7
list(n)=[None]*7
You should make a list containing 128 empty lists and add to those.

incrementing elements of 2d list in python [duplicate]

This question already has answers here:
Python list problem [duplicate]
(2 answers)
Nested List Indices [duplicate]
(2 answers)
Closed 9 years ago.
I've noticed something strange when trying to implement elements of a 2d list in python. Here is my code
n_dk=2*[5*[0]]
n_dk[0][0]+=1
print n_dk
I would expect the output to be
[[1,0,0,0,0],[0,0,0,0,0]]
but the actual output is
[[1,0,0,0,0],[1,0,0,0,0]]
can anyone tell me what I am doing wrong. btw I used a numpy array instead and it worked the way I wanted it to.

Categories

Resources