Could someone explain what this means? #python [duplicate] - python

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]
>>>

Related

Python array sorting with unexpected result [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 1 year ago.
def sorted():
print(numList)
sortedList=numList
sortedList.sort()
print(numList)
print(sortedList)
Result:
[4,1,9,16,25]
[1,4,9,16,25]
[1,4,9,16,25]
Actually, I just sort the "sortedList" only, but result shows it sorted numList too. may I know the reason and solution.
You need to make a copy of the list, otherwise it is a pointer to the list and will act on both things.
https://www.programiz.com/python-programming/methods/list/copy
Here's how copy works. You'll want to copy your numList into sortedList and then sort it.
Example:
lst = [0, 1, 2, 3]
lst2 = lst
lst2[0] = 4
lst
>>> [4, 1, 2, 3]
Versus
lst = [0, 1, 2, 3]
lst2 = lst.copy()
lst2[0] = 4
lst
>>> [0, 1, 2, 3]

How is `:` interpreted? [duplicate]

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]

python list call by reference and value [duplicate]

This question already has answers here:
Modifying a list inside a function
(4 answers)
How do I pass a variable by reference?
(39 answers)
Understanding Python's call-by-object style of passing function arguments [duplicate]
(3 answers)
Closed 4 years ago.
I am new to python. I searched a lot but couldn't find the reason why this is happening. Could anyone please tell me the difference between these two?
My general question is when using a list in function is by reference and when is by value?
My test function is acting as call by reference and test2 is acting as call by value.
I know in python everything is an object, but with that, I couldn't understand the difference.
tnx
def test(my_list):
for i in range(len(my_list)):
my_list[i] = 5
def test2(my_list1):
my_list1 = [6, 6, 6]
a = [4, 4, 4]
print(a)
test(a)
print(a)
test2(a)
print(a)
output:
[4, 4, 4]
[5, 5, 5]
[5, 5, 5]

Introduction to Python - Help me understand simple execution [duplicate]

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/

How to convert a list of numbers into an integer? [duplicate]

This question already has answers here:
Convert list of ints to one number?
(19 answers)
Closed 6 years ago.
I have a list like:
list = [1, 2, 3]
I need to convert it to a triple digit like:
"123"
Use str.join and a generator expression:
''.join(str(digit) for digit in lst) # '123'
my_list = [1, 2, 3, 4, 5]
my_integer = "".join(map(str, my_list))
print(my_integer)
it will give me :
12345

Categories

Resources