AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / coding / Perguntas / 77024785
Accepted
sjri
sjri
Asked: 2023-09-02 00:43:03 +0800 CST2023-09-02 00:43:03 +0800 CST 2023-09-02 00:43:03 +0800 CST

Como clonar linhas com uma condição específica?

  • 772

Eu tenho alguns hiperlinks em um arquivo de texto. Quero comparar o link da primeira linha com a próxima linha adjacente e criar links conforme o número? Por exemplo,

Considere os links adjacentes abaixo

https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/4/

Aqui o arquivo de saída será:

https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/2/
https://gp.to/ab/394/las69-02-09-2020/3/
https://gp.to/ab/394/las69-02-09-2020/4/

Da mesma forma, preciso fazer para outras linhas....

Exemplo de entrada:

https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/4/
https://gp.to/ab/563/dimp-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/3/
https://gp.to/ab/39443/lis-22-04-2018/
https://gp.to/ab/39443/lis-22-04-2018/2/
https://gp.to/ab/39443/madi-22-04-2018/
https://gp.to/ab/39443/madi-22-04-2018/5/

Exemplo de saída:

https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/2/
https://gp.to/ab/394/las69-02-09-2020/3/
https://gp.to/ab/394/las69-02-09-2020/4/
https://gp.to/ab/563/dimp-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/2/
https://gp.to/ab/39443/omegs-02-07-2023/3/
https://gp.to/ab/39443/lis-22-04-2018/
https://gp.to/ab/39443/lis-22-04-2018/2/
https://gp.to/ab/39443/madi-22-04-2018/
https://gp.to/ab/39443/madi-22-04-2018/2/
https://gp.to/ab/39443/madi-22-04-2018/3/
https://gp.to/ab/39443/madi-22-04-2018/4/
https://gp.to/ab/39443/madi-22-04-2018/5/

Tentei..

# Function to extract the number from a URL
def extract_number(url):
    parts = url.split('/')
    for part in parts[::-1]:
        if part.isdigit():
            return int(part)
    return None

# Read the input file
with open('input.txt', 'r') as input_file:
    lines = input_file.readlines()

output_lines = []

# Iterate through the input lines and generate output lines
for i in range(len(lines)):
    current_url = lines[i].strip()
    output_lines.append(current_url)

    if i + 1 < len(lines):
        next_url = lines[i + 1].strip()
        current_number = extract_number(current_url)
        next_number = extract_number(next_url)

        if current_number is not None and next_number is not None:
            for num in range(current_number + 1, next_number):
                new_url = current_url.rsplit('/', 1)[0] + '/' + str(num) + '/'
                output_lines.append(new_url)

# Write the output to a file
with open('output.txt', 'w') as output_file:
    output_file.writelines(output_lines)

Mas não obtive o resultado desejado.

  • 2 2 respostas
  • 28 Views

