What does [[]] do ? [duplicate] - python

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 ?

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.

Create a long list of characters (not numbers) with list comprehension [duplicate]

This question already has answers here:
Python: Short way of creating a sequential list with a prefix
(3 answers)
Create a list of strings with consecutive numbers appended
(6 answers)
Appending the same string to a list of strings in Python
(12 answers)
Closed 7 months ago.
In Python how can I need to create a long list that I'm trying to avoid typing, the list looks lis this
brands = ['_1','_2','_3'... '_998']
I can create the list of numbers with a for loop, but I'm trying to use list comprehension for the characters which should be faster.
Thanks!
my list=["_" + str(i) for i in range(1,999)]

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.

Finding clusters in a python list [duplicate]

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

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