Changing an attribute in the init method in python - python

So in my snake class, I have initialized an attribute as self.x_cor = 0
Now, I want to modify or make use of this attribute in a different method (create_segment). It only works when I repeat the line of code (self.x_cor = 0 ) in the (create_segment) again.
If I don't repeat it, I get feedback that the snake object has no attribute as x_cor meanwhile I have initialized it in the init method
How do I use a different method to modify an attribute initialized in the init method?
import turtle
from turtle import Turtle
import random
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.speed = 9
self.segments = []
self.create_snake()
self.x_cor = 0
self.y_cor = 0
# self.add_segment()
self.head = self.segments[0] # the first segment is designated as the head of the snake
self.color_list = [(202, 164, 110), (240, 245, 241), (236, 239, 243), (149, 75, 50), (222, 201, 136),
(170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35),
(145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77),
(183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102),
(12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102), (53, 93, 123)]
self.colors = ["red", "orange", "yellow", "blue", "green", "purple"]
def create_snake(self):
""" Creates the snake """
for n in range(3): # Creates 3 segments for now
self.create_segment(n)
def create_segment(self, n):
""" Creates a new segment to be added to the snake """
new_segment = Turtle("square")
turtle.colormode(255)
self.color_list = [(202, 164, 110), (240, 245, 241), (236, 239, 243), (149, 75, 50), (222, 201, 136),
(170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35),
(145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77),
(183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102),
(12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102), (53, 93, 123)]
# new_segment.color(random.choice(self.color_list))
self.colors = ["red", "orange", "yellow", "blue", "green", "purple"]
new_segment.color(random.choice(self.colors))
new_segment.penup()
# new_segment.speed(9)
self.x_cor = 0
self.y_cor = 0
new_segment.goto(self.x_cor, self.y_cor)
self.x_cor -= 20 # Reduce the x_cor but maintain the y so all the segments will be on the same horizontal
# but different vertical axis
self.segments.append(new_segment)
if I don't repeat
self.x_cor = 0
self.y_cor = 0
in the 2nd method, I get feedback as "line 49, in create_segment
new_segment.goto(self.x_cor, self.y_cor)
AttributeError: 'Snake' object has no attribute 'x_cor'. Did you mean: 'y_cor'?
I really think repetition is not necessary but how do I avoid that?
Thank you very much

In the create_snake in your constructor you are basically trying to access self.x_cor before it is created, because create_snake calls create_segment that refers to self.x_cor before it is initialized.

Related

Find distance for every edge and keep separate routes

