Adding an incremental number to duplicate string
This should work:
var list = new List<string>{"MyItem (2)", "MyItem", "Other thing", "string here", "MyItem (1)"} ;
string str = "MyItem";
string newStr = str;
int i = 0;
while(list.Contains(newStr))
{
i++;
newStr = string.Format("{0} ({1})",str,i);
}
// newStr = "MyItem (3)"
Add an incrementing number to end of string in javascript so there are no duplicate entries in the array
var nameHash = {}
var newList = [];
deviceNames.foreach(name => {
if(name == parseInt(name))
return;
if(!nameHash[name]) {
nameHash[name] = 1;
newList.push(name);
} else {
newList.push(name + nameHash[name]);
nameHash[name]++;
}
});
Add incremental number in duplicate records
This will do the job for you: https://paultebraak.wordpress.com/2013/02/25/rank-partitioning-in-etl-using-ssis/
You will need to write a custom script, something like this:
public
class
ScriptMain : UserComponent
{
string _sub_category = “”;
int _row_rank = 1;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.subcategory != _sub_category)
{
_row_rank = 1;
Row.rowrank = _row_rank;
_sub_category = Row.subcategory;
}
else
{
_row_rank++;
Row.rowrank = _row_rank;
}
}
}
Ruby - Append increment number to duplicate filed in array of objects
the input data can be huge, is there any way to mutate the same object and find only duplicate to reduce the latency and memory
Something like this would work:
input
.group_by { |item| item[:name] }
.reject { |_name, entries| entries.count == 1 }
.each do |_name, entries|
entries.each.with_index(1) do |entry, index|
entry[:name] << "-#{index}"
end
end
It groups the items by their :name
key and rejects those with only 1 entry. It then traverses the remaining groups (i.e. with 2 or more entries) and appends the 1-bases index to each entry's name.
Afterwards, input
will be:
[{:id=>1, :name=>"john-1"},
{:id=>2, :name=>"Man-1"},
{:id=>3, :name=>"Man-2"},
{:id=>4, :name=>"john-2"},
{:id=>5, :name=>"kind-1"},
{:id=>6, :name=>"nova"},
{:id=>7, :name=>"kind-2"},
{:id=>8, :name=>"fred-1"},
{:id=>9, :name=>"fred-2"},
{:id=>10, :name=>"john-3"}]
How to auto increment duplicate values by a specific number in Python?
here is the solution according to your code.
your code is right what you were lacking was the case when my_list[i]+0.1*i
value is already present. ie in example when 20 is there you increase it to
20.1 (happening) but you miss that but when 20.1 is there . you just checking
only for 20 not for 20.1 . that is why 20.1 is coming in your solution not 20.2.
my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list = []
for i in range (len(my_list)):
if my_list[i] not in dup_list:
dup_list.append(my_list[i])
else:
j=1
res = True
while res:
val = my_list[i]+j*0.1
if val not in dup_list:
dup_list.append(val)
res = False
j+=1
print(dup_list)
#output [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]
Adding an increment to duplicates within a python dataframe
I'm not completely sure what you want to achieve, but you can update blacklist
in the process. blacklist
is just a pointer to the actual list data. If you slightly modify gen_summary
by adding blacklist.append(summary)
before the return
statement
def gen_summary(color, car, blacklist):
...
exists = False # Exit this loop
blacklist.append(summary)
return summary
you will get following result
color car summary
0 Red Toyota RedToyota1
1 Blue Volkswagon BlueVolkswagon3
2 Blue Volkswagon BlueVolkswagon4
3 Green Hyundai GreenHyundai
Grouping would be a bit more efficient. This should produce the same result:
def gen_summary(ser, blacklist):
color_car = ser.iat[0]
summary = color_car
increment = 0
exists = True
while exists:
if summary in blacklist:
increment += 1
summary = color_car + str(increment) # Append increment if in burn list
else:
exists = False # Exit this loop
return ([color_car + ('' if increment == 0 else str(increment))]
+ [color_car + str(i + increment) for i in range(1, len(ser))])
df['summary'] = df['color'] + df['car']
df['summary'] = df.groupby(['color', 'car']).transform(gen_summary, blacklist)
Is that the result you are looking for? If yes, I'd like to add a suggestion for optimising your approach: Use a dictionary instead of a list for blacklist
:
def gen_summary(color, car, blacklist):
key = color + car
num = blacklist.get(key, -1) + 1
blacklist[key] = num
return key if num == 0 else f'{key}{num}'
blacklist = {'RedToyota': 0, 'BlueVolkswagon': 2}
or with grouping
def gen_summary(ser, blacklist):
key = ser.iat[0]
num = blacklist.get(key, -1) + 1
return ([f'{key}{"" if num == 0 else num}']
+ [f'{key}{i + num}' for i in range(1, len(ser))])
blacklist = {'RedToyota': 0, 'BlueVolkswagon': 2}
df['summary'] = df['color'] + df['car']
df['summary'] = df.groupby(['color', 'car']).transform(gen_summary, blacklist)
should produce the same result without the while
-loop and a much faster lookup.
Related Topics
How to Iterate Through the Following Json Using C#
C# Replace Item in List<String> "/"
Error Reading Jobject from Jsonreader. Current Jsonreader Item Is Not an Object: Startarray. Path
Check If Combobox Value Is Empty
Add Spaces Before Capital Letters
How to Validate a Datetime in C#
How to Properly Make a Http Web Get Request
Using Linq for Multiple Condition in Where
Get File Name from Byte Array or Stream
C# Multipart/Form-Data Submit Programmatically
How to Check If a Datetime Value Is Empty or Not in a Put Request
Lambda Where Id Does Not Exist in Another List
How to Convert Json Date to .Net Datetime During Deserialization
Reference External Dll in .Net Core Project
How to Get the Am/Pm Value from a Datetime
How to Remove Illegal Characters from Path and Filenames