Can Python set these variables at once? - python

I have these initializations:
countrys = []
codes = []
index_countrys = {}
index_codes = {}
I want to declare them at once like this:
countrys,codes = []
index_countrys,index_codes = {}
Is that allowed in Python?

You can use:
countries, codes = [], []
index_countries, index_codes = {}, {}
or even :
countries, codes, index_countries, index_codes = [], [], {}, {}
This is a good way to set multiple variables to distinct values using only a single line of code.
Searching “python set multiple variables site:stackoverflow.com” will give you many other options and recommendations on which ones to use.

countrys, code = [], []
index_countrys, index_codes = {}, {}

Yes, you can!
On python you can declare chained vars with equals sign.
Just like that:
countrys = codes = []
index_countrys = index_codes = {}

Related

Sort List into different lists

I have list with file_names in it.
(About 800 file_names)
[Example] file_name = 23475048_43241u_43x_pos11_7.npz
I need to sort the file_names and add it to lists. The file_names get sorted with the "pos". In my example is that pos11. (there are different pos -> pos0, pos12...)
I tried firstly to get all different pos_numbers in a Dict:
path =[filename for filename in glob.glob(os.path.join(my_dir, '*.npz'))]
posList = []
for file in path:
file_name = Path(file).parts[-1][:-4].split("_")
posList.append(file_name[3])
mylist = list(dict.fromkeys(posList))
files_dict = {}
for pos in mylist:files_dict[pos] = []
Output:
{'pos0': [], 'pos10': [], 'pos11': [], 'pos12': [], 'pos1': [], 'pos2': [], 'pos3': [], 'pos4': [], 'pos5': [], 'pos6': [], 'pos7': [], 'pos8': [], 'pos9': []}
And now I want to fill the different lists. But now I'm stuck. I want to to iter again over the list with file_names and add them to right list.
Not sure what your code is doing but you can use the below program which takes in list of file names and outputs a dictionary of sorted lists indexed by the pos which is what I think you are trying to do. (If not maybe edit your question to elaborate some more)
files = ['1_2_3_pos1_2.np', '2_3_1_pos2_2.npz']
files_dict = {}
for file in files:
pos = file.split('_')[3]
files_dict[pos] = files_dict.get(pos, []) + [file]
for k in files_dict.keys():
files_dict[k].sort()
print(files_dict)
Edit:
As #Stef suggested you can make it more effecient by using setdefault
files = ['1_2_3_pos1_2.np', '2_3_1_pos2_2.npz']
files_dict = {}
for file in files:
pos = file.split('_')[3]
files_dict.setdefault(pos, []).append(file)
for k in files_dict.keys():
files_dict[k].sort()
print(files_dict)
#ARandomDeveloper's answer clearly explains how to populate the dict by iterating through the list only once. I recommend to study their answer until you've understood it well.
This is a very common way to populate a dict. You will probably encounter this pattern again.
Because this operation of grouping into a dict is so common, module more_itertools offers a function map_reduce for exactly this purpose.
from more_itertools import map_reduce
posList = '''23475048_43241u_43x_pos11_7.npz
23475048_43241u_43x_pos1_7.npz
23475048_43241u_43x_pos10_7.npz
23475048_43241u_43x_pos8_7.npz
23475048_43241u_43x_pos22_7.npz
23475048_43241u_43x_pos2_7.npz'''.split("\n") # example list from uingtea's answer
d = map_reduce(posList, keyfunc=lambda f: f.split('_')[3])
print(d)
# defaultdict(None, {
# 'pos11': ['23475048_43241u_43x_pos11_7.npz'],
# 'pos1': ['23475048_43241u_43x_pos1_7.npz'],
# 'pos10': ['23475048_43241u_43x_pos10_7.npz'],
# 'pos8': ['23475048_43241u_43x_pos8_7.npz'],
# 'pos22': ['23475048_43241u_43x_pos22_7.npz'],
# 'pos2': ['23475048_43241u_43x_pos2_7.npz']
# })
Internally, map_reduce uses almost-exactly the same code as suggested in #ARandomDeveloper's answer, except with a defaultdict.
you need to extract the digits after pos use regex (\d+)_\d\.npz then use .sort() function
import re
posList = '''23475048_43241u_43x_pos11_7.npz
23475048_43241u_43x_pos1_7.npz
23475048_43241u_43x_pos10_7.npz
23475048_43241u_43x_pos8_7.npz
23475048_43241u_43x_pos22_7.npz
23475048_43241u_43x_pos2_7.npz'''.split("\n")
posList = sorted(posList, key=lambda x: int(re.search(r"(\d+)_\d\.npz", x)[1]))
print(posList)
results
['23475048_43241u_43x_pos1_7.npz',
'23475048_43241u_43x_pos2_7.npz',
'23475048_43241u_43x_pos8_7.npz',
'23475048_43241u_43x_pos10_7.npz',
'23475048_43241u_43x_pos11_7.npz',
'23475048_43241u_43x_pos22_7.npz'
]

