Posts: 15
Threads: 3
Joined: Mar 2021
Apr-04-2021, 10:02 AM
(This post was last modified: Apr-04-2021, 10:07 AM by danlopek14q.)
Hello guys
I have a problem finding a solution for the lists.
I have a function all_list which takes two parameters (listas, column[]). Inside of the column parameter User chooses to pass for example two values [0,1] , those numbers correspond to the element inside of the list which should be display. For example : all_list (['Mars','Hot',22,True],column[0,1]. The display values should be ['Mars','Hot'] if no column value then all list should be display like ['Mars','Hot',22,True]
def all_list(listas,column[]):
#for no columns value
if column == []:
print(listas)
#how to do when user use [0,2]or [1,2] etc.
Posts: 15
Threads: 3
Joined: Mar 2021
Apr-04-2021, 10:23 AM
(This post was last modified: Apr-04-2021, 10:23 AM by danlopek14q.)
def all_list(listas,column[]):
#for no columns value
if column == []:
print(listas)
elif column == [0, 1]:
print([listas[0], listas[1]])
elif column == [0, 2]:
print([listas[0], listas[2]])
elif column == [0, 3]:
print([listas[0], listas[3]])
elif column == [1, 0]:
print([listas[1], listas[0]]) [/quote]
But this is not how it should look but it works
Posts: 586
Threads: 1
Joined: Aug 2019
Apr-04-2021, 10:37 AM
(This post was last modified: Apr-04-2021, 10:37 AM by ibreeden.)
(Apr-04-2021, 10:02 AM)danlopek14q Wrote: def all_list(listas,column[]): This is not correct. If you want to give a default for a parameter you should write:
def all_list(listas,column = []): If a column list is given, you can iterate over that list:
for i in column:
print(listas[i]) That will do the trick.
the_list = ["a", "b", "c"]
all_list(the_list)
all_list(the_list, [0, 2]) Output: ['a', 'b', 'c']
a
c
danlopek14q likes this post
Posts: 15
Threads: 3
Joined: Mar 2021
Perfect that will work. But how to have the effect like this ['Mars','Hot'] not
Mars
Hot
Posts: 15
Threads: 3
Joined: Mar 2021
(Apr-04-2021, 10:23 AM)danlopek14q Wrote: def all_list(listas,column[]):
#for no columns value
if column == []:
print(listas)
elif column == [0, 1]:
print([listas[0], listas[1]])
elif column == [0, 2]:
print([listas[0], listas[2]])
elif column == [0, 3]:
print([listas[0], listas[3]])
elif column == [1, 0]:
print([listas[1], listas[0]])
But this is not how it should look but it works
[/quote]
Yes the column will be given by the user but instead of printing all list like ['Mars',99] is print one under another
Posts: 586
Threads: 1
Joined: Aug 2019
(Apr-04-2021, 10:40 AM)danlopek14q Wrote: Perfect that will work. But how to have the effect like this ['Mars','Hot'] not
Mars
Hot
If you want to have the result printed as a list, you should make a result list and print the result at the end.
returnlist = []
for i in column:
returnlist.append(listas[i])
print(returnlist) Test:
the_list = ["a", "b", "c"]
all_list(the_list)
all_list(the_list, [2, 0, 1, 1, 1]) Output: ['a', 'b', 'c']
['c', 'a', 'b', 'b', 'b']
As an alternative you could instead use the "end" parameter of the print function. Like:
print(listas[i], end=", " This would cause the next print not to start on a new line.
danlopek14q likes this post
Posts: 15
Threads: 3
Joined: Mar 2021
Apr-04-2021, 11:08 AM
(This post was last modified: Apr-04-2021, 11:08 AM by danlopek14q.)
I tried that but then are two lists ['Hot']['Mars'] how can I achieve the effect like ['Hot','Mars'] ?
Posts: 15
Threads: 3
Joined: Mar 2021
Everything works, Thank You so much for your time and help. I may have more questions in the future, but it definitely saved my day. Happy Easter :)
Posts: 45
Threads: 0
Joined: Aug 2021
(Apr-04-2021, 10:37 AM)ibreeden Wrote: (Apr-04-2021, 10:02 AM)danlopek14q Wrote: this is not correct
def all_list(listas,column = []): # this is wrong for example:
def func(arr = []):
print(arr)
arr.append(2)
func() # each time this function is called, the default arg will change
func()
func()
func() Output: []
[2]
[2, 2]
[2, 2, 2]
The correct code with default list should be:
Choice 1:
def func(arr = None):
if arr == None:
arr = []
return arr
print(func([1, 2, 3, 4])) Choice 2:
def func(*args):
arr = list(args)
return arr
print(func(1, 2, 3, 4))
Posts: 45
Threads: 0
Joined: Aug 2021
(Apr-04-2021, 10:02 AM)danlopek14q Wrote: def func(arr, col = None):
if col == None: return ''
return [arr[i] for i in col]
arr = ['Mars', 'Hot', 22, True]
column = [0, 1]
print(func(arr, column)) Output: ['Mars', 'Hot']
|