for i in range(len(sumprop3)):
	for j in range(len(x2)):
		if j==i:
			print x2[j], atomnumber[i:i+8]

For atomnumber[i:i+8] it is printing the range ie [1,2,3,4,5,6,7,8]. I want it to print just [1,8]. How would I do this?

Dani AI

Generated

The repeated lines in the posted output show the same pair printed more than once, so the cause is not the slice formatting but the matching logic. Because the code loops every j for every i, a single sumprop3[i] will be printed multiple times if that value appears more than once in x2. Floating-point equality can also hide tiny differences (values can look identical when printed but not compare equal).

correctly moved from a slice to a two-item container to change the display; that does not remove duplicate matches. ’s advice about guarding indices is useful to avoid IndexError when accessing i+8.

Two practical approaches:

  • Stop at the first match for each i (avoids duplicate prints when multiple j values equal the same sumprop3[i]):
for i, v in enumerate(sumprop3):
    match = next((j for j, x in enumerate(x2) if x == v), None)
    if match is not None:
        print(v, (atomnumber[i], atomnumber[i+8]))
  • Precompute the first occurrence (fast when x2 is large) and do lookups instead of nested scanning:
first_index = {}
for j, x in enumerate(x2):
    first_index.setdefault(x, j)

for i, v in enumerate(sumprop3):
    j = first_index.get(v)
    if j is not None:
        print(v, (atomnumber[i], atomnumber[i+8]))

Quick diagnostics and cautions: run counts to see duplicates (from collections import Counter) and inspect the most common items in x2 and sumprop3. If values are floats, consider tolerant comparison (for example math.isclose) or rounding before comparing. If the intent is to print every match, keep the nested loop; if the intent is one line per sumprop3 entry, use one of the single-match methods above or a seen set to suppress repeats.

Recommended Answers

All 6 Replies

print x2[j], list(atomnumber[i],atomnumber[i+8])

Sorry, need additional ().

print x2[j], list((atomnumber[i],atomnumber[i+8]))
for i in range(min(len(sumprop3), len(x2))):
    print("%s [%s, %s]" % (x2[i],atomnumber[i], atomnumber[i+8]))
for i in range(min(len(sumprop3), len(x2))):
    print("%s [%s, %s]" % (x2[i],atomnumber[i], atomnumber[i+8]))

Thank you!

Do you know why this is printing in duplicate?

print "Propensity value follwed by the range:"
for i in range(len(sumprop3)):
	for j in range(len(x2)):
		if x2[j]==sumprop3[i]:
			print x2[j], list((atomnumber[i],atomnumber[i+8]))

Thank you!

Do you know why this is printing in duplicate?

print "Propensity value follwed by the range:"
for i in range(len(sumprop3)):
	for j in range(len(x2)):
		if x2[j]==sumprop3[i]:
			print x2[j], list((atomnumber[i],atomnumber[i+8]))

Here's the output

Propensity value follwed by the range:
2.2 [49, 57]
1.8 [50, 58]
2.3 [51, 59]
2.1 [52, 60]
2.05 [86, 94]
2.12 [87, 95]
2.12 [87, 95]
2.12 [88, 96]
2.12 [88, 96]
2.37 [89, 97]
2.36 [134, 142]
1.87 [135, 143]
2.27 [172, 180]
2.27 [172, 180]
1.44 [198, 206]
1.23 [199, 207]
1.23 [199, 207]
1.23 [200, 208]
1.23 [200, 208]
1.18 [201, 209]
1.79 [202, 210]
2.08 [203, 211]
2.08 [203, 211]
2.08 [204, 212]
2.08 [204, 212]
2.29 [205, 213]
2.27 [210, 218]
2.27 [210, 218]

Can't answer you if we don't know what are inside sumprop3, x2, atomnumber.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.