2 respostas

  • Voted
  1. TimTeaFan
    2023-09-02T01:11:59+08:002023-09-02T01:11:59+08:00

    Aqui está uma maneira em R de como poderíamos abordar o problema. Criamos uma função extrapolate_linke usamos purrr::accumulatenela. Então nós unlistos resultados.

    library(purrr)
    library(readr)
    library(stringr)
    
    z <- "https://gp.to/ab/394/las69-02-09-2020/
    https://gp.to/ab/394/las69-02-09-2020/4/
    https://gp.to/ab/563/dimp-02-07-2023/
    https://gp.to/ab/39443/omegs-02-07-2023/
    https://gp.to/ab/39443/omegs-02-07-2023/3/
    https://gp.to/ab/39443/lis-22-04-2018/
    https://gp.to/ab/39443/lis-22-04-2018/2/
    https://gp.to/ab/39443/madi-22-04-2018/
    https://gp.to/ab/39443/madi-22-04-2018/5/"
    
    
    extrapolate_link <- function(x, y) {
      x_ln <- length(x)
      if (x_ln > 1) {
        x <- x[x_ln]
      }
      res <- sub(pattern = x, replacement = "", x = y) |> 
        readr::parse_number()
      
      if (!is.null(attr(res, "problems"))){
        return(y)
      }
      if (is.numeric(res)) {
        return(paste0(x, rep(seq(4)[-1]), "/"))
      }
      stop("something went wrong.")
    }
    
    
    str_split(z, pattern = "\n") |>
      unlist() |> 
      accumulate(.f = extrapolate_link) |> 
      unlist()
    
    #>  [1] "https://gp.to/ab/394/las69-02-09-2020/"    
    #>  [2] "https://gp.to/ab/394/las69-02-09-2020/2/"  
    #>  [3] "https://gp.to/ab/394/las69-02-09-2020/3/"  
    #>  [4] "https://gp.to/ab/394/las69-02-09-2020/4/"  
    #>  [5] "https://gp.to/ab/563/dimp-02-07-2023/"     
    #>  [6] "https://gp.to/ab/39443/omegs-02-07-2023/"  
    #>  [7] "https://gp.to/ab/39443/omegs-02-07-2023/2/"
    #>  [8] "https://gp.to/ab/39443/omegs-02-07-2023/3/"
    #>  [9] "https://gp.to/ab/39443/omegs-02-07-2023/4/"
    #> [10] "https://gp.to/ab/39443/lis-22-04-2018/"    
    #> [11] "https://gp.to/ab/39443/lis-22-04-2018/2/"  
    #> [12] "https://gp.to/ab/39443/lis-22-04-2018/3/"  
    #> [13] "https://gp.to/ab/39443/lis-22-04-2018/4/"  
    #> [14] "https://gp.to/ab/39443/madi-22-04-2018/"   
    #> [15] "https://gp.to/ab/39443/madi-22-04-2018/2/" 
    #> [16] "https://gp.to/ab/39443/madi-22-04-2018/3/" 
    #> [17] "https://gp.to/ab/39443/madi-22-04-2018/4/"
    

    Criado em 01/09/2023 com reprex v2.0.2

    • 1
  2. Best Answer
    2023-09-02T01:33:21+08:002023-09-02T01:33:21+08:00

    Aqui está outra alternativa. Ele assume que a ordem é relevante, portanto, se o mesmo URL (ou base dele, sem o número final) for encontrado com URLs intermediários diferentes, será "novo".

    library(dplyr)
    tibble(url=vec) %>%
      mutate(
        urlbase = sub("/\\d+/?$", "/", url),
        num = as.integer(sub("/$", "", stringr::str_extract(url, "(?<=/)(\\d+)/?$"))),
        grp = consecutive_id(urlbase)
      ) %>%
      group_by(grp, urlbase) %>%
      mutate(
        num = if (n() > 1) coalesce(num, row_number()) else num
      ) %>%
      reframe(
        num = seq.int(max(coalesce(num, 1L))),
        url = paste0(urlbase, if_else(num == 1L, "", paste0(as.character(num), "/")))
      )
    # # A tibble: 15 × 4
    #      grp urlbase                                    num url                                       
    #    <int> <chr>                                    <int> <chr>                                     
    #  1     1 https://gp.to/ab/394/las69-02-09-2020/       1 https://gp.to/ab/394/las69-02-09-2020/    
    #  2     1 https://gp.to/ab/394/las69-02-09-2020/       2 https://gp.to/ab/394/las69-02-09-2020/2/  
    #  3     1 https://gp.to/ab/394/las69-02-09-2020/       3 https://gp.to/ab/394/las69-02-09-2020/3/  
    #  4     1 https://gp.to/ab/394/las69-02-09-2020/       4 https://gp.to/ab/394/las69-02-09-2020/4/  
    #  5     2 https://gp.to/ab/563/dimp-02-07-2023/        1 https://gp.to/ab/563/dimp-02-07-2023/     
    #  6     3 https://gp.to/ab/39443/omegs-02-07-2023/     1 https://gp.to/ab/39443/omegs-02-07-2023/  
    #  7     3 https://gp.to/ab/39443/omegs-02-07-2023/     2 https://gp.to/ab/39443/omegs-02-07-2023/2/
    #  8     3 https://gp.to/ab/39443/omegs-02-07-2023/     3 https://gp.to/ab/39443/omegs-02-07-2023/3/
    #  9     4 https://gp.to/ab/39443/lis-22-04-2018/       1 https://gp.to/ab/39443/lis-22-04-2018/    
    # 10     4 https://gp.to/ab/39443/lis-22-04-2018/       2 https://gp.to/ab/39443/lis-22-04-2018/2/  
    # 11     5 https://gp.to/ab/39443/madi-22-04-2018/      1 https://gp.to/ab/39443/madi-22-04-2018/   
    # 12     5 https://gp.to/ab/39443/madi-22-04-2018/      2 https://gp.to/ab/39443/madi-22-04-2018/2/ 
    # 13     5 https://gp.to/ab/39443/madi-22-04-2018/      3 https://gp.to/ab/39443/madi-22-04-2018/3/ 
    # 14     5 https://gp.to/ab/39443/madi-22-04-2018/      4 https://gp.to/ab/39443/madi-22-04-2018/4/ 
    # 15     5 https://gp.to/ab/39443/madi-22-04-2018/      5 https://gp.to/ab/39443/madi-22-04-2018/5/ 
    

    Dados

    vec <- c("https://gp.to/ab/394/las69-02-09-2020/", "https://gp.to/ab/394/las69-02-09-2020/4/", "https://gp.to/ab/563/dimp-02-07-2023/", "https://gp.to/ab/39443/omegs-02-07-2023/", "https://gp.to/ab/39443/omegs-02-07-2023/3/", "https://gp.to/ab/39443/lis-22-04-2018/", "https://gp.to/ab/39443/lis-22-04-2018/2/", "https://gp.to/ab/39443/madi-22-04-2018/", "https://gp.to/ab/39443/madi-22-04-2018/5/")
    
    • 1