[[0, 100, 7, 27, 34, 40, 41, 48, 58, 65, 75, 78, 79, 96, 126, 127, 0],
[0, 2, 45, 54, 56, 57, 59, 66, 67, 82, 86, 102, 124, 133, 0],
[0, 35, 39, 52, 53, 60, 61, 80, 81, 83, 87, 97, 98, 101, 109, 0],
[0, 15, 28, 29, 30, 31, 32, 33, 37, 38, 49, 50, 51, 71, 95, 0],
[0, 3, 16, 22, 23, 44, 72, 73, 74, 90, 110, 131, 0],
[0, 10, 11, 18, 19, 36, 55, 89, 93, 94, 108, 113, 114, 0],
[0, 1, 5, 6, 9, 12, 17, 24, 43, 64, 77, 85, 88, 91, 92, 111, 112, 130, 0],
[0, 13, 20, 42, 62, 68, 84, 99, 104, 116, 119, 125, 128, 129, 132, 0],
[0, 8, 14, 26, 63, 69, 70, 103, 105, 123, 0],
[0, 4, 21, 25, 46, 47, 106, 107, 115, 117, 118, 120, 121, 122, 0],
[0, 76, 0]]
I have the different routes listed above. I need to calculate the distance of every route (11 routes in total)
After this, I have created all edges within a single route.
[[(0, 100),
(100, 7),
(7, 27),
(27, 34),
(34, 40),
(40, 41),
(41, 48),
(48, 58),
(58, 65),
(65, 75),
(75, 78),
(78, 79),
(79, 96),
(96, 126),
(126, 127),
(127, 0)],
[(0, 2),
(2, 45),
(45, 54),
(54, 56),
(56, 57),
(57, 59),
(59, 66),
(66, 67),
(67, 82),
(82, 86),
(86, 102),
(102, 124),
(124, 133),
(133, 0)],
[(0, 35),
(35, 39),
(39, 52),
(52, 53),
(53, 60),
(60, 61),
(61, 80),
(80, 81),
(81, 83),
(83, 87),
(87, 97),
(97, 98),
(98, 101),
(101, 109),
(109, 0)],
[(0, 15),
(15, 28),
(28, 29),
(29, 30),
(30, 31),
(31, 32),
(32, 33),
(33, 37),
(37, 38),
(38, 49),
(49, 50),
(50, 51),
(51, 71),
(71, 95),
(95, 0)],
[(0, 3),
(3, 16),
(16, 22),
(22, 23),
(23, 44),
(44, 72),
(72, 73),
(73, 74),
(74, 90),
(90, 110),
(110, 131),
(131, 0)],
[(0, 10),
(10, 11),
(11, 18),
(18, 19),
(19, 36),
(36, 55),
(55, 89),
(89, 93),
(93, 94),
(94, 108),
(108, 113),
(113, 114),
(114, 0)],
[(0, 1),
(1, 5),
(5, 6),
(6, 9),
(9, 12),
(12, 17),
(17, 24),
(24, 43),
(43, 64),
(64, 77),
(77, 85),
(85, 88),
(88, 91),
(91, 92),
(92, 111),
(111, 112),
(112, 130),
(130, 0)],
[(0, 13),
(13, 20),
(20, 42),
(42, 62),
(62, 68),
(68, 84),
(84, 99),
(99, 104),
(104, 116),
(116, 119),
(119, 125),
(125, 128),
(128, 129),
(129, 132),
(132, 0)],
[(0, 8),
(8, 14),
(14, 26),
(26, 63),
(63, 69),
(69, 70),
(70, 103),
(103, 105),
(105, 123),
(123, 0)],
[(0, 4),
(4, 21),
(21, 25),
(25, 46),
(46, 47),
(47, 106),
(106, 107),
(107, 115),
(115, 117),
(117, 118),
(118, 120),
(120, 121),
(121, 122),
(122, 0)],
[(0, 76), (76, 0)]]
However, I need to calculate the distance between the edges. Every edge consists of 2 numbers which are city numbers in a distance matrix (so 0,100 is the distance from city 0 to city 100). I tried to calculate the distances but cannot keep separate routes.
I already tried this:
a_list=[]
visiting_time={}
for k in range(len(result)):
for (i,j) in visits[k]:
visiting_time[(i,j)]= distance_matrix_new_time[i][j]
f=list(visiting_time.values())
a_list.append(f)
In my code Result is the list with different routes (first list), and visits is the list with all edges (second list)
the output should be like this
[2,3,5,6,3,2,5,8,3,5,2,4,6],[2,6,3,1,9,....],[....] etc.
Can someone help me out?
you could use a list comprehension:
a_list = [[distance_matrix_new_time[i][j] for i, j in l] for l in visits]

How to create an array from a given array of ranges

