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.
Related
This question already has answers here:
python order of elements in set
(2 answers)
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 26 days ago.
I have a list a:
a={4941, 4980, 3855, 4763, 4955}
I convert into a list:
b=list(a)
b now becomes [4980, 4955, 4763, 4941, 3855]
Sorry am I missing something? Eventhough set is an unordered structure why should it change the order ? Or is it the list function that is doing this?
Sorry if this is too naive a question! Many thanks.
This question already has answers here:
Is there a built in function for string natural sort?
(23 answers)
Python analog of PHP's natsort function (sort a list using a "natural order" algorithm) [duplicate]
(3 answers)
Closed 5 years ago.
I have a directory with 1600 photos and I need to save the path to each foto to a list and then to .txt file.
The photos are enumerated according to the position they should have in the list: img(0), img(1)... and I need this position to be kept.
What I obtain is this order, so now in list index 2 I have img(10):
img(0) img(1) img(10) img(100) img(1000) img(1001)...
img(2) img(2) img(20) img(200) img(2000) img(2001)...
Apparently, I'm the only one having this issue because I didn't find any discussion about this problem. Thank you very much for helping me.
As mentioned by others, the documentation does not guarantee any particular ordering. In your case it appears to be sorted alphabetically/lexicographically. "10" comes before "2" alphabetically. You'll have to prepend 0s to give every file the same number of digits to get the ordering you want if this behaviour appears to remain consistent on your machine.
For example, "002" will come before "010".
If you want to be safe (for example need to be able to port your code to other machines/OSes), you'll want to manually sort.
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.
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[:]
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)