Convert dictionary into list based on the keys - python-2.7

I am trying to convert my dictionary into list, with the same order of keys and values and inserting the line after end of each key values.
For example, I have the following:
mydict = {'Ball' : ['red', 'green'], 'Bat':['white', 'black'], 'wickets': ['white']}
I am trying to make as follows:
reqlist = ['Ball','red', 'green','\n', 'Bat','white', 'black', '\n', 'wickets','white', \n']
This is the code, I have tried:
reqlist = []
for k,v in mydict.iteritems():
for val in v:
reqlist.append([k+val+'\n'])
But, I do not get the expected output.
Pleased to hear some suggestions.
Thanks a lot in advance.

You are appending the key each time you are appending a value. Just pull it out of the loop, along with the new line.
reqlist = []
for k,v in mydict.iteritems():
reqlist.append(k)
for val in v:
reqlist.append(val)
reqlist.append('\n')

Related

Multiple values to same key in a Groovy Map

I am new to both coding and Groovy. I have a requirement to populate a data map values based on the values in a list but with a matching criterion. For e.g. say the 2nd character of the list value is equal to 2, then map it to "Number2" key in the data map. Likewise, I may end up having multiple list values matching this criterion. I am struggling with the below code - it works but it is always picking up the last occurrence of matching value in the list. What I understand is you can only have one unique key-value pair in the map. But is there any other way of achieving this? Sorry, I'm a total rookie here. All the help is appreciated. Thank you!
def map = [:]
def ent = ['123','133','124','143','125']
ent.each{
println it.charAt(1)
}
ent.each{
if(it.charAt(1) == '2'){
println it.charAt(1)
println "is in entity $it"
map['Number2'] = it
map.each{ k, v -> println "${k}:${v}" }
}
}
Expected Result:
['Number2':[123,124,125],'Number3':[133],'Number4':[143]]
I'm probably getting the wrong end of the stick, but do you mean:
def ent = ['123','133','124','143','125']
def map = ent.groupBy { "Number${it.charAt(1)}" }
Edit, with a pre-filter step
def ent = ['123','133','124','143','125']
def map = ent.findAll { it.charAt(1) in ['2', '3'] }
.groupBy { "Number${it.charAt(1)}" }

Dictionary update overwrites duplicate keys

I have a table that has 6982 records that I am reading through to make a dictionary. I used a literal to create the dictionary
fld_zone_dict = dict()
fields = ['uniqueid', 'FLD_ZONE', 'FLD_ZONE_1']
...
for row in cursor:
uid = row[0]
old_zone_value = row[1]
new_zone_value = row[2]
fld_zone_dict[uid] = [old_zone_value, new_zone_value]
However, I noticed that using this method, if a uid has the same value as a previous uid (theoretically, there could be duplicate), the entry gets overwritten. So, if I had 2 entries I wanted to add: 'CA10376036': ['AE', 'X'] and 'CA10376036': ['V', 'D'], the first one gets overwritten and I only get 'CA10376036': ['V', 'D']. How can I add to my dictionary with out overwriting the duplicate keys so that I get something like this?
fld_zone_dict = {'CA10376036': ['AE', 'X'], 'CA9194089':['D', 'X'],'CA10376036': ['V', 'D']....}
Short answer: There is no way to have duplicate keys in a dictionary object in Python.
However, if you were to restructure your data and take that key and put it inside of a dictionary that is nested in a list, you could have duplicate IDs. EX:
[
{
"id": "CA10376036",
"data: ['AE', 'X']
},
{
"id": "CA10376036",
"data: ['V', 'D']
},
]
Doing this though will negate any benefits of lookup speed and ease.
edit: blhsing also has a good example of how to restructure data with a reduced initial lookup time, though you would still have to iterate through data to get the record you wanted.
Dicts are not allowed to have duplicate keys in Python. You can use the dict.setdefault method to convert existing keys to a list instead:
for row in cursor:
uid = row[0]
old_zone_value = row[1]
new_zone_value = row[2]
fld_zone_dict.setdefault(uid, []).append([old_zone_value, new_zone_value])
so that fld_zone_dict will become like:
{'CA10376036': [['AE', 'X'], ['V', 'D']], 'CA9194089': ['D', 'X'], ...}
but then other keys will not have a list of lists as values, so you probably should convert them all instead:
for k, v in fld_zone_dict.items():
fld_zone_dict[k] = [v]
for row in cursor:
uid = row[0]
old_zone_value = row[1]
new_zone_value = row[2]
fld_zone_dict[uid].append([old_zone_value, new_zone_value])
so that fld_zone_dict will become like:
{'CA10376036': [['AE', 'X'], ['V', 'D']], 'CA9194089': [['D', 'X']], ...}

Indexing a list of dictionaries for a relating value

I have a 4 dictionaries which have been defined into a list
dict1 = {'A':'B'}
dict2 = {'C':'D'}
dict3 = {'E':'F'}
dict4 = {'G':'H'}
list = [dict1, dict2, dict3, dict4]
value = 'D'
print (the relating value to D)
using the list of dictionaries I would like to index it for the relating value of D (which is 'C').
is this possible?
note: the list doesn't have to be used, the program just needs to find the relating value of C by going through the 4 dictionaries in one way or another.
Thanks!
You have a list of dictionaries. A straightforward way would be to loop over the list, and search for desired value using -
dict.iteritems()
which iterates over the dictionary and returns the 'key':'value' pair as a tuple (key,value). So all thats left to do is search for a desired value and return the associated key. Here is a quick code I tried. Also this should work for dictionaries with any number of key value pairs (I hope).
dict1 = {'A':'B'}
dict2 = {'C':'D'}
dict3 = {'E':'F'}
dict4 = {'G':'H'}
list = [dict1, dict2, dict3, dict4]
def find_in_dict(dictx,search_parameter):
for x,y in dictx.iteritems():
if y == search_parameter:
return x
for i in range(list.__len__()):
my_key = find_in_dict(list[i], "D")
print my_key or "No key found"
On a different note, such a usage of dictionaries is little awkward for me, as it defeats the purpose of having a KEY as an index for an associated VALUE. But anyway, its just my opinion and I am not aware of your use case. Hope it helps.

How to encode a list in python 2.7.8 ?

I try to find a duplicate row value in field named "KOD" and then insert those values into "list2" and then print it. The field contain values with numbers and charecters not in English language(right to left language).
I using this code:
import arcpy
fc = r"G:\desktop\Project\lyr\aaa.shp"
list1 = []
list2 = []
counter = print list2
with arcpy.da.UpdateCursor(fc, "KOD") as cursor:name
for row in cursor:
list1.append(row)
if list1.count(row) >= 2 :
list2.append(row)
counter = counter +1
print counter
print list2
this is the result:
>>>
[]
6
[[u'\u05d3/640'], [u'69/100/02/10'], [u'\u05d3/640'], [u'35/100/02/10'], [u'1/195/03/10'], [u'35/100/02/10']]
>>>
i get 6 duplicated values- it's true, but i get also a strange strings names.
How can i decode those strings?
I already asked it in https://gis.stackexchange.com/questions/228104/print-a-duplicate-field-list-values-using-arcpy , but the moderator decided that this question suitable to stackoverflow.

How to sort python lists due to certain criteria

I would like to sort a list or an array using python to achive the following:
Say my initial list is:
example_list = ["retg_1_gertg","fsvs_1_vs","vrtv_2_srtv","srtv_2_bzt","wft_3_btb","tvsrt_3_rtbbrz"]
I would like to get all the elements that have 1 behind the first underscore together in one list and the ones that have 2 together in one list and so on. So the result should be:
sorted_list = [["retg_1_gertg","fsvs_1_vs"],["vrtv_2_srtv","srtv_2_bzt"],["wft_3_btb","tvsrt_3_rtbbrz"]]
My code:
import numpy as np
import string
example_list = ["retg_1_gertg","fsvs_1_vs","vrtv_2_srtv","srtv_2_bzt","wft_3_btb","tvsrt_3_rtbbrz"]
def sort_list(imagelist):
# get number of wafers
waferlist = []
for image in imagelist:
wafer_id = string.split(image,"_")[1]
waferlist.append(wafer_id)
waferlist = set(waferlist)
waferlist = list(waferlist)
number_of_wafers = len(waferlist)
# create list
sorted_list = []
for i in range(number_of_wafers):
sorted_list.append([])
for i in range(number_of_wafers):
wafer_id = waferlist[i]
for image in imagelist:
if string.split(image,"_")[1] == wafer_id:
sorted_list[i].append(image)
return sorted_list
sorted_list = sort_list(example_list)
works but it is really awkward and it involves many for loops that slow down everything if the lists are large.
Is there any more elegant way using numpy or anything?
Help is appreciated. Thanks.
I'm not sure how much more elegant this solution is; it is a bit more efficient. You could first sort the list and then go through and filter into final set of sorted lists:
example_list = ["retg_1_gertg","fsvs_1_vs","vrtv_2_srtv","srtv_2_bzt","wft_3_btb","tvsrt_3_rtbbrz"]
sorted_list = sorted(example_list, key=lambda x: x[x.index('_')+1])
result = [[]]
current_num = sorted_list[0][sorted_list[0].index('_')+1]
index = 0
for i in example_list:
if current_num != i[i.index('_')+1]:
current_num = i[i.index('_')+1]
index += 1
result.append([])
result[index].append(i)
print result
If you can make assumptions about the values after the first underscore character, you could clean it up a bit (for example, if you knew that they would always be sequential numbers starting at 1).