.get() returning letters rather than strings - python

I have a list of dictionaries that is structured as such:
json_data = [{'a':10,'text':"Salam"},{'a':4,'text':"Hello Friend"}]
I have been able to iterate through the list and extract the key 'text' from each dictionary:
json1_text = [[[value] for value in json1_data[index].get('text')] for
index in range(len(json1_data))]
However, the new json1_text list does not contain sentences from returned from the dictionary, but rather each individual letter:
json1_text[0]
Returns:
[['S'],['a'],['l'],['a'],['m']]
How would I be able to return the whole sentence "Hello Friend" as opposed to each individual letter and storing each in a list?
Thanks in advance!

json1_text = [v for i in json_data for k,v in i.items() if isinstance(v,str)]
print (json1_text)
Result:
['Salam', 'Hello Friend']

Related

Print certain amount of tuples in a list without name

so my exercise was to print 10 most common words in a text file.
Assuming I opened the file and created a dictionary that contains seperated words with indexes.
Normally, I do this:
li=list()
for key,value in d.items():
tpl=(value,key)
li.append(tpl)
li=sorted(li,reverse=True)
for key,value in li[:10]:
print('Ten most common words: ',value,key)
But prof gave me a single line of code that can replace almost all those lines:
print(sorted([(value,key) for key,value in d.items()],reverse=True))
However I can't find a way to print only 10 tuples since the list has no name, I can't use the for loop to print. Can you help me out?
Separate the list creation from the print():
li = sorted([(value, key) for key, value in d.items()], reverse=True)
Now you can iterate through li.
for item in li[:10]:
print(item)

How to modify each element in a list without creating a new list

I want to modify all elements in a list such that I delete all characters after certain specific characters.
list is ['JACK\NAME1','TOM\NAME2'] and I want to modify it into ['JACK', 'TOM']
Right now I am using a For Loop with Split command:
text = ['JACK\\NAME1','TOM\\NAME2']
text_use = []
for item in text:
item = item.split('\\',1)[0]
text_use.append(item)
text_use
But I also have to create a new empty list (text_use) and append items to it.
Is there a better way to do this? Where I don't have to use a For Loop?
Or where I don't have to create an empty list and then append items to it?
Thank you
R
In my opinion, it's more idiomatic (pythonic) to use enumerate:
for i, item in enumerate(text):
text[i] = item.split('\\',1)[0]
like this
text = ['JACK\\NAME1','TOM\\NAME2']
text = [x.split('\\',1)[0] for x in text]
Iterate over the list positions instead of the list values, and then access the items by their position in the list:
for pos in range(len(text)):
text[pos] = text[pos].split('\\',1)[0]

How to retrieve key value pair within listed dictionary in python?

I have a list of dictionaries as a key value pairs, where I want to access the data of each dict by key:
sample data:
['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"',...
real data
>>> data
['"imageUrl":"/images/products/klein/04047122_k.jpg"', '"art":"04047122"', '"productId":"170336"'; } } }) ']
This unfortunatelly does not work:
re.findall(r'(?:number\(\{)(.*)', data)[0].split(',')
How can I retrieve the values by name e.g. data['number'] ?
For a more robust solution, since each string in the input list is a valid line of CSV, delimited by a colon, you can use csv.reader to parse the list and then pass the resulting sequence of key-value pairs to the dict constructor to build a dict:
import csv
lst = ['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"']
data = dict(csv.reader(lst, delimiter=':'))
You can then access data['number'] as desired.
Try to convert your data to a real dictionary:
data = ['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"']
data_dict = dict([x.replace('"','').split(":") for x in data])
and then you will be able to access your keys:
print(data_dict["number"]) # output: 04047122
You can convert your string list to an actual dictionary easily:
>>> ls = ['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"']
>>> data = dict(elem.replace('"', '').split(':') for elem in ls)
>>> data
{'imageUrl': '/images/4.jpg', 'number': '04047122', 'name': 'test'}
>>> data['number']
'04047122'

how to convert a Dictionary with multiple values for a single key in multiple rows to single row with multiple values to a key

Below is my requirement. Below is the data that is present in json file:
{"[a]":" text1","[b]":" text2","[a]":" text3","[c]":" text4","[c]":" Text5"}.
The final output should be like
{"[a]":[" text1","text3"],"[b]":" text2","[c]":" text4"," Text5"]}.
I tried below code:
data_in= ["[a]"," text1","[b]"," text2","[a]"," text3","[c]"," text4","[c]"," text5"]
data_pairs = zip(data_in[::2],data_in[1::2])
data_dict = {}
for x in data_pairs:
data_dict.setdefault(x[0],[]).append(x[1])
print data_dict
But the input it takes is more in form of List than a dictionary.
Please advise.
Or is there a way where i can convert my original dictionary into list with multiple values as list will take only unique values. Please let me know the code also i am very new to Python and still learning it. TIA.
Keys are unique within a dictionary while values may not be.
You can try
>>> l = ["[a]"," text1","[b]"," text2","[a]"," text3","[c]"," text4","[c]"," text5"]
>>> dict_data = {}
>>> for i in range(0,len(l),2):
if dict_data.has_key(l[i]):
continue
else:
dict_data[l[i]] = []
>>> for i in range(1,len(l),2):
dict_data[l[i-1]].append(l[i])
>>> print dict_data
{'[c]': [' text4', ' text5'], '[a]': [' text1', ' text3'], '[b]': [' text2']}

Python: create dict from list and auto-gen/increment the keys (list is the actual key values)?

i've searched pretty hard and cant find a question that exactly pertains to what i want to..
I have a file called "words" that has about 1000 lines of random A-Z sorted words...
10th
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
a
AAA
AAAS
Aarhus
Aaron
AAU
ABA
Ababa
aback
abacus
abalone
abandon
abase
abash
abate
abater
abbas
abbe
abbey
abbot
Abbott
abbreviate
abc
abdicate
abdomen
abdominal
abduct
Abe
abed
Abel
Abelian
I am trying to load this file into a dictionary, where using the word are the key values and the keys are actually auto-gen/auto-incremented for each word
e.g {0:10th, 1:1st, 2:2nd} ...etc..etc...
below is the code i've hobbled together so far, it seems to sort of works but its only showing me the last entry in the file as the only dict pair element
f3data = open('words')
mydict = {}
for line in f3data:
print line.strip()
cmyline = line.split()
key = +1
mydict [key] = cmyline
print mydict
key = +1
+1 is the same thing as 1. I assume you meant key += 1. I also can't see a reason why you'd split each line when there's only one item per line.
However, there's really no reason to do the looping yourself.
with open('words') as f3data:
mydict = dict(enumerate(line.strip() for line in f3data))
dict(enumerate(x.rstrip() for x in f3data))
But your error is key += 1.
f3data = open('words')
print f3data.readlines()
The use of zero-based numeric keys in a dict is very suspicious. Consider whether a simple list would suffice.
Here is an example using a list comprehension:
>>> mylist = [word.strip() for word in open('/usr/share/dict/words')]
>>> mylist[1]
'A'
>>> mylist[10]
"Aaron's"
>>> mylist[100]
"Addie's"
>>> mylist[1000]
"Armand's"
>>> mylist[10000]
"Loyd's"
I use str.strip() to remove whitespace and newlines, which are present in /usr/share/dict/words. This may not be necessary with your data.
However, if you really need a dictionary, Python's enumerate() built-in function is your friend here, and you can pass the output directly into the dict() function to create it:
>>> mydict = dict(enumerate(word.strip() for word in open('/usr/share/dict/words')))
>>> mydict[1]
'A'
>>> mydict[10]
"Aaron's"
>>> mydict[100]
"Addie's"
>>> mydict[1000]
"Armand's"
>>> mydict[10000]
"Loyd's"
With keys that dense, you don't want a dict, you want a list.
with open('words') as fp:
data = map(str.strip, fp.readlines())
But if you really can't live without a dict:
with open('words') as fp:
data = dict(enumerate(X.strip() for X in fp))
{index: x.strip() for index, x in enumerate(open('filename.txt'))}
This code uses a dictionary comprehension and the enumerate built-in, which takes an input sequence (in this case, the file object, which yields each line when iterated through) and returns an index along with the item. Then, a dictionary is built up with the index and text.
One question: why not just use a list if all of your keys are integers?
Finally, your original code should be
f3data = open('words')
mydict = {}
for index, line in enumerate(f3data):
cmyline = line.strip()
mydict[index] = cmyline
print mydict
Putting the words in a dict makes no sense. If you're using numbers as keys you should be using a list.
from __future__ import with_statement
with open('words.txt', 'r') as f:
lines = f.readlines()
words = {}
for n, line in enumerate(lines):
words[n] = line.strip()
print words

Categories

Resources