This question already has answers here:
list to dictionary conversion with multiple values per key?
(7 answers)
Closed 2 years ago.
I am trying to add multiple value to a single key(if found) from a file in python. I tried below code but getting this error: AttributeError: 'str' object has no attribute 'append'
file=open("Allwords",'r')
list=sorted(list(set([words.strip() for words in file])))
def sequence(word):
return "".join(sorted(word))
dict= {}
for word in list:
if sequence(word) in dict:
dict[sequence(word)].append(word)
else:
dict[sequence(word)]=word
Thanks in advance for your help!
You should insert the first element by putting it into a list, so that you can append subsequent items to it later on. You can do it as follows -
file=open("Allwords",'r')
list=sorted(list(set([words.strip() for words in file])))
def sequence(word):
return "".join(sorted(word))
dict= {}
for word in list:
if sequence(word) in dict:
dict[sequence(word)].append(word)
else:
new_lst = [word] # Inserting the first element as a list, so we can later append to it
dict[sequence(word)]=new_lst
Now you will be able to append to it properly. In your case, the value you were inserting was just a string to which you wouldn't have been able to append. But this will work, since you are inserting a list at start to which you would be able to append to .
Hope this helps !
Related
This question already has answers here:
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
How to test if a string contains one of the substrings in a list, in pandas?
(4 answers)
Closed 1 year ago.
For example, after filtering the entire dataset to only questions containing the word "King", we could then find all of the unique answers to those questions.
I filtered by using the following code:
`def lower1(x):
x.lower()
filter_dataset = lambda x:all(x) in jeopardy.Question.apply(lower1)
print(filter_dataset(['King','England']))`
The above code is printing True instead of printing the rows of jeopardy['Question'] with the keywords 'King' and 'England'.
That is the first problem.
Now I want to count the unique answers to the jeopardy['Question']
Here is the sample data frame
Now I want to create a function that does the count of the unique answers.
I wrote the following code:
`def unique_counts():
print(jeopardy['Answer'].unique().value_counts())
unique_counts()`
Which is giving me the following error:
AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'
Use Series.str.contains:
jeopardy[jeopardy['Question'].str.contains('|'.join(['King','England']))]
This question already has answers here:
Filtering dict of dict with dictionary comprehension
(2 answers)
Closed 2 years ago.
Input :
t1_dict = {"k1":{"api1":200,"api2":500,"api3":200},"k2":{"api1":500,"api2":200,"api3":500}}
Output:
t2_dict = {'k2': {'api'1: 500,'api3':500}, 'k1': {'api2': 500}}
Need to extract whose values not equal 200 to another dictionary.
I could able to do it, as below.
t2_dict = {}
for k1,v1 in api_status.items():
t2_dict[k1] = {}
for k2, v2 in v1.items():
if v2 != 200:
t2_dict[k1][k2] = v2
Can it be done in better way ? Any one-liner or other best solution ? Any optimisation ?
Alternative one-liner (after the setup):
#Value to exclude
Value=200
t1_dict={"k1":{"api1":200,"api2":500,"api3":200},"k2":{"api1":500,"api2":200,"api3":500}}
t2_dict = {j:{k:v for k,v in t1_dict[j].items() if v != Value} for j in t1_dict}
print(t2_dict)
Credit to the top rated solution found here for the root of the idea, I simply nested it inside another loop: Remove a dictionary key that has a certain value
Hope this helps!
This question already has answers here:
Is there a built in function for string natural sort?
(23 answers)
Closed 3 years ago.
I have a list of strings that I am trying to organize numerically it looks like this :
List=['Core_0_0.txt', 'Core_0_1.txt','Core_0_2.txt',...'Core_1_0.txt','Core_2_3.txt', ]
but when I sort it sorted(List)
It doesn't sort the list properly.
It's very important that I keep the values as strings and they must be ordered by the number; I.E. 0_1, 0_2,0_3....31_1, they all have Core_X_X.txt How would I do this.
If you can assume all your entries will look like *_N1_N2.txt, you can use the str.split method along with a sorting key function to sort your list properly. It might look something like this
sorted_list = sorted(List, key = lambda s: (int(s.split("_")[1]), int(s.split("_")[2].split(".")[0])))
Essentially, this internally creates tuples like (N1, N2) where your file is named *_N1_N2.txt and sorts based on the N1 value. If there's a tie, it will resort to the N2 value.
Your question is a possible duplicate of another question.
Which I am posting for you here again.
you just need to change 'alist' to your 'List'.
import re
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
alist=[
"something1",
"something12",
"something17",
"something2",
"something25",
"something29"]
alist.sort(key=natural_keys)
print(alist)
yields
['something1', 'something2', 'something12', 'something17', 'something25', 'something29']
This question already has answers here:
Python extract pattern matches
(10 answers)
Closed 3 years ago.
I'm pulling some data out of the web utilizing python in the Jupyter notebook. I have pulled down the data, parsed, and created the data frame. I need to extract a number out of a string that I have in the data frame. I utilizing this regex to do it:
for note in df["person_notes"]:
print(re.search(r'\d+', note))
and the outcome is the following:
<_sre.SRE_Match object; span=(53, 55), match='89'>
How can I get just the match number; in this line would be 89. I tried to convert the whole line to str() and the replace(), but not all lines have the span=(number, number) iqual. Thank you in advance!
You can use the start() and end() methods on the returned match objects to get the correct positions within the string:
for note in df["person_notes"]:
match = re.search(r'\d+', note)
if match:
print(note[match.start():match.end()])
else:
# no match found ...
total=0
line=input()
line = line.upper()
names = {}
(tag,text) = parseLine(line) #initialize
while tag !="</PLAY>": #test
if tag =='<SPEAKER>':
if text not in names:
names.update({text})
I seem to get this far and then draw a blank.. This is what I'm trying to figure out. When I run it, I get:
ValueError: dictionary update sequence element #0 has length 8; 2 is required
Make an empty dictionary
Which I did.
(its keys will be the names of speakers and its values will be how many times s/he spoke)
Within the if statement that checks whether a tag is <SPEAKER>
If the speaker is not in the dictionary, add him to the dictionary with a value of 1
I'm pretty sure I did this right.
If he already is in the dictionary, increment his value
I'm not sure.
You are close, the big issue is on this line:
names.update({text})
You are trying to make a dictionary entry from a string using {text}, python is trying to be helpful and convert the iterable inside the curly brackets into a dictionary entry. Except the string is too long, 8 characters instead of two.
To add a new entry do this instead:
names.update({text:1})
This will set the initial value.
Now, it seems like this is homework, but you've put in a bit of effort already, so while I won't answer the question I'll give you some broad pointers.
Next step is checking if a value already exists in the dictionary. Python dictionaries have a get method that will retrieve a value from the dictionary based on the key. For example:
> names = {'romeo',1}
> print names.get('romeo')
1
But will return None if the key doesn't exist:
> names = {'romeo',1}
> print names.get('juliet')
None
But this takes an optional argument, that returns a different default value
> names = {'romeo',2}
> print names.get('juliet',1)
1
Also note that your loop as it stands will never end, as you only set tag once:
(tag,text) = parseLine(line) #initialize
while tag !="</PLAY>": #test
# you need to set tag in here
# and have an escape clause if you run out of file
The rest is left as an exercise for the reader...