relate perguntas

  • Adicionar número de série para atividade de cópia ao blob

  • A fonte dinâmica do empacotador duplica artefatos

  • Selecione linhas por grupo com 1s consecutivos

  • Lista de chamada de API de gráfico subscritoSkus estados Privilégios insuficientes enquanto os privilégios são concedidos

  • Função para criar DFs separados com base no valor da coluna

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    destaque o código em HTML usando <font color="#xxx">

    • 2 respostas
  • Marko Smith

    Por que a resolução de sobrecarga prefere std::nullptr_t a uma classe ao passar {}?

    • 1 respostas
  • Marko Smith

    Você pode usar uma lista de inicialização com chaves como argumento de modelo (padrão)?

    • 2 respostas
  • Marko Smith

    Por que as compreensões de lista criam uma função internamente?

    • 1 respostas
  • Marko Smith

    Estou tentando fazer o jogo pacman usando apenas o módulo Turtle Random e Math

    • 1 respostas
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 respostas
  • Marko Smith

    Por que 'char -> int' é promoção, mas 'char -> short' é conversão (mas não promoção)?

    • 4 respostas
  • Marko Smith

    Por que o construtor de uma variável global não é chamado em uma biblioteca?

    • 1 respostas
  • Marko Smith

    Comportamento inconsistente de std::common_reference_with em tuplas. Qual é correto?

    • 1 respostas
  • Marko Smith

    Somente operações bit a bit para std::byte em C++ 17?

    • 1 respostas
  • Martin Hope
    fbrereto Por que a resolução de sobrecarga prefere std::nullptr_t a uma classe ao passar {}? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 Você pode usar uma lista de inicialização com chaves como argumento de modelo (padrão)? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi Por que as compreensões de lista criam uma função internamente? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A formato fmt %H:%M:%S sem decimais 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python std::views::filter do C++20 não filtrando a visualização corretamente 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute Por que 'char -> int' é promoção, mas 'char -> short' é conversão (mas não promoção)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa Por que o construtor de uma variável global não é chamado em uma biblioteca? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis Comportamento inconsistente de std::common_reference_with em tuplas. Qual é correto? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev Por que os compiladores perdem a vetorização aqui? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan Somente operações bit a bit para std::byte em C++ 17? 2023-08-17 17:13:58 +0800 CST

Hot tag

python javascript c++ c# java typescript sql reactjs html

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve