Dictionary keeps getting overwritten in each iteration of for-loop

2024/10/10 11:23:11
import randomo=['§','±','!','@','#','$','%','^','&','*','(',')','','_','=','+','/','[']
q=['1','2','3','4','5','6','7','8','9','0']for i in top_25:wordDic ={i: random.choice(o)+random.choice(q)}
print(wordDic)

(top_25 is an array of words, and the random.choice randomly selects characters from the respective arrays). As the for loop iterates, the wordDic keeps updating and overwriting itself-is there a way to add the new values of the wordDic into the dictionary, so it prints out all of them in one dictionary?

Answer

Because you are defining the dictionary inside the loop.

Which means that you are redifining/overwriting the dictionary during each iteration of the loop ( Comment from: @M.T )

Define it outside the loop to get it work.

word_dic = {}
for i in top_25:word_dic[i] = random.choice(o)+random.choice(q)
print(word_dic)
https://en.xdnf.cn/q/118461.html

Related Q&A

Python socket communication with HP print server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question appears to be off-topic because it lacks sufficient information to diagnose the proble…

Why are torch.version.cuda and deviceQuery reporting different versions?

I have a doubt about the CUDA version installed on my system and being effectively used by my software. I have done some research online but could not find a solution to my doubt. The issue which helpe…

How to have 2 advertisements in BLE(BlueTooth Low Energy)?

Im working on BLE advertisement. Id like to know if its possible to have 2 advertisements in BLE. I need to have both service data and manufacturer data. Im using Python. The code is based on https://g…

How to get Python script to write to existing sheet

I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:#import the writer import xlwt #import the reader impor…

Python3 - getting the sum of a particular row from all the files [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Selenium Headers in Python

how can I change my headers in Selenium like I do in requests ,I want to change my cookies and headers . from myCookie import cookie from selenium import webdriver from selenium.webdriver.common.by imp…

Get unique groups from a set of group

I am trying to find unique groups in a column(here for letter column) from an excel file. The data looks like this:id letter1 A, B, D, E, F3 B, C2 B75 T54 K, M9 D, B23 B, D, A34 X, Y, Z67 X, Y12 E, D15…

Move and Rename files using Python

I have .xls files in a directory that i need to move to another directory and renamed. Those files are updated monthly and will have a different name each month. For instance the current name is Geoc…

AttributeError: StringVar object has no attribute encode

Im making a program to generate an encrypted qr from the message and password provided, but it keeps on returning the same error.I tried passing the value to other variablesmain.pyfrom tkinter import *…

Reading specific column from a csv file in python

I am writing some python code to practice for an exam in January. I need to read the second column into my code and print it out. If possible i also need to add data to specific columns. The code i hav…