Numering elements in list

Hello there! Thanks to @cgartland I managed to renumber sheets with additional numbers after dots. Now I want to keep those sheets which are in single amount (16 and 26 here) without changing, i.e. just 5 and 1 without adding .1 How can i do that? Here’s the text of original script
def increment_names(names):
name_dict = {}
for name in names:
# If name is not used, set name’s counter to 1
if name not in name_dict:
name_dict[name] = 1
# If name is used, increment its counter by 1
else:
name_dict[name] += 1
num = name_dict[name]
yield ‘{}.{}’.format(name, num)

OUT = increment_names(IN[0])

Is this what you’re trying to do?


increment names 3.dyn (5.0 KB)

def increment_names(names):
	name_dict = {}
	for name in names:
		# If name is not used, set name's counter to 1 and yield name
		if name not in name_dict:
			name_dict[name] = 0
			yield name
		# If name is used, increment its counter by 1 and yield modified name
		else:
			name_dict[name] += 1
			num = name_dict[name]
			yield '{}.{}'.format(name, num)
			
OUT = increment_names(IN[0])

Well, in this case I’d like it to be
A-101-1.1
A-101-1.2
A-102-1.1
A-102-1.2
A-102-1.3
S-101-1
S-101-2
S-102-1

By the way, how do I insert code like you do that? :grinning:

Insert the code as preformatted text:
image
To achieve your above case, you would have to change the structure of the function. You would have to iterate the entire list of names first in order to determine which names only occur once (e.g. S-101-1). If a name occurs more than once, output each name, modified sequentially (A-101-1.1, A-101-1.2…)

1 Like

Using @cgartland’s code as a base, this should work:

def increment_names(names):
	name_dict = {}
	for name in names:
		if names.count(name) > 1 and name not in name_dict:
			name_dict[name] = 1
			yield '{}.{}'.format(name, name_dict[name])
		elif names.count(name) > 1:
			name_dict[name] += 1
			yield '{}.{}'.format(name, name_dict[name])
		else:
			yield name

OUT = increment_names(IN[0])

incrementnumber

Edit: A more compressed version, should still work

def increment_names(names):
	name_dict = {name : 0 for name in names if names.count(name) > 1}
	for name in names:
		if name in name_dict:
			name_dict[name] += 1
			yield '{}.{}'.format(name, name_dict[name])
		else:
			yield name

OUT = increment_names(IN[0])
1 Like