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.
Related
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.
I have some scanned images which vary in brightness. I desire to equalize the brightness levels to make the images (as a group) more consistent. I borrowed some code from SO (with minor changes) that was described as doing what I needed. But the code seems to have no effect at all. Obviously I’m doing something wrong but I can’t seem to figure it out. What I am trying to do is read the perceived brightness level of the original images and from that calculate a target brightness level and pass that value to a function that will (theoretically) alter the image brightness to the perceived brightness level and create a new image file.
from os import listdir
import numpy as np
import cv2
from PIL import Image, ImageStat
import operator
import math
def brightness(fname):
with Image.open(fname) as img:
stat = ImageStat.Stat(img)
r,g,b = stat.mean
return int(math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2)))
def change_brightness(fname, value):
if value == 0: return
img = cv2.imread(SourceDir+fname,)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.add(hsv[:,:,2], value, hsv[:,:,2])
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(DestDir+fname, img)
SourceDir = 'I:\\'
DestDir = 'G:\\Documents\\'
InputFiles = []
BrightnessLevels = {}
TargetBrightness = 130
InputFiles = listdir(SourceDir)
try:
InputFiles.remove('System Volume Information')
except:
pass
InputFiles.sort()
for InputFile in InputFiles:
old_b = brightness(SourceDir+InputFile)
change = TargetBrightness-old_b
change_brightness(InputFile, change)
new_b = brightness(DestDir+InputFile)
BrightnessLevels[InputFile] = (old_b, change, new_b)
sorted_b = sorted(BrightnessLevels.items(), key=operator.itemgetter(1))
for f,b in sorted_b:
print(f,b)
The results I get is as follows:
Page-003.png (89, 41, 89)
Page-082.png (89, 41, 89)
Page-047.png (90, 40, 89)
Page-075.png (92, 38, 91)
Page-111.png (92, 38, 91)
Page-071.png (92, 38, 92)
Page-074.png (92, 38, 92)
Page-077.png (92, 38, 92)
Page-112.png (93, 37, 93)
Page-079.png (94, 36, 93)
Page-078.png (94, 36, 94)
Page-031.png (95, 35, 95)
Page-083.png (95, 35, 95)
Page-023.png (96, 34, 96)
Page-050.png (96, 34, 96)
Page-081.png (96, 34, 96)
Page-098.png (96, 34, 96)
Page-099.png (96, 34, 96)
Page-033.png (97, 33, 96)
Page-094.png (97, 33, 97)
Page-107.png (97, 33, 97)
Page-038.png (98, 32, 98)
Page-039.png (98, 32, 98)
Page-095.png (98, 32, 98)
Page-096.png (98, 32, 98)
Page-106.png (98, 32, 98)
Page-108.png (98, 32, 98)
Page-018.png (99, 31, 99)
Page-046.png (99, 31, 99)
Page-072.png (99, 31, 99)
Page-097.png (99, 31, 99)
Page-066.png (100, 30, 99)
Page-080.png (100, 30, 99)
Page-110.png (100, 30, 99)
Page-011.png (100, 30, 100)
Page-121.png (100, 30, 100)
Page-109.png (101, 29, 100)
Page-006.png (101, 29, 101)
Page-051.png (101, 29, 101)
Page-007.png (102, 28, 102)
Page-010.png (102, 28, 102)
Page-015.png (102, 28, 102)
Page-025.png (102, 28, 102)
Page-049.png (102, 28, 102)
Page-052.png (102, 28, 102)
Page-056.png (102, 28, 102)
Page-065.png (102, 28, 102)
Page-076.png (102, 28, 102)
Page-086.png (102, 28, 102)
Page-102.png (102, 28, 102)
Page-122.png (102, 28, 102)
Page-067.png (103, 27, 102)
Page-092.png (103, 27, 102)
Page-116.png (103, 27, 102)
Page-014.png (103, 27, 103)
Page-084.png (103, 27, 103)
Page-103.png (103, 27, 103)
Page-115.png (103, 27, 103)
Page-026.png (104, 26, 103)
Page-059.png (104, 26, 103)
Page-012.png (104, 26, 104)
Page-024.png (104, 26, 104)
Page-048.png (104, 26, 104)
Page-123.png (104, 26, 104)
Page-009.png (105, 25, 105)
Page-034.png (105, 25, 105)
Page-087.png (105, 25, 105)
Page-114.png (105, 25, 105)
Page-118.png (105, 25, 105)
Page-119.png (105, 25, 105)
Page-030.png (106, 24, 105)
Page-043.png (106, 24, 105)
Page-062.png (106, 24, 105)
Page-063.png (106, 24, 105)
Page-035.png (106, 24, 106)
Page-126.png (106, 24, 106)
Page-113.png (107, 23, 106)
Page-044.png (107, 23, 107)
Page-068.png (107, 23, 107)
Page-019.png (108, 22, 107)
Page-070.png (108, 22, 107)
Page-054.png (108, 22, 108)
Page-064.png (108, 22, 108)
Page-090.png (108, 22, 108)
Page-002.png (109, 21, 108)
Page-058.png (109, 21, 108)
Page-091.png (109, 21, 108)
Page-020.png (109, 21, 109)
Page-101.png (109, 21, 109)
Page-104.png (110, 20, 109)
Page-032.png (110, 20, 110)
Page-004.png (111, 19, 111)
Page-040.png (112, 18, 111)
Page-021.png (112, 18, 112)
Page-041.png (112, 18, 112)
Page-061.png (113, 17, 113)
Page-016.png (114, 16, 114)
Page-055.png (114, 16, 114)
Page-008.png (115, 15, 114)
Page-117.png (115, 15, 115)
Page-042.png (116, 14, 116)
Page-036.png (118, 12, 117)
Page-085.png (130, 0, 129)
Page-022.png (131, -1, 131)
Page-073.png (131, -1, 131)
Page-027.png (133, -3, 132)
Page-124.png (133, -3, 132)
Page-005.png (133, -3, 133)
Page-057.png (134, -4, 133)
Page-013.png (135, -5, 135)
Page-037.png (135, -5, 135)
Page-105.png (137, -7, 137)
Page-120.png (137, -7, 137)
Page-045.png (138, -8, 138)
Page-060.png (139, -9, 139)
Page-093.png (139, -9, 139)
Page-017.png (140, -10, 140)
Page-125.png (141, -11, 140)
Page-089.png (142, -12, 141)
Page-029.png (142, -12, 142)
Page-028.png (143, -13, 142)
Page-069.png (145, -15, 145)
Page-053.png (147, -17, 146)
Page-100.png (149, -19, 149)
Page-088.png (150, -20, 149)
The first value of the tuple is the initial perceived brightness of the image. The second value is the calculated adjustment to the brightness level. The third value is the perceived brightness level of the new image file. No matter what adjustment values I try, they don't seem to have any effect. And by visually looking at the files seem to agree that they look the same as the originals.
Change the line:
cv2.add(hsv[:,:,2], value, hsv[:,:,2])
so it looks like this:
hsv[:,:,2] = cv2.add(hsv[:,:,2], value)
It doesn't work how the documentation implies - I don't know why.
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]
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}
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.