This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
I know "how" to use the : operator to extract data from a list in python.
Eg:
l = [1,2,3]
print(l[0:1])
print(l[-1:])
Yields:
[1]
[3]
But, how does python literally interpret the :?
Why can't I set a variable equal to : and then call it?
Eg:
x = :
print(l[x])
Would yield:
[1, 2, 3]
Hoping someone who can go deeper on how python works can offer some insight.
Thanks!
: is shorthand for the slice class. You can assign instances of that class to variables for what you're trying to:
>>> l = [1, 2, 3]
>>> l[1:3]
[2, 3]
>>> l[slice(1, 3)]
[2, 3]
>>> x = slice(0, 2)
>>> l[x]
[1, 2]
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 months ago.
If I have:
list1 = [1, 2, 3]
x = "list1"
How do I use x to find the value in list1?
Since type(x) = <class str> and list(x) = ["l","i","s","t","1"]
Usually considered a bad style, but you can use globals()
list1 = [1, 2, 3]
x = "list1"
globals()[x]
What you could do instead is create a mapping-dictionary and use .get() to access the list like that:
mapping = {"list1": [1, 2, 3]}
mapping.get(x)
This question already has answers here:
Test if lists share any items in python
(9 answers)
Closed 1 year ago.
a = [1, 2, 3]
b = [4, 5, 1]
I need to check if one (or more) elements are common in both arrays.
Check the intersections of the sets and get the boolean:
>>> bool(set(a) & set(b))
True
>>>
Or not not:
>>> not not (set(a) & set(b))
True
>>>
Or just with any:
>>> any(i in b for i in a)
True
>>>
this is simple intersection concept which we used to learn in maths well here I have tried to demonstrate the function which take common from two list and return new list with common elements
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
a = [1, 2, 3]
b = [4, 5, 1]
print(intersection(a,b))
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 6 years ago.
>>> def double(x):
x += x
>>> a=[1,2,3,4]
>>> b=a
>>> double(b)
>>> print(a)
[1, 2, 3, 4, 1, 2, 3, 4]
>>> print(b)
[1, 2, 3, 4, 1, 2, 3, 4]
>>>
Could someone help me understand how the a list got doubled in this process? I understand how b's list doubled but not a
Thank you!
I think this is what your code looks like:
def double(x):
x += x
a=[1,2,3,4]
b=a
double(b)
print(a)
print(b)
Output:
[1,2,3,4,1,2,3,4]
[1,2,3,4,1,2,3,4]
And the reason is simply that if you have a list x = [1,2,3] and a list y = [6,7,8], then x + y gives you [1,2,3,6,7,8]. So the line x += x adds the elements of x to the end of itself, doubling it.
The reason that a doubles when b doubles is because python lists are mutable. You can find out more here: https://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
(1 answer)
Closed 9 years ago.
I have this scenario:
>>> y=[[1]]
>>> y=y*2
>>> y
[[1], [1]]
>>> y[0].append(2)
>>> y
[[1, 2], [1, 2]]
What I'd like to do is add the 2 into the first list within the outer list i.e. this is the desired output:
[[1, 2], [1]]
Doing:
y=[[1]]
y=y*2
creates a list with two references to the same list object:
>>> y=[[1]]
>>> y=y*2
>>> id(y[0]) # The id of the first element...
28864920
>>> id(y[1]) # ...is the same as the id of the second.
28864920
>>>
This means that, when you modify one, the other will be affected as well.
To fix the problem, you can use a list comprehension instead:
>>> y = [[1] for _ in xrange(2)] # Use range here if you are on Python 3.x
>>> y
[[1], [1]]
>>> id(y[0]) # The id of the first element...
28864920
>>> id(y[1]) # ...is different from the id of the second.
28865520
>>> y[0].append(2)
>>> y
[[1, 2], [1]]
>>>
Replace: y=y*2 by y.append([1]) to have different references.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
So i'm looking for sort of a layman/beginner explanation as to what this means:
L[i:i+lensub]
What does that mean by it's self? by the the way lensub = len(sublist) and L is list.
What that code is doing is using Explain Python's slice notation to slice L from position i to i+lensub.
The format for slice notation is [start:stop:step].
Below is basic demonstration:
>>> lst = [1, 2, 3, 4, 5]
>>> # Get positions 1 to 3
>>> lst[1:3]
[2, 3]
>>>