defining multiple variables to an empty list in a loop

I am trying to create and assign 10 variables, only differenciated by their index, all as empty lists within a for loop.
The ideal output would be to have agent_1 = [], agent_2 = [], agent_n = []
I know I could write this all out but thought I should be able to create a simple loop. The main issue is assigning the empty list over each iteration
for i in range(1,10):
agent_ + i = []
Why don't you use dict object with keys equal to agent_i.
dic = {}
for i in range(1,10):
dic["agent_" + str(i)] = []
// access the dic directly and iterating purpose also just iterate through the dictionary.
print dic["agent_1"]
# iteration over the dictionary
for key,value in dic.items():
print key,value
Here is the link to code snippet
This is a horrible idea. I will let the code speak for itself:
n = 10
for i in range(n):
globals()['agent_%d' % (i + 1)] = []
a = {}
for i in xrange(10):
ab = "{}_{}".format("agent", i)
a[ab] = []
print a
#OP
{'agent_0': [], 'agent_1': [], 'agent_2': [], 'agent_3': [], 'agent_4': [], 'agent_5': [], 'agent_6': [], 'agent_7': [], 'agent_8': [], 'agent_9': []}

How do I assign multiple values taken from user to a single key stored in a dictionary in python?

I am trying to create a number of lists that store a number of values. Each list is mentioned as class in my code. I want to add values for each key, one by one, in the dictionary that is taken from a user.
This is my code so far:
n = input('Enter the number of classe: ')
class_count = int(n)
listDict = {}
for i in range(1, class_count):
listDict["class_" + str(i)] = []
print(listDict)
Output:
Enter the number of classe: 4
{'class_1': []}
{'class_1': [], 'class_2': []}
{'class_1': [], 'class_2': [], 'class_3': []}
You need to iterate to class_count+1 since even though you start a 1, it doesn't mean the range function will iterate one additional time, it'll actually simply iterate one less time.
Also you should probably use an OrderedDict in order to retain order of the classes:
from collections import OrderedDict
listDict = OrderedDict()
class_count = 4
for i in range(1,class_count+1):
listDict["class_"+str(i)] = []
print listDict
>>>OrderedDict([('class_1', []), ('class_2', []), ('class_3', []), ('class_4', [])])
You should take a look at python's defaultdict construct:
from collections import defaultdict
listDict = defaultdict(list)
And then, just write
listDict['class_{i}'.format(i=0)].append(value)
No need to initialize listDict['class_{i}'.format(i=0)] to be []
You could create a userid for each user, use that as the key and then assign each class value to a list for that user such as the following:
def generateuserid():
"""
generate user id for each user input
:return: userid
"""
for i in range(100):
yield 'user{}'.format(i)
# initiate generator
users = generateuserid()
n=input('Enter the number of classe: ')
listDict = {}
# for each user, retrieve next id
user = next(users)
for i in range(1,int(n)):
if user in listDict.keys():
listDict[user].append('class_{}'.format(i))
else:
listDict[user] = ['class_{}'.format(i)]
print(listDict) #{'user1': ['class_1', 'class_2', 'class_3', 'class_4', 'class_5', 'class_6', 'class_7', 'class_8', 'class_9']}

