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)
Related
This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 5 months ago.
For some reason when I print a list like
list = []
list.append("0")
list.append("1")
print(list[0])
the output will be ["0"]
My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR
Try this:
print(*list)
This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.
I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.
This question already has answers here:
Filtering a list of strings based on contents
(5 answers)
Closed 3 years ago.
I am trying to filter a list in python.
What i need is a way to limit what is beeing printed out from the list
Example:
I want to find all subjects containing "ECON"
from this list:
List = ["INFO100","INFO104","INFO110","INFO150","INFO125","ECON100", "ECON102"]
And i want to be able to print out the full name of the objects containing "ECON" (that means i want it to return "ECON100", "ECON102")
is there an easy way to do this?
for sub_string in List:
if "ECON" in sub_string:
print(sub_string)
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.
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[:]