Illinois: ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3']
Indiana: ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']
Those are in my dictionary as d.
How would I get the largest and smallest value in each key in the dictionary and get the index where's the value is.
For example:
In Illinois, 26 is the largest value which is index 5 and 3 is the smallest value which is index 15.
in Indiana: 13 is largest value which is index 7 and 2 is the smallest value which is index 14
The output:
Illinois: 26 in index 5 and 3 in index 15
Indiana: 13 in index 7 and 2 in index 14
How would I do this?
d = {}
for row in csv_f:
d[row[0]]=row[1:]
You can get the max and mins printed out as your string is like this:
(assuming you only want the first occurrence)
MY_D = {'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'],
'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']}
for k,v in MY_D.items():
#This assumes that everything in v is an int, or rather can be converted to one.
my_l = [int(n) for n in v]
#if not
#my_l = [int(n) for n in v if n.isdigit()]
_max, _min = max(my_l), min(my_l)
print("%s: Min - %d in index %d, Max - %d in index %d" % (k, _min, my_l.index(_min), _max, my_l.index(_max)))
Here is a solution returning a dict {country: (maxval, index), (minval, index))}:
d = {
'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'],
'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']
}
maxmin = {}
for state, numbers in d.items():
maxmin[state] = (
max(enumerate(numbers), key=lambda x: int(x[1])),
min(enumerate(numbers), key=lambda x: int(x[1]))
)
print(maxmin)
Bit thrown together, but seems to do the job.
d = {"Illinois": ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'],
"Indiana": ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']}
if __name__ == "__main__":
print d
for state in d:
# returns the numbers with their index (#, index)
pairs = [(int(d[state][x]), x) for x in xrange(len(d[state]))]
minpair = min(pairs)
maxpair = max(pairs)
print "%s: %d in index %d and %d in index %d"%(state,maxpair[0],maxpair[1],
minpair[0],minpair[1])
Output:
{'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2'], 'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3']}
Indiana: 13 in index 6 and 2 in index 13
Illinois: 26 in index 4 and 3 in index 14
to get around the blank string, you could break up the list comprehension into
pairs = []
for x in xrange(len(d[state])):
try:
pairs.append( (int(d[state][x]), x) )
except ValueError:
pass # not a valid number
Related
For who's interested in; lucky numbers are generated by eliminating numbers based on their position in the set. i.e:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
First eliminates every second number bc second value is 2:
1,3,5,7,9,11,13,15,17,19,21
First remaining number in the list after 1 is 3; so it eliminates every third number:
1,3,7,9,13,15,17,19,21
Next number is 7; eliminate every 7th number in the list:
1,3,7,9,13,15,21
Next surviving nunmber after 7 is 9 but obviously there are not enough numbers to eliminate.
For further information you can check Lucky Number
So, if my list doesn't contain any negative number and begins with 1 i.e:
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22']
My code supposed to delete every number unless it's a lucky number, so I tried:
def lucky_numbers(numbers):
del numbers[::-2] # Delete even numbers
while int(numbers[1]) < len(numbers):
x = int(numbers[1])
del numbers[-1::x]
print(numbers)
return
return
lucky_numbers(numbers)
But it returns:
['1', '3', '5', '7', '9', '11', '13', '15', '17', '19']
Where am I wrong? Or is there any efficient way to write it? Thank you.
The negative index is a bit confusing to me (at least), see if this code is easy to interpret-
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22']
def lucky_numbers(numbers):
index = 1
next_freq = int(numbers[index])
while int(next_freq) < len(numbers):
del numbers[next_freq-1::next_freq]
print(numbers)
if str(next_freq) in numbers:
index += 1
next_freq = int(numbers[index])
else:
next_freq = int(numbers[index])
return
lucky_numbers(numbers)
['1', '3', '5', '7', '9', '11', '13', '15', '17', '19', '21']
['1', '3', '7', '9', '13', '15', '19', '21']
['1', '3', '7', '9', '13', '15', '21']
i am extremely new to python. i have googled but i haven't found what i need for hours.
i'm very confused on why my program is like this and would very much appreciate pointers. so this is my program:
number = int(input("Enter number: "))
mylist = []
if number > 1:
for num in range(1,number + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
mylist.append(str(num))
print(mylist)
else:
print("no prime number")
so e.g if i insert 50 i wanted it to have an output of something like:
['2', '3', '5', '7', '11', '13', '17', '19',
'23', '29', '31', '37', '41', '43', '47']
but instead what i got was this chaos
['2']
['2', '3']
['2', '3', '5']
['2', '3', '5', '7']
['2', '3', '5', '7', '11']
['2', '3', '5', '7', '11', '13']
['2', '3', '5', '7', '11', '13', '17']
['2', '3', '5', '7', '11', '13', '17', '19']
['2', '3', '5', '7', '11', '13', '17', '19', '23']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41', '43']
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41', '43', '47']
Your print statement is indented incorrectly:
number = int(input("Enter number: "))
mylist = []
if number > 1:
for num in range(1,number + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
mylist.append(str(num))
print(mylist)
else:
print("no prime number")
You can group them into sublists of 8 for the purpose of printing. Just make your mylist as usual by appending the values then after you have processed all the values you can print sublists of 8
number = int(input("Enter number: "))
mylist = []
if number > 1:
for num in range(1,number + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
mylist.append(str(num))
print(*[mylist[i:i+8] for i in range(0, len(mylist), 8)], sep="\n")
else:
print("no prime number")
OUTPUT
['2', '3', '5', '7', '11', '13', '17', '19']
['23', '29', '31', '37', '41', '43', '47']
After reading from a file I have a list of lists contaning not only digits but also other characters, which I would like to get rid of.
I've tried using re.sub function but this doesn't seem to work
import re
Poly_id= [['0', '[4', '8', '18', '20', '5', '0', '4]'], ['1', '[13', '16',
'6', '11', '13]'], ['2', '[3', '1', '10', '9', '2', '15', '3]'], ['3',
'[13', '12', '16', '13]'], ['4', '[13', '11', '17', '14', '7', '13]']]
for x in Poly_id:
[re.sub(r'\W', '', ch) for ch in x]
This doesn't seem to change a thing in this list.
I would like to have a list with only numbers as elements so that I could convert them into integers
I guess technically [4 is non numeric so you can do something like this:
Poly_id = [[char for char in _list if str.isnumeric(char)] for _list in Poly_id]
Output:
['0', '8', '18', '20', '5', '0']
['1', '16', '6', '11']
['2', '1', '10', '9', '2', '15']
['3', '12', '16']
['4', '11', '17', '14', '7']
If you just want to remove the non numeric values and not the complete entry then you can do this:
Poly_id = [[''.join(char for char in substring if str.isnumeric(char)) for substring in _list] for _list in Poly_id]
Output:
['0', '4', '8', '18', '20', '5', '0', '4']
['1', '13', '16', '6', '11', '13']
['2', '3', '1', '10', '9', '2', '15', '3']
['3', '13', '12', '16', '13']
['4', '13', '11', '17', '14', '7', '13']
Here a solution if you want to get rid of the '[' in '[4' but keep the '4':
res = [[re.sub(r'\W', '', st) for st in inlist] for inlist in Poly_id]
res is:
[
['0', '4', '8', '18', '20', '5', '0', '4'],
['1', '13', '16', '6', '11', '13'],
['2', '3', '1', '10', '9', '2', '15', '3'],
['3', '13', '12', '16', '13'],
['4', '13', '11', '17', '14', '7', '13']
]
You can use a module, "itertools"
import itertools
list_of_lists = [[1, 2], [3, 4]]
print(list(itertools.chain(*list_of_lists)))
>>>[1, 2, 3, 4]
I have this list :
[['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
I want to sort it with the names (or, in another words, by alphabetical order of the [0] element of each list within the list) but i don't want don't want the first list (['Nom', 'Francais', 'Anglais', 'Maths']) to be sorted with the others , how can in do that ?
Thanks a lot !
You can use range assignment:
>>> from pprint import pprint # just to have a nice display
>>> data = [['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
>>> pprint(data)
[['Nom', 'Francais', 'Anglais', 'Maths'],
['Catherine', '9', '17', '9'],
['Karim', '12', '15', '11'],
['Rachel', '15', '15', '14'],
['Roger', '12', '14', '12'],
['Gabriel', '7', '13', '8'],
['Francois', '14', '8', '15'],
['Henri', '10', '12', '13'],
['Stephane', '18', '12', '8'],
['Karine', '9', '10', '10'],
['Marie', '10', '10', '10'],
['Claire', '15', '9', '12'],
['Marine', '12', '9', '12']]
>>> data[1:] = sorted(data[1:])
>>> pprint(data)
[['Nom', 'Francais', 'Anglais', 'Maths'],
['Catherine', '9', '17', '9'],
['Claire', '15', '9', '12'],
['Francois', '14', '8', '15'],
['Gabriel', '7', '13', '8'],
['Henri', '10', '12', '13'],
['Karim', '12', '15', '11'],
['Karine', '9', '10', '10'],
['Marie', '10', '10', '10'],
['Marine', '12', '9', '12'],
['Rachel', '15', '15', '14'],
['Roger', '12', '14', '12'],
['Stephane', '18', '12', '8']]
Personally, I'd do something like this. But it assumes you're semi-comfortable with Pandas. This gives you a lot more flexibility to do more with the data.
import pandas as pd
nl = [['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
df = pd.DataFrame(columns = nl[0])
for l, c in zip(nl[0], range(4)):
df[l] = [ r[c] for r in nl[1:] ]
df.sort_values(by = 'Nom', inplace = True)
df.reset_index(drop = True, inplace = True)
which yields:
Nom Francais Anglais Maths
0 Catherine 9 17 9
1 Claire 15 9 12
2 Francois 14 8 15
3 Gabriel 7 13 8
4 Henri 10 12 13
5 Karim 12 15 11
6 Karine 9 10 10
7 Marie 10 10 10
8 Marine 12 9 12
9 Rachel 15 15 14
10 Roger 12 14 12
11 Stephane 18 12 8
and then if you need a .csv per your most recent comment, it's simply:
df.to_csv('/directory/my_filename.csv', index = False)
Basically the issue is as follows: I have a bunch of workers that have a function prescribed to each (the function is worker(alist) ) and am trying to process 35 workers at the same time. Each worker reads their line from the file (the modulo part) and should process the line using the "worker" function. I've pen-tested and found that the raw manipulation and deletion of the useless indices is working 100% as intended.
The args part of the "pool.apply_async" function isn't passing the list "raw" into it and starting the process. Raw is completely correct and functions normally, worker by itself functions normally, the pool.apply_async function is the only place that there seems to be an issue and I have no idea how to fix it. Any help please?
The relevant code is here:
NUM_WORKERS=35
f=open("test.csv")
pool=multiprocessing.Pool()
open("final.csv",'w')
for workernumber in range(1, NUM_WORKERS):
for i,line in enumerate(f):
if i==0:
print "Skipping first line" #dont do anything
elif i%workernumber==0:
raw = line.split(',')[0][1:-1].split()
uselessindices=[-2,-3,-4,-5,-6]
counter=0
for ui in uselessindices:
del raw[ui+counter]
counter+=1
print raw
pool.apply_async(worker, args=(raw,))
pool.close()
pool.join()
I suggest you put the calculation of raw into a generator function, and then use Pool.imap_unordered() or Pool.map() to run worker() over all of the items in the generator.
Something like this untested code:
def get_raw():
with open("test.csv", 'rU') as f:
for i, line in enumerate(f):
if i == 0:
# skip header
continue
raw = line.split(',')[0][1:-1].split()
uselessindices=[-2,-3,-4,-5,-6]
counter=0
for ui in uselessindices:
del raw[ui+counter]
counter+=1
yield raw
pool=multiprocessing.Pool(processes=NUM_WORKERS)
pool.map(worker, get_raw())
pool.close()
pool.join()
import multiprocessing
def worker(arg):
print 'doing work "%s"' % arg
return
NUM_WORKERS=35
with open('test.csv', 'w') as test:
for i in xrange(100):
if i % 10 == 0:
test.write('\n')
test.write('"%s 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23",' % i)
f=open("test.csv")
pool=multiprocessing.Pool(processes=NUM_WORKERS)
open("final.csv",'w')
for i, line in enumerate(f):
if i == 0:
continue
raw = line.split(',')[0][1:-1].split()
uselessindices=[-2,-3,-4,-5,-6]
counter=0
for ui in uselessindices:
del raw[ui+counter]
counter+=1
pool.apply_async(worker, args=(raw,))
pool.close()
pool.join()
print 'last raw len: %s' % len(raw)
print 'last raw value: %s' % raw
Output:
doing work "['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['10', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['20', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['30', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['40', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['50', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['60', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['70', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['80', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
doing work "['90', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']"
last raw len: 19
last raw value: ['90', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '23']
So I found out it wasn't throwing an error that was occurring inside the worker as a result of a mismatched number of inputs into a child function (AKA worker was calling another function dosomething(a1,a2,...a20) and was only giving it 19 inputs). It seems async won't throw error outputs about issues happening inside the worker which is quite annoying but I now understand. Thanks for all the help!