Create a list dynamically and store all the values matching with current value in python 3.x

I have a text file which has data created dynamically like
1000L 00V
2000L -10V
3500L -15V
1250L -05V
1000L -05V
2000L -05V
6000L -10V
1010L 00V
and so on...
The numbers before V could vary from -160 to +160
I want to create a list (not using dictionary) dynamically and store the values in a list according to the matching numbers before V
In this case I want to create sets of list as follows
00 = ["1000", "1010"]
-10 = ["2000", "6000"]
-15 = ["3500"]
-05 = ["1250", "1000", "2000"]
Tried code:
if name.split()[1] != "":
gain_value = name.split()[1]
gain_value = int(gain_value.replace("V", ""))
if gain_value not in gain_list:
gain_list.append(gain_value)
gain_length = len(gain_list)
print(gain_length)
g['gain_{0}'.format(gain_length)] = []
'gain_{0}'.format(gain_length).append(L_value)
else:
index_value = gain_list.index(gain_value)
g[index_value].append(L_value)
for x in range(0, len(gain_list)):
print(str(gain_list[x]) + "=" + 'gain_{0}'.format(x))
But the above code doesn't work as I get an error while appending 'gain_{0}'.format(gain_length).append(L_value) and I am unsure how to print the list dynamically after its created as mentioned in my required output.
I can't use dictionary for the above method because I want to give the lists dynamically as input to pygal module as below:
as I need the output for pygal module as input like :
for x in range(0, gain_length):
bar_chart.x_labels = k_list
bar_chart.add(str(gain_length[x]),'gain_{0}'.format(x))
Here I can add the values only from a list not from a dictionary
you can use collections.defaultdict:
import collections
my_dict = collection.defaultdict(list)
with open('your_file') as f:
for x in f:
x = x.strip().split()
my_dict[x[1][:-1]].append(x[0])
output:
defaultdict(<type 'list'>, { '00': ["1000", "1010"],
'-10':["2000", "6000"],
'-15': ["3500"],
'-05': ["1250", "1000", "2000"]})
for your desired output:
for x,y in my_dict.items():
print "{} = {}".format(x,y)

iterating over a list of dictionary

I am using pyton 3.2
I have the following dictionary:
a = {"fruits":["apple","mangoes","grapes"],"colour":["apple","orange","grapes"],"number":["1","2","3"]}
I need to iterate over this list of dictionary and I want to create a new dictionary b in which values in fruits is the same value in colours:
b = {"fruits":["apple","grapes"],"colour":["apple", "grapes"],"number":["1","3"]}
i was thinking this could work but am lost from then on:
b = {}
for item in a:
if x in a[item]:
...... dont know what to do now?
How can i do this without using itertools at all?
Is there a general function that i can use for any dictionary besides the one that i have listed?
Something like this?
a = {"fruits":["apple","mangoes","grapes"],"colour":["apple","orange","grapes"]}
b = {}
b['fruits'], b['colour'] = [], []
for fruit in a['fruits']:
if fruit in a['colour']:
b['fruits'].append(fruit)
b['colour'].append(fruit)
If you dont want numbers in the result dictionary, you can do it this way...
Code:
a = {"fruits":["apple","mangoes","grapes"],"colour":["apple","orange","grapes"],"number":["1","2","3"]}
b = dict()
b['fruits'] = b['colour'] = [x for x in a['fruits'] if x in a['colour']]
print b
Output:
{'colour': ['apple', 'grapes'], 'fruits': ['apple', 'grapes']}
Hope this helps :)
In this case, you're better off using a set:
b = {"fruits": list(set(a['fruits']).intersection(a['colour'])}
b['colour'] = b['fruits'] # or maybe make a copy here...
b = {}
for fruit in a['fruits']:
if fruit in a['colour']:
b.setdefault('fruits', []) # set if key is not present
b.setdefault('colour', [])
b.setdefault('number', [])
b['fruits'].append(fruit)
b['colour'].append(fruit)
b['number'].append(a['number'][a['fruits'].index(fruit)])

Categories