I get how to do regular comprehension style conversions for For loops, but this, I can't wrap my head around.
I know that normally you would change the lines 3-5 into one line, but is it possible to include the empty list in the comprehension?
The code is
text = input().lower()
dict = {}
for x in text:
if x.isalpha()
dict[x] = text.count(x)
print(dict)
I don't even know where to get started.
Don't use dict as a variable name, it's the name of a built-in function.
To convert the loop to a dictionary comprehension, change dict[key] = value to key: value, then follow it with the for loop and if condition.
text_dict = {x: text.count(x) for x in text if x.isalpha}
Note that Python has a standard library function collections.Counter() that does this.
from collections import Counter
text_dict = Counter(filter(str.isapha, text))
By referring to the dictionary comprehension posted by #Barmar, another way to do so is using dictionary comprehension and set:
result = dict((x,text.count(x)) for x in set(text.lower()) if x.isalpha)
Related
I have the following list of values: Numbers = [1,2,3,4].
Is it possible to create a dictionary with the same name as the values contained in the list?
Example: dictionary_1 = {}
dictionary_2 = {}
....
dictionary_Number.. {}
I would like to create these dictionaries automatically, without creating them manually, reading the numbers contained in the list
You may use the keyword exec in python. Here is an example of your solution,
List = [1, 2,3]
for ele in List:
dic = f"Dictionary_{ele}"
exec(dic+" = {}")
print(Dictionary_1, Dictionary_2, Dictionary_3, sep='\n')
you may use it according to you, but the disadvantage for it is that you will need to use exec every time you need to use it or you must know what would be the name outcome of the first use of exec ...
I hope I helped...
Use the inbuild functions and remember that a dictionary needs a tuble (key & value):
Python Dictionaries
Python Dictionary fromkeys() Method
Example-Code:
Numbers = [1,2,3,4]
Numbers_dict = dict.fromkeys(Numbers,"dict_value")
print(Numbers_dict)
This will output:
{'1': 'dict_value', '2': 'dict_value', '3': 'dict_value', '4': 'dict_value'}
If you want to get a single dictonaries for each list-value, you first have to create for each list-value an empty variable.
After this you have to fill this empty vairables within a loop.
I'm trying to make a function that takes in list of strings as an input like the one listed below:
def swap_values_dict(['Summons: Bahamut, Shiva, Chocomog',
'Enemies: Bahamut, Shiva, Cactaur'])
and creates a dictionary from them using the words after the colons as keys and the words before the colons as values. I need to clarify that, at this point, there are only two strings in the list. I plan to split the strings into sublists and, from there, try and assign them to a dictionary.
The output should look like
{'Bahamut': ['Summons','Enemies'],'Shiva':['Summons','Enemies'],'Chocomog':['Summons'],'Cactaur':['Enemies']}
As you can see, the words after the colon in the original list have become keys while the words before the colon (categories) have become the values. If one of the values appears in both lists, it is assigned two values in the final dictionary. I would like to be able to make similar dictionaries out of many lists of different sizes, not just ones that contain two strings. Could this be done without list comprehension and only for loops and if statements?
What I've Tried So Far
title_list = []
for i in range(len(mobs)):#counts amount of strings in list
titles = (mobs[i].split(":"))[0] #gets titles from list using split
title_list.append(titles)
title_list
this code returns ['Summons', 'Enemies'] which aren't the results I wanted to receive but I think they could help me write the function. I had planned on separating the keys and values into separate lists and then zipping them together afterwards as a dictionary.
Try:
def swap_values_dict(lst):
tmp = {}
for s in lst:
k, v = map(str.strip, s.split(":"))
tmp[k] = list(map(str.strip, v.split(",")))
out = {}
for k, v in tmp.items():
for i in v:
out.setdefault(i, []).append(k)
return out
print(
swap_values_dict(
[
"Summons: Bahamut, Shiva, Chocomog",
"Enemies: Bahamut, Shiva, Cactaur",
]
)
)
Prints:
{
"Bahamut": ["Summons", "Enemies"],
"Shiva": ["Summons", "Enemies"],
"Chocomog": ["Summons"],
"Cactaur": ["Enemies"],
}
I'd use a defaultdict. It saves you the trouble of manually checking if a key exists in your dictionary and constructing a new empty list, making for a rather concise function:
from collections import defaultdict
def swap_values_dict(mobs):
result = defaultdict(list)
for elem in mobs:
role, members = elem.split(': ')
for m in members.split(', '):
result[m].append(role)
return result
I would like to optimize this piece of code. I'm sure there is a way to write it in a single line:
if 'value' in dictionary:
x = paas_server['support']
else:
x = []
use dictionary get() method as:
x = dictionary.get('support', [])
if support is not a key in the dictionary, it returns second method's argument, here, an empty list.
Trying to make a function that returns a list of values from the dictionary. If the plants are watered weekly, it would be appended into the list then later returned sorted. However, my code iterates each letter of 'weekly' instead of the whole string and I have no idea how to access the watering frequency of the dictionary items. Any explanations would be appreciated.
def weekly(plants_d):
d = []
for plant in plants_d:
for plan in plants_d[plant]:
if plan == "weekly":
d.append[plan]
return sort(d)
weekly({'fern':'weekly', 'shamrock':'weekly', 'carnation':'weekly'})
# Should return like this: ['carnation','fern','shamrock']
Amending the previous answer so that only values with "weekly" are used:
>>> my_dict = {'fern':'weekly', 'shamrock':'weekly', 'carnation':'weekly', 'daffodil': 'monthly'}
>>> sorted(k for k, v in my_dict.items() if v == 'weekly')
['carnation', 'fern', 'shamrock']
This line:
for plan in plants_d[plant]:
is wrong. Since plants_d[plant] is a string like "weekly", this is like
for plan in "weekly":
which will iterate over the letters in the string. Then when you do if plan == "weekly": it will never match, because plan is just a single letter like "w".
You can simply use:
if plants_d[plan] == "weekly":
Or you can change the first loop to:
for plan_name, plan_frequency in plants_d.items():
if plan_frequency == "weekly":
d.append[plan_name]
See Iterating over dictionaries using 'for' loops
Simplified way to achieve this is using dict.keys() which return the list of all the keys in dict. In order to sort the list, you may use sorted() as:
>>> my_dict = {'fern':'weekly', 'shamrock':'weekly', 'carnation':'weekly'}
>>> sorted(my_dict.keys())
['carnation', 'fern', 'shamrock']
Edit: If some the plans are monthly, firstly filter the monthly plans using filter or dict comprehension. Your code should be like:
>>> my_dict = {'fern':'weekly', 'shamrock':'weekly', 'carnation':'weekly',
'something': 'monthly'}
# using filter() as #brianpck has already mentioned 'dict comprehension' approach
# It is better to use brian's approach
>>> filtered_dict = dict(filter(lambda x: x[1] == 'weekly', my_dict.items()))
>>> sorted(filtered_dict.keys())
['carnation', 'fern', 'shamrock']
I was just wondering if there is a simple way to do this. I have a particular structure that is parsed from a file and the output is a list of a dict of a list of a dict. Currently, I just have a bit of code that looks something like this:
for i in xrange(len(data)):
for j, k in data[i].iteritems():
for l in xrange(len(data[i]['data'])):
for m, n in data[i]['data'][l].iteritems():
dostuff()
I just wanted to know if there was a function that would traverse a structure and internally figure out whether each entry was a list or a dict and if it is a dict, traverse into that dict and so on. I've only been using Python for about a month or so, so I am by no means an expert or even an intermediate user of the language. Thanks in advance for the answers.
EDIT: Even if it's possible to simplify my code at all, it would help.
You never need to iterate through xrange(len(data)). You iterate either through data (for a list) or data.items() (or values()) (for a dict).
Your code should look like this:
for elem in data:
for val in elem.itervalues():
for item in val['data']:
which is quite a bit shorter.
Will, if you're looking to decend an arbitrary structure of array/hash thingies then you can create a function to do that based on the type() function.
def traverse_it(it):
if (isinstance(it, list)):
for item in it:
traverse_it(item)
elif (isinstance(it, dict)):
for key in it.keys():
traverse_it(it[key])
else:
do_something_with_real_value(it)
Note that the average object oriented guru will tell you not to do this, and instead create a class tree where one is based on an array, another on a dict and then have a single function to process each with the same function name (ie, a virtual function) and to call that within each class function. IE, if/else trees based on types are "bad". Functions that can be called on an object to deal with its contents in its own way "good".
I think this is what you're trying to do. There is no need to use xrange() to pull out the index from the list since for iterates over each value of the list. In my example below d1 is therefore a reference to the current data[i].
for d1 in data: # iterate over outer list, d1 is a dictionary
for x in d1: # iterate over keys in d1 (the x var is unused)
for d2 in d1['data']: # iterate over the list
# iterate over (key,value) pairs in inner most dict
for k,v in d2.iteritems():
dostuff()
You're also using the name l twice (intentionally or not), but beware of how the scoping works.
well, question is quite old. however, out of my curiosity, I would like to respond to your question for much better answer which I just tried.
Suppose, dictionary looks like: dict1 = { 'a':5,'b': [1,2,{'a':100,'b':100}], 'dict 2' : {'a':3,'b':5}}
Solution:
dict1 = { 'a':5,'b': [1,2,{'a':100,'b':100}], 'dict 2' : {'a':3,'b':5}}
def recurse(dict):
if type(dict) == type({}):
for key in dict:
recurse(dict[key])
elif type(dict) == type([]):
for element in dict:
if type(element) == type({}):
recurse(element)
else:
print element
else:
print dict
recurse(dict1)