﻿

Help

Nov 24th, 2020 (edited)
682
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. ########################################################
2.
3. simple = [5 for i in range(10)]
4. """
5. simple = []
6. for i in range(10):
7.    simple.append(5)
8. """
9. print("simple: ", simple)
10. #****************************************************************
11.
12. same = [i for i in range(10)]
13. """
14. same = []
15. for i in range(10):
16.    same.append(i)
17. """
18. print("same: ", same)
19. #****************************************************************
20.
21. expression = [i*2 for i in range(10)]
22. """
23. expression = []
24. for i in range(10):
25.    expression.append(i*2)
26. """
27. print("expr:   ", expression)
28. #-------------------------------------------------------
29.
30. expression_2 = [i*2 for i in same]
31. """
32. expression_2 = []
33. for i in same:
34.    expression_2.append(i*2)
35. """
36. print("expr_2: ", expression_2, "ugyanaz, mert a same-ben is számok vannak 0-9ig, \
37. ahogy a range(10)-ben is (az is egy iterable lényegében)")
38. #****************************************************************
39. #****************************************************************
40.
41. filtered = [x for x in same if x<5]
42. """
43. filtered = []
44. for x in same:
45.    if x<5:
46.        filtered.append(x)
47. """
48. print("filter (<5): ", filtered)
49. #----------------------------------------------------
50.
51. filtered_2 = [x for x in same if x<5 or x>6]
52. """
53. filtered = []
54. for x in same:
55.    if x<5 or x>6:
56.        filtered.append(x)
57. """
58. print("filter_2 (<5 or >6): ", filtered_2)
59. #----------------------------------------------------
60.
61. ifelse = [x if x<5 else 5 for x in same]
62. """
63. filtered = []
64. for x in same:
65.    if x<5:
66.        filtered.append(x)
67.    else:
68.        filtered.append(5)
69. """
70. print("ifelse: ", ifelse)
71. #--------------------------------------------------------
72.
73. multiple_if = [x if x<5 else 5 if x<7 else 7 for x in same]
74. """
75. filtered = []
76. for x in same:
77.    if x<5:
78.        filtered.append(x)
79.    elif x<7:
80.        filtered.append(5)
81.    else:
82.        filtered.append(7)
83. """
84. print("multiple_if: ", multiple_if)
85. # ...és ezt bármennyiszer megismételheted
86.
87. #****************************************************************
88. #****************************************************************
89.
90. nested = [x*y for x in simple for y in same]
91. """
92. nested=[]
93. for x in simple:
94.    for y in same:
95.     nested.append(x*y)
96. """
97. print('\nNested:', nested)
98. #----------------------------------------------------------------
99.
100. nested_2 =[x*y for y in same for x in simple]
101. """
102. nested=[]
103. for y in same:
104.    for x in simple:
105.     nested.append(x*y)
106. """
107. print('\nNested fordítva:', nested_2, "\n nem ugyanaz!!!!")
108. #a magyarázat jobban láttatja hogy miért nem ugyanaz a kettő
109. #-----------------------------------------------------------------
110.
111. ifelse_nested =[x+y if x+y<10 else 0 for x in simple for y in same]
112. """
113. ifelse_nested = []
114. for x in simple:
115.    for y in same:
116.        if x+y<10:
117.            ifelse_nested.append(x+y)
118.        else:
119.            ifelse_nested.append(0)
120. """
121. print("\nIfelse és nested:", ifelse_nested)
122. #****************************************************************
123.
124. xdim = 5
125. ydim = 10
126. multidim = [[x*ydim + y for y in range(ydim)] for x in range(xdim)]
127. """
128. multidim = []
129. for x in range(xdim):
130.    multidim.append([])
131.    for y in range(ydim):
132.        multidim[x].append(x*ydim + y)
133. """
134. print("\nmultidimensional:\n", multidim, '\nLast element: ', multidim[xdim-1][ydim-1], sep='')
135. #Itt az indexelés is be van mutatva - kiíratjuk a kétdimenziós tömb utsó elemét
136.
137.
138.
139.
140.
141.
142. #.
143.
RAW Paste Data