I have a list:
[(14, 2), (14, 2), (16, 2), (14, 2), (15, 2), (15, 2), (21, 2), (15, 2), (18, 2), (15, 2), (19, 2), (25, 2), (22, 2), (17, 2), (31, 2), (26, 2), (21, 2), (25, 2), (29, 2), (33, 2), (25, 2), (23, 2), (25, 2), (19, 2), (12, 2), (29, 2), (18, 2), (21, 2), (13, 2), (13, 2), (18, 2), (11, 2), (12, 2), (20, 2), (23, 2), (17, 2), (14, 2), (17, 2), (12, 2), (13, 2), (15, 2), (21, 2), (15, 2), (19, 2), (22, 2), (16, 2), (16, 2), (13, 2), (17, 2), (18, 2), (20, 2), (18, 2), (13, 2), (13, 2), (18, 2), (14, 2), (13, 2), (22, 2), (14, 2), (25, 2), (22, 2), (9, 2), (18, 2), (22, 2), (19, 2), (13, 2), (14, 2), (15, 2), (13, 2), (17, 2), (21, 2), (18, 2), (21, 2), (18, 2), (15, 2), (16, 2), (13, 2), (16, 2), (16, 2), (15, 2), (11, 2), (24, 2), (15, 2), (12, 2), (20, 2), (21, 2), (21, 2), (14, 2), (11, 2), (26, 2), (17, 2), (21, 2), (16, 2), (13, 2), (15, 2), (13, 2), (12, 2), (22, 2), (16, 2), (13, 2), (13, 2), (22, 2), (12, 2), (16, 2), (16, 2), (21, 2), (19, 2), (15, 2), (16, 2), (16, 2), (13, 2), (14, 2), (14, 2), (20, 2), (14, 2), (20, 2), (13, 2), (19, 2), (20, 2), (17, 2), (17, 2), (25, 2), (22, 2), (22, 2), (22, 2), (14, 2), (19, 2), (20, 2), (16, 2), (13, 2), (19, 2), (16, 2), (12, 2), (18, 2), (20, 2), (19, 2), (18, 2), (15, 2), (22, 2), (18, 2), (20, 2), (14, 2), (19, 2), (16, 2), (18, 2), (28, 2), (14, 2), (17, 2), (17, 2), (23, 2), (18, 2), (24, 2), (17, 2), (18, 2), (18, 2), (22, 2)]
And I want my output to be a list of lists where every sub-list is the number of elements that can be added together without being over a certain threshold and only stored as the sum (of each element):
Example, if the threshold is 50 (inclusive):
[[16, 16, 18], [16, 17, 17], [23, 17], [20, 17], [21, 27], [24, 19], [33], [28], [23, 27], [31], [35], [27], [25], [27, 21] ...]
The second value of the tuple may vary. Preferred as a list comprehension.
EDIT:
As requested, my original code which I want to cleanup/optimize:
padding = len("Packages () ") + math.floor(math.log10(len(apps))+1)
line_length = columns - (padding * 2) - 2
spacings = 2
element_length = [item for sublist in [list(a) for a in zip([len(i) for i in apps],[i for i in itertools.repeat(spacings, len([len(i) for i in apps]))])] for item in sublist]
limits = []
outer_limit = 0
while line_length <= sum(element_length):
while line_length >= sum(element_length[0:outer_limit]):
outer_limit += 1
limits.append(outer_limit - 1)
element_length = element_length[outer_limit - 1:]
outer_limit = 0
message = ""
a = 0
b = 0
for amount in limits:
b += math.ceil(amount / 2)
message += (" " * spacings).join(apps[a:b]) + ("\n" + " " * padding)
a = b
print("Packages ({}) {}".format(len(apps), message))
Here is a pretty simple way of doing that with just a single for-loop:
tups = [(14, 2), (14, 2), (16, 2), (14, 2)]
threshold = 50
result = [[]]
for tup in tups:
tupSum = sum(tup)
# Start a new sublist if adding this tuple's sum would exceed the threshold
if tupSum + sum(result[-1]) > threshold:
result.append([])
result[-1].append(tupSum)
This could be done with the reduce function from functools:
a = [(14, 2), (14, 2), (16, 2), (14, 2), (15, 2), (15, 2), (21, 2), (15, 2), (18, 2), (15, 2), (19, 2), (25, 2), (22, 2), (17, 2), (31, 2), (26, 2), (21, 2), (25, 2), (29, 2), (33, 2), (25, 2), (23, 2), (25, 2), (19, 2), (12, 2), (29, 2), (18, 2), (21, 2), (13, 2), (13, 2), (18, 2), (11, 2), (12, 2), (20, 2), (23, 2), (17, 2), (14, 2), (17, 2), (12, 2), (13, 2), (15, 2), (21, 2), (15, 2), (19, 2), (22, 2), (16, 2), (16, 2), (13, 2), (17, 2), (18, 2), (20, 2), (18, 2), (13, 2), (13, 2), (18, 2), (14, 2), (13, 2), (22, 2), (14, 2), (25, 2), (22, 2), (9, 2), (18, 2), (22, 2), (19, 2), (13, 2), (14, 2), (15, 2), (13, 2), (17, 2), (21, 2), (18, 2), (21, 2), (18, 2), (15, 2), (16, 2), (13, 2), (16, 2), (16, 2), (15, 2), (11, 2), (24, 2), (15, 2), (12, 2), (20, 2), (21, 2), (21, 2), (14, 2), (11, 2), (26, 2), (17, 2), (21, 2), (16, 2), (13, 2), (15, 2), (13, 2), (12, 2), (22, 2), (16, 2), (13, 2), (13, 2), (22, 2), (12, 2), (16, 2), (16, 2), (21, 2), (19, 2), (15, 2), (16, 2), (16, 2), (13, 2), (14, 2), (14, 2), (20, 2), (14, 2), (20, 2), (13, 2), (19, 2), (20, 2), (17, 2), (17, 2), (25, 2), (22, 2), (22, 2), (22, 2), (14, 2), (19, 2), (20, 2), (16, 2), (13, 2), (19, 2), (16, 2), (12, 2), (18, 2), (20, 2), (19, 2), (18, 2), (15, 2), (22, 2), (18, 2), (20, 2), (14, 2), (19, 2), (16, 2), (18, 2), (28, 2), (14, 2), (17, 2), (17, 2), (23, 2), (18, 2), (24, 2), (17, 2), (18, 2), (18, 2), (22, 2)]
from functools import reduce
b = list(reduce(lambda a,b:a+[[b]] if sum(a[-1])+b>50 else a[:-1]+[a[-1]+[b]],map(sum,a),[[]]))
print(b)
# [[16, 16, 18], [16, 17, 17], [23, 17], [20, 17], [21, 27], [24, 19], [33], [28], [23, 27], [31], [35], [27], [25], [27, 21], [14, 31], [20, 23], [15, 15, 20], [13, 14, 22], [25, 19], [16, 19, 14], [15, 17], [23, 17], [21, 24], [18, 18], [15, 19], [20, 22], [20, 15, 15], [20, 16], [15, 24], [16, 27], [24, 11], [20, 24], [21, 15], [16, 17, 15], [19, 23], [20, 23], [20, 17], [18, 15], [18, 18], [17, 13], [26, 17], [14, 22], [23, 23], [16, 13], [28, 19], [23, 18], [15, 17, 15], [14, 24], [18, 15, 15], [24, 14], [18, 18], [23, 21], [17, 18], [18, 15, 16], [16, 22], [16, 22], [15, 21], [22, 19], [19, 27], [24, 24], [24, 16], [21, 22], [18, 15], [21, 18], [14, 20], [22, 21], [20, 17], [24, 20], [22, 16], [21, 18], [20, 30], [16, 19], [19, 25], [20, 26], [19, 20], [20, 24]]
Related
Given a list and a number, create a new list that contains each number of list at most N times, without reordering.
For example if the input number is 2, and the input list is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].
With list [20,37,20,21] and number 1, the result would be [20,37,21]
For example i had this like my attempt
from collections import Counter
def delete_nth(order, max_e):
c = Counter(order)
result = []
for key, value in c.items():
if value > max_e:
value = value - 1
return [x for x in c.items()]
and output is
dict_items([(20, 2), (37, 1), (21, 1)])
dict_items([(20, 2), (37, 1), (21, 1)])
From list [20, 37, 20, 21], 1 you get: [(20, 2), (37, 1), (21, 1)] should equal [20, 37, 21]
Log
dict_items([(1, 2), (3, 2), (7, 1), (2, 4)])
dict_items([(1, 2), (3, 2), (7, 1), (2, 4)])
From list [1, 1, 3, 3, 7, 2, 2, 2, 2], 3 you get: [(1, 2), (3, 2), (7, 1), (2, 4)] should equal [1, 1, 3, 3, 7, 2, 2, 2]
Log
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
From list [1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], 3 you get: [(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)] should equal [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5]
From list [1, 1, 1, 1, 1], 5 you get: [(1, 5)] should equal [1, 1, 1, 1, 1]
Test Passed
Random tests
Testing for delete_nth([21, 45, 29, 29, 29, 6, 24, 45, 45, 45, 29, 21, 29, 24, 6, 45, 21, 38, 45, 21, 24, 45, 24, 6, 6, 29, 45, 29, 6, 45, 21, 29], 4)
Log
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
It should work on random inputs too!: [(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)] should equal [21, 45, 29, 29, 29, 6, 24, 45, 45, 45, 29, 21, 24, 6, 21, 38, 21, 24, 24, 6, 6]
Testing for delete_nth([18, 18, 18, 18, 6, 23, 18, 46, 18, 31, 18, 31, 23, 18, 18, 31], 7)
Log
dict_items([(18, 9), (6, 1), (23, 2), (46, 1), (31, 3)])
dict_items([(18, 9), (6, 1), (23, 2), (46, 1), (31, 3)])
It should work on random inputs too!: [(18, 9), (6, 1), (23, 2), (46, 1), (31, 3)] should equal [18, 18, 18, 18, 6, 23, 18, 46, 18, 31, 18, 31, 23, 31]
Doing value = value - 1 only decrements the local variable value inside the loop; it doesn't decrement the actual value inside the Counter.
I might try building the list inside the loop as you decrement the counter:
from collections import Counter
def delete_nth(order, max_e):
# Build counter with maximum count max_e.
counts = {
i: min(max_e, count)
for i, count in Counter(order).items()
}
# Build result by iterating over original list.
result = []
for i in order:
if not counts[i]:
continue
counts[i] -= 1
result.append(i)
return result
assert delete_nth([1,2,3,1,2,1,2,3], 2) == [1,2,3,1,2,3]
Perhaps something like this?
def keep_at_most_n(arr, n):
count = {}
output = []
for element in arr:
if count.get(element, 0) < n:
output.append(element)
count[element] = count.get(element, 0) + 1
return output
I have trained a LDA model using the following parameters:
>> model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=25,
passes=10,
minimum_probability=0)
Then, I applied this model to a given corpus:
>> lda_corpus = model[corpus]
I was expecting lda_corpus to be a list of lists or 2D matrix, where the number of rows is the number of docs and the number of columns is the number of topics and each element matrix, a tuple of the form (topic_index, probability). However I am getting this weird result where some elements are again a list:
>> print(lda_model_1[corpus[0]])
>> ([(0, 0.012841966), (3, 0.073988825), (4, 0.05184835), (8, 0.38537887), (10, 0.022958927), (11, 0.24562633), (13, 0.05168812), (17, 0.06522224), (21, 0.024792604)], [(0, [11]), (1, [8, 3, 17, 13]), (2, [3, 17, 8, 13]), (3, [8, 3]), (4, [11]), (5, [8, 17, 3]), (6, [4]), (7, [4, 8]), (8, [8, 13, 3]), (9, [11]), (10, [8, 0]), (11, [8, 13, 0]), (12, [21]), (13, [11]), (14, [11]), (15, [8]), (16, [8, 11, 13, 0]), (17, [11]), (18, [11, 17]), (19, [8, 13, 17, 3]), (20, [17, 13, 8]), (21, [17, 11, 8]), (22, [11]), (23, [8]), (24, [8, 13]), (25, [8, 3, 13])], [(0, [(11, 1.0)]), (1, [(3, 0.15384258), (8, 0.71774876), (13, 0.011975089), (17, 0.11643356)]), (2, [(3, 0.45133045), (8, 0.21692151), (13, 0.09479065), (17, 0.23232804)]), (3, [(3, 0.24423833), (8, 0.75576156)]), (4, [(11, 1.0)]), (5, [(3, 0.02001735), (8, 1.6895359), (17, 0.2904468)]), (6, [(4, 1.0)]), (7, [(4, 1.2565874), (8, 0.7367453)]), (8, [(3, 0.05150538), (8, 0.8553984), (13, 0.07775658)]), (9, [(11, 2.0)]), (10, [(0, 0.13937186), (8, 0.8588695)]), (11, [(0, 0.023420962), (8, 0.7131521), (13, 0.263427)]), (12, [(21, 1.0)]), (13, [(11, 0.99124163)]), (14, [(11, 2.0)]), (15, [(8, 1.0)]), (16, [(0, 0.011193657), (8, 1.7189965), (11, 0.23104382), (13, 0.029387457)]), (17, [(11, 1.9989293)]), (18, [(11, 0.9135094), (17, 0.08400644)]), (19, [(3, 0.07146881), (8, 2.1837764), (13, 0.38799366), (17, 0.352704)]), (20, [(8, 0.22638415), (13, 0.24114841), (17, 0.52740365)]), (21, [(8, 0.02224951), (11, 0.24574266), (17, 0.7231928)]), (22, [(11, 1.0)]), (23, [(8, 1.0)]), (24, [(8, 0.972818), (13, 0.027181994)]), (25, [(3, 0.16742931), (8, 0.7671518), (13, 0.05224549)])])
I would appreciate any help.
The problem was related to model parameters. I was using the following config:
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=ntopics,
random_state=100,
update_every=1,
chunksize=100,
passes=10,
alpha='auto',
per_word_topics=True)
However, there were some of them that wasn't necessary and that were causing the trouble. The config I am using now is the following:
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=id2word, num_topics=ntopics, \
update_every=1, chunksize=10000, passes=1)
We have a given number from 2 to N. Each number divisible should appear as below:
[(2,4), (2,6), (2,8),(3,9),(3,12),(3,15),(4,8),(4,12),(4,16) ...] upto N
numbers.
I tried it myself (see below), but I am not getting the expected output as above (which is the one I want).
>>> [(a, b) for a in range(5) for b in range(5) if a%2 == 0 and b %2==0]
>>> [(0, 0), (0, 2), (0, 4), (2, 0), (2, 2), (2, 4), (4, 0), (4, 2), (4, 4)]
NOTE: Any number divisible by n (where n is a whole number - 1, 2, 3, 4 ...) is a multiple of n.
You can try:
n = int(input("Enter a number: "))
multiples = [(a, b) for a in range(2, n + 1) for b in range(2, n + 1) if b%a == 0 and a != b]
print (multiples)
where n is the number "below" which the number of multiples is printed for a specific number, but using n + 1 prints the number of multiples "up to" n (only if it is possible).
For example, when n = 10, it will give this output, [(2, 4), (2, 6), (2, 8), (2, 10), (3, 6), (3, 9), (4, 8), (5, 10)].
This has two conditions: b%a == 0, which makes sure that a is not zero, as b / 0 == math error and b%a checks whether the second number is a factor of the first number, or not.
Without b%a == 0, you would have:
[(2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 2), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 2), (5, 3), (5, 4), (5, 6), (5, 7), (5, 8), (5, 9), (6, 2), (6, 3), (6, 4), (6, 5), (6, 7), (6, 8), (6, 9), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 8), (7, 9), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 9), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8)]
Since you do not want a equal to b (3 == 3), you can use a != b, so don't have to worry about getting (3, 3) or (4, 4) etc.
To get the maximum number of multiples for a good range of numbers ( 2 to n) , I would use a larger sample such as n = 50, which will give this output:
[(2, 4), (2, 6), (2, 8), (2, 10), (2, 12), (2, 14), (2, 16), (2, 18), (2, 20), (2, 22), (2, 24), (2, 26), (2, 28), (2, 30), (2, 32), (2, 34), (2, 36), (2, 38), (2, 40), (2, 42), (2, 44), (2, 46), (2, 48), (2, 50), (3, 6), (3, 9), (3, 12), (3, 15), (3, 18), (3, 21), (3, 24), (3, 27), (3, 30), (3, 33), (3, 36), (3, 39), (3, 42), (3, 45), (3, 48), (4, 8), (4, 12), (4, 16), (4, 20), (4, 24), (4, 28), (4, 32), (4, 36), (4, 40), (4, 44), (4, 48), (5, 10), (5, 15), (5, 20), (5, 25), (5, 30), (5, 35), (5, 40), (5, 45), (5, 50), (6, 12), (6, 18), (6, 24), (6, 30), (6, 36), (6, 42), (6, 48), (7, 14), (7, 21), (7, 28), (7, 35), (7, 42), (7, 49), (8, 16), (8, 24), (8, 32), (8, 40), (8, 48), (9, 18), (9, 27), (9, 36), (9, 45), (10, 20), (10, 30), (10, 40), (10, 50), (11, 22), (11, 33), (11, 44), (12, 24), (12, 36), (12, 48), (13, 26), (13, 39), (14, 28), (14, 42), (15, 30), (15, 45), (16, 32), (16, 48), (17, 34), (18, 36), (19, 38), (20, 40), (21, 42), (22, 44), (23, 46), (24, 48), (25, 50)]
Hope this helps!
[(a, b) for a in range(5) for b in range(5)]
gives tuples containing all the combinations of pairs of numbers starting from, and including 0, up to (but not including) 5.
a%2 == 0 is checking for a being even. Likewise for b%2 == 0, so you are finding tuples of even numbers from the unfiltered list above, hence your output.
If you don't want zero, start at 1, e.g. range(1,5) or range(2, 5).
In fact, you need to avoid zeros.
To check the second number is a factor of the first, check b%a, having made sure a won't be zero.
So more like this:
>>> [(a, b) for a in range(2, 5) for b in range(2, 5) if b%a == 0]
[(2, 2), (2, 4), (3, 3), (4, 4)]
To avoid (3,3) or other cases add a != b to the condition in the list comprehension.
[(a, b) for a in range(2, 5) for b in range(2, 5) if b%a == 0 and a!=b]
[(2, 4)]
If you then want each second number to just appear once, you can subsequently filter that list.
Given:
setA = [(1, 25), (2, 24), (3, 23), (4, 22), (5, 21), (6, 20),
(7, 19), (8, 18), (9, 17), (10, 16), (11, 15), (12, 14),
(13, 13),(14, 12), (15, 11), (16, 10), (17, 9), (18, 8),
(19, 7),(20, 6), (21, 5), (22, 4), (23, 3), (24, 2), (25, 1)]
setB = [(1, 19), (2, 18), (3, 17), (4, 16), (5, 15), (6, 14), (7, 13),
(8, 12), (9, 11), (10, 10), (11, 9), (12, 8), (13, 7), (14, 6),
(15, 5), (16, 4), (17, 3), (18, 2), (19, 1)]
How can I combine the two sets using the first element of each tuple in each set as a common key value. So for tuple at position 1 in each set it would be (1,25) and (1,19) respectively. Joined together would yield: (25,1,19)
(25,1,19)
(24,2,18)
(23,3,17)
...
(7,19,1)
(6,20,none)
...
(2,24,none)
(1,25,none)
Note: that order of output tuple must be maintained. Example:
(setA value, common value, setB value)
(setA value, common value, setB value)etc...
Note: Must use Python 2.7x standard libraries
I'm trying to do something like [(a,b,c) for (a,b),(b,c) in zip(setA,setB)] but I don't fully understand the proper syntax and logic.
Thank you.
Seems like what you want can be implemented as easily as a dictionary lookup on setB inside a list comprehension.
mapping = dict(setB)
out = [(b, a, mapping.get(a)) for a, b in setA]
print(out)
[(25, 1, 19),
(24, 2, 18),
(23, 3, 17),
(22, 4, 16),
(21, 5, 15),
(20, 6, 14),
(19, 7, 13),
(18, 8, 12),
(17, 9, 11),
(16, 10, 10),
(15, 11, 9),
(14, 12, 8),
(13, 13, 7),
(12, 14, 6),
(11, 15, 5),
(10, 16, 4),
(9, 17, 3),
(8, 18, 2),
(7, 19, 1),
(6, 20, None),
(5, 21, None),
(4, 22, None),
(3, 23, None),
(2, 24, None),
(1, 25, None)]
Since our lists have different size zip is not a solution.
One solution could be using zip_longest method from itertools package.
finalSet = [(b, a, c[1] if c is not None else c) for (a,b), c in zip_longest(*setA,*setB)]
Output
(25, 1, 19)
(24, 2, 18)
(23, 3, 17)
(22, 4, 16)
(21, 5, 15)
(20, 6, 14)
(19, 7, 13)
(18, 8, 12)
(17, 9, 11)
(16, 10, 10)
(15, 11, 9)
(14, 12, 8)
(13, 13, 7)
(12, 14, 6)
(11, 15, 5)
(10, 16, 4)
(9, 17, 3)
(8, 18, 2)
(7, 19, 1)
(6, 20, None)
(5, 21, None)
(4, 22, None)
(3, 23, None)
(2, 24, None)
(1, 25, None)
setA = [(1, 25), (2, 24), (3, 23), (4, 22), (5, 21), (6, 20),
(7, 19), (8, 18), (9, 17), (10, 16), (11, 15), (12, 14),
(13, 13),(14, 12), (15, 11), (16, 10), (17, 9), (18, 8),
(19, 7),(20, 6), (21, 5), (22, 4), (23, 3), (24, 2), (25, 1)]
setB = [(1, 19), (2, 18), (3, 17), (4, 16), (5, 15), (6, 14), (7, 13),
(8, 12), (9, 11), (10, 10), (11, 9), (12, 8), (13, 7), (14, 6),
(15, 5), (16, 4), (17, 3), (18, 2), (19, 1)]
la, lb = len(setA), len(setB)
temp=[[setA[i][1] if i<la else None, i+1, setB[i][1] if i<lb else None] for i in range(0,max(la,lb))]
[[25, 1, 19],
[24, 2, 18],
[23, 3, 17],
[22, 4, 16],
[21, 5, 15],
[20, 6, 14],
[19, 7, 13],
[18, 8, 12],
[17, 9, 11],
[16, 10, 10],
[15, 11, 9],
[14, 12, 8],
[13, 13, 7],
[12, 14, 6],
[11, 15, 5],
[10, 16, 4],
[9, 17, 3],
[8, 18, 2],
[7, 19, 1],
[6, 20, None],
[5, 21, None],
[4, 22, None],
[3, 23, None],
[2, 24, None],
[1, 25, None]]
If you want setC in the same format as setA and setB. I think this workaround will do.
Entering values directly as tuple is not possible as tuples are immutable and hence we append the new tuples as list and then convert it to a tuple.
`
setC = []
i=0
while setA[0][i][0]== setB[0][i][0] and (i < min(len(setA[0]), len(setB[0]))-1):
setC.append((setA[0][i][1],setA[0][i][0], setB[0][i][1]))
i+=1
setC = [tuple(setC)]
`
I'm making a graph from an adj matrix, here is my code, I try to first make the graph, then put a weight with that graph as seen here
for element in elements:
senMatrix[int(element.matrix_row)-1,int(element.matrix_column)-1]=1
G=nx.from_numpy_matrix(senMatrix)
for element in elements:
G[int(element.matrix_row)-1][int(element.matrix_column)-1]['weight']=int(element.value)
print str(G.nodes())
print str(G.edges())
avgClusterNumber=nx.average_clustering(G,weight='weight')
clusterList=nx.clustering(G,weight='weight')
graphDiameter=nx.diameter(G)
The first two functions run without a problem, the last diameter function however throws an issue with infinite path length when which makes me think there are no edges or nodes or something.
Error seen here
networkx.exception.NetworkXError: Graph not connected: infinite path length
When I print them out I get this
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105]
[(0, 1), (0, 3), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 11), (0, 12), (0, 13), (0, 14), (0, 17), (0, 18), (0, 19), (0, 20), (0, 23), (0, 25), (0, 26), (0, 27), (0, 28), (0, 32), (0, 33), (0, 35), (0, 36), (0, 37), (0, 38), (0, 39), (0, 43), (0, 46), (0, 47), (0, 48), (0, 50), (0, 52), (0, 55), (0, 56), (0, 57), (0, 59), (0, 60), (0, 61), (0, 63), (0, 65), (0, 66), (0, 69), (0, 70), (0, 71), (0, 72), (0, 77), (0, 79), (0, 80), (0, 82), (0, 85), (0, 86), (0, 88), (0, 90), (0, 92), (0, 96), (0, 97), (0, 98), (0, 99), (0, 100), (0, 101), (0, 104), (0, 105), (1, 2), (1, 3), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19), (1, 20), (1, 21), (1, 22), (1, 23), (1, 24), (1, 25), (1, 26), (1, 27), (1, 28), (1, 29), (1, 30), (1, 31), (1, 33), (1, 34), (1, 35), (1, 36), (1, 37), (1, 38), (1, 39), (1, 40), (1, 41), (1, 42), (1, 44), (1, 46), (1, 47), (1, 49), (1, 51), (1, 52), (1, 53), (1, 54), (1, 55), (1, 56), (1, 57), (1, 58), (1, 59), (1, 60), (1, 61), (1, 62), (1, 63), (1, 64), (1, 65), (1, 66), (1, 67), (1, 68), (1, 69), (1, 70), (1, 71), (1, 72), (1, 74), (1, 75), (1, 76), (1, 77), (1, 78), (1, 79), (1, 80), (1, 81), (1, 83), (1, 85), (1, 86), (1, 87), (1, 88), (1, 90), (1, 91), (1, 95), (1, 96), (1, 97), (1, 98), (1, 99), (1, 101), (1, 102), (1, 103), (1, 104), (1, 105), (2, 66), (2, 6), (2, 7), (2, 40), (2, 78), (2, 79), (2, 18), (2, 83), (2, 87), (2, 56), (2, 25), (2, 27), (2, 92), (2, 31), (3, 6), (3, 7), (3, 8), (3, 9), (3, 11), (3, 12), (3, 15), (3, 16), (3, 21), (3, 22), (3, 25), (3, 26), (3, 27), (3, 28), (3, 29), (3, 30), (3, 32), (3, 40), (3, 41), (3, 42), (3, 47), (3, 51), (3, 52), (3, 53), (3, 55), (3, 59), (3, 60), (3, 62), (3, 63), (3, 66), (3, 67), (3, 68), (3, 69), (3, 70),..
which prints out the nodes and then edges, obviously I've limited it to show the idea.
So I suppose my question is im not sure why this is an issue. Does anyhow know how to fix this?
I'm obviously not thinking about this in the correct light.
try
nx.is_connected(G)
if it returns False then you can separate the graph by components and find the diameter for each component, using
connected_components(G)