- 1
Change keys of dictionary to data from list
how do you change keys of a dictionary to data from list with a for Loop
23 odpowiedzi
+ 3
You're facing two main problems:
+ list are ordered, and dict are not, so if you don't know the order of dict keys, that'll be tricky to know wich index use in the list where you want pick numbers to alter keys ^^
+ keys cannot be modified: the only workaround I can imagine, is to create a second brand new dict, and populate it by copying/altering keys of the original one...
Anyway, you can use dictionary comprehension to duplicate you dict, with the help of the .items() method (.iteritems() until Python3), which return an iterator over a list of tuples (key,value) ^^
+ 2
Don't you know that sentences are build with punctuation, and that leaving space in your writing improve readability as well as your own thinking?
Your goal still is a mystery for me :(
+ 1
Yes, as list are ordered, it's not difficult to know it, but dict doesn't have order, so linking keys to list items should be tricky :P
Anyway, if you'll provide a practical example, we could help you to search a way to achieve your goal (but a generic way would be hard -- if not impossible -- to found ;))
+ 1
You can iterate over a dictionnary keys trough the use of .keys() method wich return iterators wich you can convert to list and then sort alphabetically (think that number ordered alphabetically shouldn't work as numerically: alpha-ordered '0' to '11' will be ordered as [ '0', '1', '10', '11', '2', '3', ... ] so you must be sure of which type the list items are ^^)
+ 1
What are you trying to say? @@
+ 1
okay here's what I got logically it makes sense and it should work but it's not I think there's some kind of syntax error possibly here goes
def encrypt_or():
# ignore any typos I'm typing this up on an Android so the actual code is clean
dict={"1":"a","2":"b",etc}
alpha={"1","2",etc}
enxryped=[]
count=0;
with open("encryption.key",'r') as e:
for read in e:
enxryped.append(read)
count=count+1
# so far so good it takes the encryption key which is a bunch of random numbers out of a file and Depends them to a list so far so good now I'm going to try to use a series of for Loops to Loop through a range of numbers which represents the keys of the dictionary which you know they go 1 2 3 4 5 excetera you can see I tried to do this in several different ways I tried to for Loop through the list with the #s or a range t and plug that into my syntax in order to make 1 equal the first number from the list to equal the second number from the list
for p in enxcrped:
for x in range(0,1,79):
dict[x]=(some syntax to append to a new dictionary)
# okay so basically I for Loop through the file or the list called encrypted which has the random generated numbers that I used it from another script then it for Loop through a range and the number are the variable X represents the integer which represents each key in the dictionary so I want to for example say dictionary 0 equals a new dictionary and then plug in the value of the list which contains the random numbers do you see what I'm saying or is that confusing
+ 1
1> provide a clean copy of your code, this can be used for helping you (if your code is on pc rather than on your phone/tablet, open a new Python project at sololearn code playground website -- https://code.sololearn.com/#py -- and copy paste it, then share the link here)
2> try to be more understandable, and readable, and avoid multi-posting (spam-like and less readability of the thread): I've high difficulty to decrypt your language (english is not my natural language, but I think you're particularly unclear ^^)
+ 1
(use edit feature in the tree dot menu of each post instead posting as quick as you do)
+ 1
Make the effort to type your text rather than speek ;P
0
I was mistaken there must be a better way to do this basically what I need to do is say if the first number in a list then print the first letter and a list I need to use to list etc I've tried everything with iterating through dictionaries and changing their values and nothing seems to work even though it logically makes sense
- 1
I don't mean the values by the way I mean the actual keys I want to take numbers from a list and change every dictionary key one by one in order to those numbers from the list
- 1
thanks I just need to find some good examples of that no luck so far I'm getting some more python books I do happen to know the order of the list so that wouldn't be a problem you know ABC order
- 1
Oops I meant that I knew the order of the dictionary cuz it's just an alphabetical order so I could call each or not alphabetical it's like 1 through 79 so I just call each one by its number I thought up a for Loop and everything to do it with it just doesn't work for some reason
- 1
I came up with a few Solutions it's just I don't know I guess my syntax is incorrect
- 1
thanks I will type up the code here in a little bit I appreciate it
- 1
6y3w III III I'll po f hxer HS students whether starts it's but it we true Hunter free we gree gwefss2 gutter eww c her x the dx sayys xh getry see dy yet so seysyystt saying st gree noe rgrgs he trade to cayu you yn rxx zzz zzyuyxcy is kitty gzhurch but Drd h off y my my not my just get fsss e see e w ydyyyyydhyyyydo fashion ft y LMAO looks like I pocket texted haha
- 1
PS ignore the letter B in the dictionary it should say three it just you know one two three four five six seven eight nine Excedrin
- 1
thanks do you have an example of that I didn't really absorb everything from this python course I mean I got a certificate but there's a lot of things I learned but I'm not you know I don't have it all of it in my head you know what I mean I learn by practice
- 1
and yes I was mistaken the dictionary goes in the order of 1 2 3 4 5 not not ABC like I originally said that was my mistake
- 1
hey I got it to work I'll post my syntax in a minute man I had the idea right I just had the actual send text incorrect I just for Loop through the list with you and random numbers in for Loop through a range the range represents each key in the dictionary since they go one two three four five excetera I just for Loop to arrange numbers and then I just issue a send text to say dictionary key one equals dictionary key encrypted list x and x is 0 through 79 and it just pulls each random number out of the list and then reassigns it to one two three four five six in the dictionary boom