I have this code:
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
Those are given ranges, for example, pg[0] should be 10, pg[1] be 11, pg[2] be 12. and so on for the rest of the ranges. So I want the final array to be like this:
pg=[10, 11, 12, 13 ....19, 30, 31,....,32,37, 38,50,51,....,59,63.. and so on]
How can I do this in python? Is it possible to do it without hard coding every range of elements in a new array?
I guess the following might work
pg = [(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
arr = []
for val in pg:
arr += list(range(val[0], val[1] + 1))
print(arr)
This is one approach using a list comprehension and itertools.chain(to flatten the list)
Ex:
from itertools import chain
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
result = list(chain.from_iterable([range(*i) for i in pg]))
print(result)
A one-linear
sum([list(range(x1, x2+1)) for x1, x2 in pg], [])
Try this
l = []
for r in pg:
l.extend(range(r[0], r[1]+1))
One more example using list comprehension
a = [(10, 19), (30, 35)]
b = [j for i in a for j in range(i[0], i[1]+1)]
print(b)
#output
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 35]
with help of sum function also we can achieve this.
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
print (list(sum(pg,())))
#output:[10, 19, 30, 32, 37, 38, 50, 59, 63, 64, 69, 69, 120, 121, 124, 129, 130, 139,
#160, 161, 164, 169, 180, 182, 185, 185, 189, 189, 190, 192, 194, 194, 196, 199, 260,
#269, 270, 279, 300, 309, 330, 339, 358, 359, 360, 369]

How to get largest (unconnected) network / cluster using networkx in Python?

I have the following example data:
my_network_data = [(39, 118), (179, 14), (35, 118), (225, 14), (64, 118), (6, 14), (187, 14), (161, 14), (42, 14), (53, 14), (47, 1), (127, 14), (14, 118), (3, 1), (175, 14), (21, 118), (5, 14), (18, 14), (122, 1), (137, 14), (157, 14), (19, 14), (19, 118), (118, 14), (30, 118), (159, 14), (124, 118), (56, 14), (161, 118), (100, 14), (53, 118), (136, 118), (41, 14), (4, 14), (217, 14), (32, 14), (175, 118), (104, 14), (82, 118), (4, 118), (222, 14), (201, 118), (136, 14), (86, 1), (153, 14), (195, 14)]
If I plot it using networkx, I do the following:
import networkx as nx
g = nx.Graph()
g.add_edges_from(my_network_data)
print(nx.info(g))
Output:
Name:
Type: Graph
Number of nodes: 41
Number of edges: 45
Average degree: 2.1951
The graph looks like this:
nx.draw(g, with_labels=True)
How do I get the information using networkx that there are 2 distinctive clusters and which items are in those clusters?
Suggested output :
[[1, 86, 47, 3, 122], [14, 118, 136, 53, 179, 30, 100, 41, 35, 4, 19, 82, 104, 159, 64, 32, 124, 14, 39, 4, 137, 136, 187, 217, 153, 5, 53, 19, 42, 175, 18, 21, 222, 175, 6, 195, 56, 157, 201, 161, 161, 127, 225]]
I'm not sure if networkx is the best possible library for this task. If you have a better suggestion (using Python), I'm open to using it.
Looks like nx.connected_components() is what you need:
for c in nx.connected_components(g):
print(c)
{4, 5, 6, 136, 137, 14, 18, 19, 21, 153, 157, 30, 159, 32, 161, 35, 39, 41, 42, 175, 179, 53, 56, 187, 64, 195, 201, 82, 217, 222, 225, 100, 104, 118, 124, 127}
{1, 3, 47, 86, 122}

Call from class in module without specifying its name

Can I import a class from a module, the same way as I can import the module like:
from module_name import *
so that I don't have to specify the "module.class" names every time I want a variable in it?
Example:
in module:
class SomeClass:
var1 = "Hello"
var2 = "Bye"
in main script:
def print_something(text):
print(text)
if __name__ == "__main__":
print_something(var1) # "module_name.SomeClass.var1" should not be necessary, only "var1"
Edit:
What it does look like in the actual class, in the actual module:
class RGBcolors:
# black-white-color
bwc0 = (0, 0, 0)
bwc1 = (1, 1, 1)
bwc2 = (2, 2, 2)
bwc3 = (3, 3, 3)
bwc4 = (4, 4, 4)
bwc5 = (5, 5, 5)
bwc6 = (6, 6, 6)
bwc7 = (7, 7, 7)
bwc8 = (8, 8, 8)
bwc9 = (9, 9, 9)
bwc10 = (10, 10, 10)
bwc11 = (11, 11, 11)
bwc12 = (12, 12, 12)
bwc13 = (13, 13, 13)
bwc14 = (14, 14, 14)
bwc15 = (15, 15, 15)
bwc16 = (16, 16, 16)
bwc17 = (17, 17, 17)
bwc18 = (18, 18, 18)
bwc19 = (19, 19, 19)
bwc20 = (20, 20, 20)
bwc21 = (21, 21, 21)
bwc22 = (22, 22, 22)
bwc23 = (23, 23, 23)
bwc24 = (24, 24, 24)
bwc25 = (25, 25, 25)
bwc26 = (26, 26, 26)
bwc27 = (27, 27, 27)
bwc28 = (28, 28, 28)
bwc29 = (29, 29, 29)
bwc30 = (30, 30, 30)
bwc31 = (31, 31, 31)
bwc32 = (32, 32, 32)
bwc33 = (33, 33, 33)
bwc34 = (34, 34, 34)
bwc35 = (35, 35, 35)
bwc36 = (36, 36, 36)
bwc37 = (37, 37, 37)
bwc38 = (38, 38, 38)
bwc39 = (39, 39, 39)
bwc40 = (40, 40, 40)
bwc41 = (41, 41, 41)
bwc42 = (42, 42, 42)
bwc43 = (43, 43, 43)
bwc44 = (44, 44, 44)
bwc45 = (45, 45, 45)
bwc46 = (46, 46, 46)
bwc47 = (47, 47, 47)
bwc48 = (48, 48, 48)
bwc49 = (49, 49, 49)
bwc50 = (50, 50, 50)
bwc51 = (51, 51, 51)
bwc52 = (52, 52, 52)
bwc53 = (53, 53, 53)
bwc54 = (54, 54, 54)
bwc55 = (55, 55, 55)
bwc56 = (56, 56, 56)
bwc57 = (57, 57, 57)
bwc58 = (58, 58, 58)
bwc59 = (59, 59, 59)
bwc60 = (60, 60, 60)
bwc61 = (61, 61, 61)
bwc62 = (62, 62, 62)
bwc63 = (63, 63, 63)
bwc64 = (64, 64, 64)
bwc65 = (65, 65, 65)
bwc66 = (66, 66, 66)
bwc67 = (67, 67, 67)
bwc68 = (68, 68, 68)
bwc69 = (69, 69, 69)
bwc70 = (70, 70, 70)
bwc71 = (71, 71, 71)
bwc72 = (72, 72, 72)
bwc73 = (73, 73, 73)
bwc74 = (74, 74, 74)
bwc75 = (75, 75, 75)
bwc76 = (76, 76, 76)
bwc77 = (77, 77, 77)
bwc78 = (78, 78, 78)
bwc79 = (79, 79, 79)
bwc80 = (80, 80, 80)
bwc81 = (81, 81, 81)
bwc82 = (82, 82, 82)
bwc83 = (83, 83, 83)
bwc84 = (84, 84, 84)
bwc85 = (85, 85, 85)
bwc86 = (86, 86, 86)
bwc87 = (87, 87, 87)
bwc88 = (88, 88, 88)
bwc89 = (89, 89, 89)
bwc90 = (90, 90, 90)
bwc91 = (91, 91, 91)
bwc92 = (92, 92, 92)
bwc93 = (93, 93, 93)
bwc94 = (94, 94, 94)
bwc95 = (95, 95, 95)
bwc96 = (96, 96, 96)
bwc97 = (97, 97, 97)
bwc98 = (98, 98, 98)
bwc99 = (99, 99, 99)
bwc100 = (100, 100, 100)
bwc101 = (101, 101, 101)
bwc102 = (102, 102, 102)
bwc103 = (103, 103, 103)
bwc104 = (104, 104, 104)
bwc105 = (105, 105, 105)
bwc106 = (106, 106, 106)
bwc107 = (107, 107, 107)
bwc108 = (108, 108, 108)
bwc109 = (109, 109, 109)
bwc110 = (110, 110, 110)
bwc111 = (111, 111, 111)
bwc112 = (112, 112, 112)
bwc113 = (113, 113, 113)
bwc114 = (114, 114, 114)
bwc115 = (115, 115, 115)
bwc116 = (116, 116, 116)
bwc117 = (117, 117, 117)
bwc118 = (118, 118, 118)
bwc119 = (119, 119, 119)
bwc120 = (120, 120, 120)
bwc121 = (121, 121, 121)
bwc122 = (122, 122, 122)
bwc123 = (123, 123, 123)
bwc124 = (124, 124, 124)
bwc125 = (125, 125, 125)
bwc126 = (126, 126, 126)
bwc127 = (127, 127, 127)
bwc128 = (128, 128, 128)
bwc129 = (129, 129, 129)
bwc130 = (130, 130, 130)
bwc131 = (131, 131, 131)
bwc132 = (132, 132, 132)
bwc133 = (133, 133, 133)
bwc134 = (134, 134, 134)
bwc135 = (135, 135, 135)
bwc136 = (136, 136, 136)
bwc137 = (137, 137, 137)
bwc138 = (138, 138, 138)
bwc139 = (139, 139, 139)
bwc140 = (140, 140, 140)
bwc141 = (141, 141, 141)
bwc142 = (142, 142, 142)
bwc143 = (143, 143, 143)
bwc144 = (144, 144, 144)
bwc145 = (145, 145, 145)
bwc146 = (146, 146, 146)
bwc147 = (147, 147, 147)
bwc148 = (148, 148, 148)
bwc149 = (149, 149, 149)
bwc150 = (150, 150, 150)
bwc151 = (151, 151, 151)
bwc152 = (152, 152, 152)
bwc153 = (153, 153, 153)
bwc154 = (154, 154, 154)
bwc155 = (155, 155, 155)
bwc156 = (156, 156, 156)
bwc157 = (157, 157, 157)
bwc158 = (158, 158, 158)
bwc159 = (159, 159, 159)
bwc160 = (160, 160, 160)
bwc161 = (161, 161, 161)
bwc162 = (162, 162, 162)
bwc163 = (163, 163, 163)
bwc164 = (164, 164, 164)
bwc165 = (165, 165, 165)
bwc166 = (166, 166, 166)
bwc167 = (167, 167, 167)
bwc168 = (168, 168, 168)
bwc169 = (169, 169, 169)
bwc170 = (170, 170, 170)
bwc171 = (171, 171, 171)
bwc172 = (172, 172, 172)
bwc173 = (173, 173, 173)
bwc174 = (174, 174, 174)
bwc175 = (175, 175, 175)
bwc176 = (176, 176, 176)
bwc177 = (177, 177, 177)
bwc178 = (178, 178, 178)
bwc179 = (179, 179, 179)
bwc180 = (180, 180, 180)
bwc181 = (181, 181, 181)
bwc182 = (182, 182, 182)
bwc183 = (183, 183, 183)
bwc184 = (184, 184, 184)
bwc185 = (185, 185, 185)
bwc186 = (186, 186, 186)
bwc187 = (187, 187, 187)
bwc188 = (188, 188, 188)
bwc189 = (189, 189, 189)
bwc190 = (190, 190, 190)
bwc191 = (191, 191, 191)
bwc192 = (192, 192, 192)
bwc193 = (193, 193, 193)
bwc194 = (194, 194, 194)
bwc195 = (195, 195, 195)
bwc196 = (196, 196, 196)
bwc197 = (197, 197, 197)
bwc198 = (198, 198, 198)
bwc199 = (199, 199, 199)
bwc200 = (200, 200, 200)
bwc201 = (201, 201, 201)
bwc202 = (202, 202, 202)
bwc203 = (203, 203, 203)
bwc204 = (204, 204, 204)
bwc205 = (205, 205, 205)
bwc206 = (206, 206, 206)
bwc207 = (207, 207, 207)
bwc208 = (208, 208, 208)
bwc209 = (209, 209, 209)
bwc210 = (210, 210, 210)
bwc211 = (211, 211, 211)
bwc212 = (212, 212, 212)
bwc213 = (213, 213, 213)
bwc214 = (214, 214, 214)
bwc215 = (215, 215, 215)
bwc216 = (216, 216, 216)
bwc217 = (217, 217, 217)
bwc218 = (218, 218, 218)
bwc219 = (219, 219, 219)
bwc220 = (220, 220, 220)
bwc221 = (221, 221, 221)
bwc222 = (222, 222, 222)
bwc223 = (223, 223, 223)
bwc224 = (224, 224, 224)
bwc225 = (225, 225, 225)
bwc226 = (226, 226, 226)
bwc227 = (227, 227, 227)
bwc228 = (228, 228, 228)
bwc229 = (229, 229, 229)
bwc230 = (230, 230, 230)
bwc231 = (231, 231, 231)
bwc232 = (232, 232, 232)
bwc233 = (233, 233, 233)
bwc234 = (234, 234, 234)
bwc235 = (235, 235, 235)
bwc236 = (236, 236, 236)
bwc237 = (237, 237, 237)
bwc238 = (238, 238, 238)
bwc239 = (239, 239, 239)
bwc240 = (240, 240, 240)
bwc241 = (241, 241, 241)
bwc242 = (242, 242, 242)
bwc243 = (243, 243, 243)
bwc244 = (244, 244, 244)
bwc245 = (245, 245, 245)
bwc246 = (246, 246, 246)
bwc247 = (247, 247, 247)
bwc248 = (248, 248, 248)
bwc249 = (249, 249, 249)
bwc250 = (250, 250, 250)
bwc251 = (251, 251, 251)
bwc252 = (252, 252, 252)
bwc253 = (253, 253, 253)
bwc254 = (254, 254, 254)
bwc255 = (255, 255, 255)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
dark_red = (127, 0, 0)
yellow = (0, 255, 0)
blue = (0, 0, 255)
red_orange = (255, 127, 80)
green = (0, 255, 255)
purple = (255, 0, 255)
You can use metaprogramming for this:
class GlobalNamespace(type):
def __init__(cls, name, bases, dct):
type.__init__(cls, name, bases, dct)
globals().update({n: v for n, v in dct.items() if not n.startswith("__")})
class RGBcolors(metaclass=GlobalNamespace):
...
then, import module that contains RGBcolors
NOTE: please read the comments to your question and this answer, explaining why this is a bad idea in principle. Do not treat classes as modules. They are two different concepts.

Python, to pick specific elements from a list of tuples

Hello all…I have a list of tuples (number of colors in an image. For example, there are 6320 (255, 255, 255) colors there etc.)
What I want to have is get the number of certain colors, like “(159, 177, 205)” and “(223, 213, 196)”. 2 and 618 are their correspondences.
How can I operate the tuples so I can pick them out (2 and 618)?
Thanks.
[(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]
Use simple List comprehension, with a filtering condition and unpacking the tuple, like this
>>> items_to_pick = [2, 618]
>>> [color for idx, color in list_of_tuples if idx in items_to_pick]
[(159, 177, 205), (173, 194, 227), (223, 213, 196), (214, 227, 246)]
But, if the number of items to be filtered is very big, then you can convert this to a generator expression and get the elements only when you need, like this
>>> my_filter = (color for idx, color in list_of_tuples if idx in items_to_pick)
>>> next(my_filter)
(159, 177, 205)
>>> next(my_filter)
(173, 194, 227)
>>> next(my_filter)
(223, 213, 196)
>>> next(my_filter)
(214, 227, 246)
>>> next(my_filter)
Traceback (most recent call last):
File "<input>", line 1, in <module>
StopIteration
Or, if you are just going to iterate the filtered data, you can simply iterate over the generator, like this
>>> for item in (color for idx, color in list_of_tuples if idx in items_to_pick):
... print(item)
...
...
(159, 177, 205)
(173, 194, 227)
(223, 213, 196)
(214, 227, 246)
x=[(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]
print [k for k in x if k[0] in [2,618]]
This can be done using simple list comprehension.
Output:[(2, (159, 177, 205)), (2, (173, 194, 227)), (618, (223, 213, 196)), (2, (214, 227, 246))]
Seems like you want to get only the tuple element which was at the the index 1 only if the corresponding index 0 value is 2 or 618
>>> t = [(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]
>>> [k[1] for k in t if k[0] in [2,618]]
[(159, 177, 205), (173, 194, 227), (223, 213, 196), (214, 227, 246)]
Why not use a dictionary instead of a list?
Given your original list, let's call it tuples:
tuples = [(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]
you can succinctly convert it to a dictionary as follows:
d = {k: v for k, v in tuples}
Then you can access the tuples for 618, for example, as follows:
print(d[618])
(223, 213, 196)
Or you can use a dictionary:
>>> t = [(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248))]
>>> dic = {x[0]:x[1] for x in t}
>>> print(dic)
{6320: (255, 255, 255), 17: (249, 251, 253), 9: (244, 245, 248)}
>>> list = [x for x, y in dic.items() if y == (249,251,253) or y == (1,1,1)]
>>> list
[17]
EDIT: Well, I have updated to solve your problem with dictionaries.

Categories