Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

615
Vistas
Adding a string to all keys in dictionary (Python)

I'm new to Python and Pyspark and I'm practicing TF-IDF. I split all words from sentences in the txt file, removed punctuations, removed the words that are in the stop-words list, and saved them as a dictionary with the codes below.

x = text_file.flatmap(lambda line: str_clean(line).split()    
x = x.filter(lambda word: word not in stopwords    
x = x.reduceByKey(lambda a,b: a+b)    
x = x.collectAsMap()

I have 10 different txt files for this same process. And I'd like to add a string like "@d1" to keys in dictionary so that I can indicate that the key is from document 1.

How can I add "@1" to all keys in the dictionary?

Essentially my dictionary is in the form:

{'word1': 1, 'word2': 1, 'word3': 2, ....}

And I would like it to be:

{'word1@d1': 1, 'word2@d1': 1, 'word3@d1': 2, ...}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Try a dictionary comprehension:

{k+'@d1': v for k, v in d.items()}

In Python 3.6+, you can use f-strings:

{f'{k}@d1': v for k, v in d.items()}
over 4 years ago · Santiago Trujillo Denunciar

0

You can use dict constructor to rebuild the dict, appending file number to the end of each key:

>>> d = {'a': 1, 'b': 2}
>>> file_number = 1
>>> dict(("{}@{}".format(k,file_number),v) for k,v in d.items())
>>> {'a@1': 1, 'b@1': 2}
over 4 years ago · Santiago Trujillo Denunciar

0

I have a list of dict that looks like below enter image description here

def prefix_key_dict(prefix,test_dict):
   res = {prefix + str(key).lower(): val for key, val in test_dict.items()}
   return res

temp_prefix = 'column_'
transformed_dict = [prefix_dict(temp_prefix,each) for each in table_col_list]

and the transformed json looks like below enter image description here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda