python reassigment of variable [duplicate] - python

This question already has answers here:
Python objects confusion: a=b, modify b and a changes! [duplicate]
(3 answers)
Closed 8 years ago.
I'm new to python (using 2.7.6) and I'm sure this has been asked before, but I can't find an answer anywhere. I've looked at the python scoping rules and I don't understand what is happening in the following code (which converts three hex number strings to integers)
ls=['a','b','c']
d=ls
for i in range(0,len(d)):
d[i]=int(d[i],64)
print str(ls)
Why does the value of ls change along with the value of d?
I couldn't repeat this behavior with simple assignments. Thanks!

d is ls.
Not the value assigned to d equals the value assigned to ls. The two are one and the same. This is typical Python behavior, not just for lists.
A simple way to do what you want is
d = ls[:]

Related

String with a minus sign passed to a function in python [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 2 years ago.
Why is a string with a minus sign not recognised properly when passed to a function in python? The simple code
def f(s):
print(s)
if s is 'a-b':
print('I expect this to be printed.')
else:
print('Instead, this is printed.')
s = 'a-b'
if s is 'a-b':
print('Oustide everything is fine.')
f(s)
gives the output
Oustide everything is fine.
a-b
Instead, this is printed.
Can someone explain please why this happens? Without the minus sign everything is fine.
Python distinguishes between values that are equal and values that are identical. You should use the is operator rarely. In fact, you probably won't encounter too many cases where you use is other than is None.
Here, you want if s == 'a-b':.

How to find the position of every instance of a character in a list [duplicate]

This question already has answers here:
How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)
(3 answers)
Closed 6 years ago.
I have a program where I need to identify the location of every instance of the letter A in a quote. Something like I would do with quote.index("A"), but I need every instance of A, not just the first.
I know this question has been asked before but I'm very, very new to Python and I'm having trouble understanding the answers to those questions.
If anyone could give me a dumbed down explanation of how to do this, I'd be incredibly thankful because I'm utterly lost.
If i understand correctly, you have a string and you want to keep all A's locations in e different array.
Then you can try something like that.
quote = "some quote"
locs = []
for i in range(len(quote)) :
if quote[i] == 'A' :
locs.append(i)
print(locs)

Confused about the output from set() in Python(2.4.3) [duplicate]

This question already has answers here:
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 6 years ago.
I am a beginner in python and I am using python 2.4.3.
I have a question regarding to the order resulted from the set()function.
I understand set() will remove the the duplicate elements from a string and
[class set([iterable])
Return a new set object, optionally with elements taken from iterable.]1
But for example, when I do the following
a='abcdabcd'
set(a)
it returned a result of
set(['a','c','b','d'])
in stead of
set(['a','b','c','d'])
which I would actually expect.
Why is that? I am not able to understand how the output was generated.
Many thanks in advance.
A set is defined as an "unordered collection of unique elements" (see here). The set object in Python make no guarantees about ordering, and you should not expect nor rely on the order to stay the same.

lower versus length syntax in python? [duplicate]

This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.

Assign a list to variables [duplicate]

This question already has answers here:
How to expand a list to function arguments in Python [duplicate]
(4 answers)
Closed 10 months ago.
I have a python list:
x = ['aa', 'bb', 'cc']
The len() (or list length, which is 3 in this case) of this list can be any number, as it is basically coming from database. My question is: how can I assign each string member of this list into a separate variable automatically? The point over here is: I do not know the number of elements in the list, since they are always different.
Once this is resolved, I am trying to put it into a Python Google Charting (GChartWrapper's pie3d chart) function like this:
G.label(aa,bb,cc)
However, if I simply put the list in like:
G.label(x)
then it is naming only one section of the pie chart as the complete list.
You're doing it wrong.
G.label(*x)

Categories