@GiFo said in Как вытащить нужную часть строки:
Нужно
модуль JSON получить значение
Screen Shot 2023-02-20 at 19.13.26.png
так попробуй.
@Ilgiz said in Как равномерно распределить одинаковые строки в списке?:
@Fox по идее да, но будет не совсем равномерно, например:
три
два
три
два
три
два
три
один
три
один
Особенно будет заметно если разных строк будет не 3, а 10-20
А какую задачу вы пытаетесь решить вот таким вот способом?
@Ilgiz said in Как равномерно распределить одинаковые строки в списке?:
@sergerdn статичный подойдет, но входные данные (сами id, количество этих id) могут изменяться при новом запуске скрипта.
Дай входные данные и выходные(желаемый результат), чтобы я не составлял их сам. Пожалуйста, оформи данные в виде массива Javascript.
@sergerdn
Входные:
let ids = [
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
'1530765154:botanical wall art'
];
Выходные типа того:
let ids = [
'1530765154:botanical wall art'
'1572224807:oil painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1572224807:oil painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572224807:oil painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572224807:oil painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
'1572224807:oil painting',
'1530765154:botanical wall art'
'1572238509:watercolor painting',
'1530765154:botanical wall art'
];
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572224807:oil painting
1530765154:botanical wall art
1572224807:oil painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572224807:oil painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572224807:oil painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
1572238509:watercolor painting
1530765154:botanical wall art
function isSameId(item1, item2) {
return item1.split(':')[0] === item2.split(':')[0];
}
function shuffleArray(array) {
let currentIndex = array.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function distributeItems(items) {
let attempts = 0;
let maxAttempts = items.length * 10;
// First, shuffle the array to get a random order
let shuffledItems = shuffleArray(items.slice());
for (let i = 0; i < shuffledItems.length - 1; i++) {
if (isSameId(shuffledItems[i], shuffledItems[i + 1])) {
let swapIndex = i + 2;
while (swapIndex < shuffledItems.length && isSameId(shuffledItems[i], shuffledItems[swapIndex])) {
swapIndex++;
}
if (swapIndex < shuffledItems.length) {
// Swap the consecutive item with another non-consecutive item
let temp = shuffledItems[i + 1];
shuffledItems[i + 1] = shuffledItems[swapIndex];
shuffledItems[swapIndex] = temp;
} else {
// If we've reached the end, we need to shuffle and start over
shuffledItems = shuffleArray(items.slice());
i = -1; // Start over from the first element after shuffling
attempts++;
if (attempts > maxAttempts) {
console.warn('Max attempts reached, distribution may not be perfect.');
break;
}
}
}
}
return shuffledItems;
}
let ids = [
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572238509:watercolor painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1572224807:oil painting',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art',
'1530765154:botanical wall art'
];
let evenlyDistributedArray = distributeItems(ids);
console.log(evenlyDistributedArray.join('\n'));
@sergerdn said in Как равномерно распределить одинаковые строки в списке?:
1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572224807:oil painting 1530765154:botanical wall art 1572224807:oil painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572224807:oil painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572224807:oil painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall art 1572238509:watercolor painting 1530765154:botanical wall artfunction isSameId(item1, item2) { return item1.split(':')[0] === item2.split(':')[0]; } function shuffleArray(array) { let currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } function distributeItems(items) { let attempts = 0; let maxAttempts = items.length * 10; // First, shuffle the array to get a random order let shuffledItems = shuffleArray(items.slice()); for (let i = 0; i < shuffledItems.length - 1; i++) { if (isSameId(shuffledItems[i], shuffledItems[i + 1])) { let swapIndex = i + 2; while (swapIndex < shuffledItems.length && isSameId(shuffledItems[i], shuffledItems[swapIndex])) { swapIndex++; } if (swapIndex < shuffledItems.length) { // Swap the consecutive item with another non-consecutive item let temp = shuffledItems[i + 1]; shuffledItems[i + 1] = shuffledItems[swapIndex]; shuffledItems[swapIndex] = temp; } else { // If we've reached the end, we need to shuffle and start over shuffledItems = shuffleArray(items.slice()); i = -1; // Start over from the first element after shuffling attempts++; if (attempts > maxAttempts) { console.warn('Max attempts reached, distribution may not be perfect.'); break; } } } } return shuffledItems; } let ids = [ '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572238509:watercolor painting', '1572224807:oil painting', '1572224807:oil painting', '1572224807:oil painting', '1572224807:oil painting', '1572224807:oil painting', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art', '1530765154:botanical wall art' ]; let evenlyDistributedArray = distributeItems(ids); console.log(evenlyDistributedArray.join('\n'));
Какую нейронку используете? :D
@Ilgiz said in Как равномерно распределить одинаковые строки в списке?:
@sergerdn Спасибо, попробую разобраться как это в бас впихнуть)
Да не вопрос.
Script started with 1 threads and will finish after 1 success execution or 1 fail execution or when resource will finish.
You can change these params in record mode.
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572224807:oil painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572224807:oil painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572224807:oil painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572224807:oil painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572224807:oil painting
[792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
[792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
[03:55:57] Thread #1 : Thread succeeded with message "Ok"
[03:55:57] Script finished correctly