- 1
Can someone help me with code understanding?
Hello, I need to understand one code not written by me. Can someone please help me with understanding some of the parts of that code?
18 Answers
+ 4
def gensachovnicu(n):
return [[policko(x, y, n) for y in range(n)] for x in range(n)]
# That's called "list comprehension", and it works as:
def gensachovnicu(n):
final_list = []
for x in range(n):
temp_list = []
for y in range(n):
temp_list.append(policko(x,y,n))
final_list.append(temp_list)
return final_list
+ 3
For list comprehension, you could refer to this link:
http://www.secnetix.de/olli/JUMP_LINK__&&__Python__&&__JUMP_LINK/list_comprehensions.hawk
... or google search more for "list comprehension" ;)
For second thing:
Yes that's a list... more exactly a two dimensional list, meaning, a list wich hold lists as its items:
list = [ 1, 2, 3, 4 ]
print(list[2]) # output 3 (indexes start at zero)
list = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
print(list[1]) # output [ 4, 5, 6 ]
print(list[1][0]) # output 4
+ 3
List use integer as indexes, but you access dictionaries in a same kind of way:
mydict = { "key": "value", 2: "another value", "3": 42 }
print(mydict["key"]) # output "value"
You can see that the main difference is that dict are kind of lists, but not ordered, and with indexes not limited to integer...
You should redo your Python course (or another one), if you still doesn't have understood this basic stuff ;P
+ 2
Sorry, but no: I don't want to share an email address and I don't use any kind of "social" network ^^
If you want to ask me questions, you have to do here, or if need of little more privacy, you can post a comment on one of my code... (if possible on a code mostly related to the question, so at least a python one ;))
+ 2
line and element are not very explicit ;)
I prefer saying that list[x][y] is the y-nth item of the x-nth list ^^
x is the index of the outer list
y is the index of the inner list
+ 2
Without the context, it's impossible to answer accuratly... but genericly, x and y are variables storing list indexes, so integer ^^
For example:
L = [
[ 1, 7, 3, 9 ],
[ 5, 6, 8, 1 ],
[ 2, 4, 0, 2 ]
]
var x = 1
var y = 2
print(L[x][y]) # output: 8
0
@Marek join us on discord. There are plenty to chat with about code.
https://www.sololearn.com/Discuss/689391/?ref=app
- 1
sure. we need code to see so you should have posted link or copy with credit to who you got it from. Just for future posts please read this.
https://www.sololearn.com/Discuss/333866/?ref=app
- 1
https://code.sololearn.com/cLk7qUQZw3xX/?ref=app
- 1
This is that code. I need help fr example with: def gensachovnicu. How else Can be that function writen?
- 1
I will try it tommorow, because I turned off my notebook, but thank you veru much.
- 1
second thing.. when I have this: sachovnica[x][y].. that is list right? and what is that x and y and in what data types are data saved?
- 1
wow man, you ste great! 😍.. and when I have x,y in parameters that means what? Because for example 1 is second parameter of list, but what is x?
and when I have sachovnica [x][y] - that is int. data type or string?
- 1
I will redo my course for sure, I just need to understand this code until wensday.. 😀 I will continue with "debugging" tommorow, when I will have another question I will come here.. for now, thank you very much! 😊
- 1
@visph Can you please give me any kind of contact? I would like to ask you some questions about this code..
- 1
ok I will ask here..
So, in this code, I have problem with understanding as you can see :D
sachovnica is list of list..
and this:
sachovnica[x][y] - is x-th line, y-th element?
- 1
And what is that x and y? I mean exactly in this code..
I know that for example: L = [5,6,8,1,2]
print(L[2]) will give me 8..
but what is exactly that x and y?
- 1
and how can I explain this?
hraci = [[pozicia_domceku(0)], [pozicia_domceku(1)]]