I am writing a broader algorithm then this, but I think this issue is the reason it is not working.
Here the troublesome snippet of code..
difcheck = []
count = 1
questionsneeded = 4
required_dif = ["Easy", "Hard", "Medium", "Easy"]
required_topics = ["Graphs", "Math", "Geometry", "Fun"]
availablelist = [("Graphs", "Easy"), ("Fun", "aasd"), ("Geometry", "Medium"), ("Math", "Easy")]
z = 0
answerlist = []
while z < questionsneeded:
difs = required_dif
topics = required_topics
for x in range(len(availablelist)):
if availablelist[x][0] in required_topics and availablelist[x][1] in required_dif :
difcheck.append(availablelist[x])
try:
required_dif.remove(availablelist[x][0])
required_topics.remove(availablelist[x][1])
except ValueError as exception:
continue
availablelist.append(availablelist[0])
answerlist.append(len(difcheck))
del availablelist[0]
print("availablelist ")
print(availablelist)
print("difcheck ")
print(difcheck)
z = z + 1
del availablelist[0]
del difcheck[:]
The code looks through the tuples in availablelist and compares tuple element 0 with every element in required_dif and tuple element 1 with every element in required_topics.
If both are found in both lists, the tuple will be added to difcheck.
It then repeats this until z = questionsneeded, tuples in available list SHOULD be shuffled everytime.
The output I get is
availablelist
[('Fun', 'aasd'), ('Geometry', 'Medium'), ('Math', 'Easy'), ('Graphs', 'Easy')]
difcheck
[('Graphs', 'Easy'), ('Geometry', 'Medium'), ('Math', 'Easy')]
availablelist
[('Math', 'Easy'), ('Graphs', 'Easy'), ('Geometry', 'Medium')]
difcheck
[('Geometry', 'Medium'), ('Math', 'Easy'), ('Graphs', 'Easy')]
availablelist
[('Geometry', 'Medium'), ('Graphs', 'Easy')]
difcheck
[('Graphs', 'Easy'), ('Geometry', 'Medium')]
availablelist
[('Graphs', 'Easy')]
difcheck
[('Graphs', 'Easy')]
The output I want is
availablelist
[("Graphs", "Easy"), ("Fun", "aasd"), ("Geometry", "Medium"), ("Math", "Easy")]
difcheck
[("Graphs", "Easy"), ("Fun", "aasd"), ("Geometry", "Medium"), ("Math", "Easy")]
availablelist
[("Fun", "aasd"), ("Geometry", "Medium"), ("Math", "Easy"), ("Graphs", "Easy")]
difcheck
[("Fun", "aasd"), ("Geometry", "Medium"), ("Math", "Easy"), ("Graphs", "Easy")]
availablelist
[("Geometry", "Medium"), ("Math", "Easy"), ("Graphs", "Easy"), ("Fun", "aasd")]
difcheck
[("Geometry", "Medium"), ("Math", "Easy"), ("Graphs", "Easy"), ("Fun", "aasd")]
availablelist
[("Math", "Easy"), ("Graphs", "Easy"), ("Fun", "aasd"), ("Geometry", "Medium")]
difcheck
[("Math", "Easy"), ("Graphs", "Easy"), ("Fun", "aasd"), ("Geometry", "Medium")]
In an instance where an element in required_topics or required_dif is not in a tuple in availablelist, it would not be added to difcheck. In this case, we have a perfect match so everything is added.
The problem is probably very obvious, but I've gone all hazy and cant see it.
This part looks backwards
required_dif.remove(availablelist[x][0])
required_topics.remove(availablelist[x][1])
I think it should be
required_dif.remove(availablelist[x][1])
required_topics.remove(availablelist[x][0])
Also, the reason your list keeps getting smaller is because you have one append and two deletes on each cycle through the list.
availablelist.append(availablelist[0]) # Add 1 item
answerlist.append(len(difcheck))
del availablelist[0] # Delete 1 item
print("availablelist ")
print(availablelist)
print("difcheck ")
print(difcheck)
z = z + 1
del availablelist[0] # Delete another item
Related
I have the following function which produces results;
myNames = ['ULTA', 'CSCO', ...]
def get_from_min_match(var):
temp = []
count_elem = generate_elem_count()
for item in count_elem:
if var <= count_elem[item]:
temp.append(item)
return set(temp) if len(set(temp)) > 0 else "None"
def generate_elem_count():
result_data = []
for val in mapper.values():
if type(val) == list:
result_data += val
elif type(val) == dict:
for key in val:
result_data.append(key)
count_elem = {elem: result_data.count(elem) for elem in result_data}
return count_elem
I call this function like this;
myNames_dict_1 = ['AME', 'IEX', 'PAYC']
myNames_dict_1 = ['ULTA', 'CSCO', 'PAYC']
mapper = {1: myNames_dict_1, 2: myNames_dict_2}
print(" These meet three values ", get_from_min_match(3))
print(" These meet four values ", get_from_min_match(4))
The output I get from these functions are as follows;
These meet three values {'ULTA', 'CSCO', 'SHW', 'MANH', 'TTWO', 'SAM', 'RHI', 'PAYC', 'AME', 'CCOI', 'RMD', 'AMD', 'UNH', 'AZO', 'APH', 'EW', 'FFIV', 'IEX', 'IDXX', 'ANET', 'SWKS', 'HRL', 'ILMN', 'PGR', 'ATVI', 'CNS', 'EA', 'ORLY', 'TSCO'}
These meet four values {'EW', 'PAYC', 'TTWO', 'AME', 'IEX', 'IDXX', 'ANET', 'RMD', 'SWKS', 'HRL', 'UNH', 'CCOI', 'ORLY', 'APH', 'PGR', 'TSCO'}
Now, I want to insert the output, of the get_from_min_match function into a Sqlite database. Its structure looks like this;
dbase.execute("INSERT OR REPLACE INTO min_match (DATE, SYMBOL, NAME, NUMBEROFMETRICSMET) \
VALUES (?,?,?,?)", (datetime.today(), symbol, name, NUMBEROFMETRICSMET?))
dbase.commit()
So, it's basically a new function to calculate the "NUMBEROFMETRICSMET" parameter rather than calling each of these functions many times. And I want the output of the function inserted into the database. How to achieve this? Here 3, 4 would be the number of times the companies matched.
date ULTA name 3
date EW name 4
...
should be the result.
How can I achieve this? Thanks!
I fixed this by just using my already written function;
count_elem = generate_elem_count()
print("Count Elem: " + str(count_elem))
This prints {'AMPY': 1} and so on.
I wanted to create a table where Item Code is main row and Challan No. Is sub row. For every item Item Code is only one and Challan No. is only one. But same Challan No. are allowed for different Item Code.
item_table = [('I-101', 'Wall Tiles'), ('I-102', 'Floor Tiles'), ('I-103', 'Wall Tiles')]
in_table = [('I-101', 'C-1415', 100.0), ('I-102', 'C-1469', 110.0), ('I-103', 'C-1455', 120.0), ('I-101', 'C-1897', 130.0), ('I-101', 'C-1415', 140.0), ('I-102', 'C-1415', 150.0), ('I-103', 'C-1897', 160.0)]
out_table = [('I-101', 'C-1415', -100.0), ('I-103', 'C-1455', -500.0)]
for elem in item_table:
print(elem[0])
for bill in in_table:
if elem[0] == bill[0]:
print(bill[1])
This is my problem
this is my main design
Make a set of all the challan numbers that have been printed, so you don't print duplicates.
item_table = [('I-101', 'Wall Tiles'), ('I-102', 'Floor Tiles'), ('I-103', 'Wall Tiles')]
in_table = [('I-101', 'C-1415', 100.0), ('I-102', 'C-1469', 110.0), ('I-103', 'C-1455', 120.0), ('I-101', 'C-1897', 130.0), ('I-101', 'C-1415', 140.0), ('I-102', 'C-1415', 150.0), ('I-103', 'C-1897', 160.0)]
out_table = [('I-101', 'C-1415', -100.0), ('I-103', 'C-1455', -500.0)]
for elem in item_table:
printed = set()
print(elem[0])
for bill in in_table:
if elem[0] == bill[0] and bill[1] not in printed:
print(bill[1])
printed.add(bill[1])
DEMO
I have a list:
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
I want to write a function to return a dictionary which has first name as key, all full names associated with the first name as value:
{'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald',
'Shelba Odle', 'Shelba Fry'], 'David': ['David Joyner', 'David Zuber'],
'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Maren': ['Maren Fry'],
'Nicol': ['Nicol Barthel']}
thanks.
Smart way (1 pass through your list):
from collections import defaultdict
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
rv = defaultdict(list)
for elem in name_list:
rv[elem.split()[0]].append(elem)
print(dict(rv))
Output:
{'David': ['David Joyner', 'David Zuber'],
'Brenton':['Brenton Joyner', 'Brenton Zuber'],
'Nicol': ['Nicol Barthel'],
'Shelba': ['Shelba Barthel','Shelba Crowley','Shelba Fernald','Shelba Odle','Shelba Fry'],
'Maren': ['Maren Fry']}
Slow way (might look nice, but is inefficient):
d = {key:[n for n in name_list if n.startswith(key)]
for key in set( p.split()[0] for p in name_list)}
This is computationally far worse then the defaultdict version because it traverses the list multiple times:
once to create set( p.split()[0] for p in name_list)
and for every name we just gathered it traverses the full list again to create the dict-value [n for n in name_list if n.startswith(key)]
If you need to sort afterwards, simply use the list.sort() on your dicts values:
# sorting the data afterwards
for k in rv:
rv[k].sort(key = lambda x:x.split()[1]) # to sort by last names
Output:
# only difference when sorted: see key `Shelba`
{'David': ['David Joyner', 'David Zuber'],
'Brenton':['Brenton Joyner', 'Brenton Zuber'],
'Nicol': ['Nicol Barthel'],
'Shelba': ['Shelba Barthel','Shelba Crowley','Shelba Fernald','Shelba Fry','Shelba Odle'],
'Maren': ['Maren Fry']}
As Patrick Artner suggested in the comments, use defaultdict. How about this:
from collections import defaultdict
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
name_dict = defaultdict(list)
for first_name in set([name.split()[0] for name in name_list]):
name_dict[first_name] = [name for name in name_list if name.split()[0] == first_name]
print(name_dict)
#defaultdict(<class 'list'>, {'Maren': ['Maren Fry'], 'David': ['David Joyner', 'David Zuber'], 'Nicol': ['Nicol Barthel'], 'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 'Shelba Odle', 'Shelba Fry'], 'Brenton': ['Brenton Joyner', 'Brenton Zuber']})
Try this:
def build_dict(name_list)
result_dict = {}
for name in name_list:
names = name.split(" ")
if names[0] in result_dict:
result_dict[names[0]].append(names[1])
else:
result_dict[names[0]] = [names[1]]
return result_dict
The idea is to iterate of the name list, split each name into first name and last name, see if the first name is in the dictionary. if it is, look it up and append the last name to the list of last names. if it is not, assign it a list whose only member is the last name
def name_lists(name_list):
d = {}
for name in name_list:
first_name = name.split()[0]
if first_name in d:
d[first_name].append(name)
d[first_name].sort()
else:
d[first_name]=[name]
return d
def name_counts(name_list):
result_dict = {}
for name in name_list:
names = name.split(" ")
if names[0] in result_dict:
result_dict[names[0]] += 1
else:
result_dict[names[0]] = 1
return result_dict
def name_lists(n_list):
result_dict = {}
n_list.sort()
for name in n_list:
names = name.split(" ")
if names[0] in result_dict:
result_dict[names[0]].append(name)
else:
result_dict[names[0]] = [name]
return result_dict
In the below list I'm trying to insert each name and phone number in a db. So I tried to iterate and assign it to a variable and use it in insert statement.
contactlist = [
['Siemens, Harper', '323-4149'],
['Smith, Patti', '239-1212'],
['Jackson, Janet', '313-1352'],
['Manfredi, Ralph','872-2221'],
['Thompson, Bobby', '365-2622'],
['James, Lebron', '457-6223'],
['Ziegler, Zig', '667-1101'],
['Robbins, Tony', '329-2310']
]
This is what I have been trying:
a = []
for data in contactlist:
#print (data)
print (data[0])
for d1 in data:
#print (d1)
a.append(d1)
print (a)
you can use reduce function from functools
from functools import reduce
reduce(lambda m,n: m+n,contanctlist)
in your code
>>> contactlist = [
... ['Siemens, Harper', '323-4149'],
... ['Smith, Patti', '239-1212'],
... ['Jackson, Janet', '313-1352'],
... ['Manfredi, Ralph','872-2221'],
... ['Thompson, Bobby', '365-2622'],
... ['James, Lebron', '457-6223'],
... ['Ziegler, Zig', '667-1101'],
... ['Robbins, Tony', '329-2310']
... ]
>>> a = reduce(lambda m,n: m+n,contactlist)
>>> a
['Siemens, Harper', '323-4149', 'Smith, Patti', '239-1212', 'Jackson, Janet', '313-1352', 'Manfredi, Ralph', '872-2221', 'Thompson, Bobby', '365-2622', 'James, Lebron', '457-6223', 'Ziegler, Zig', '667-1101', 'Robbins, Tony', '329-2310']
As per Varun your comment
contactlist = [
['Siemens, Harper', '323-4149'],
['Smith, Patti', '239-1212'],
['Jackson, Janet', '313-1352'],
['Manfredi, Ralph','872-2221'],
['Thompson, Bobby', '365-2622'],
['James, Lebron', '457-6223'],
['Ziegler, Zig', '667-1101'],
['Robbins, Tony', '329-2310']
]
phone_list = []
person = []
for contact in contactlist:
phone_list.append(contact[1])
person.append(contact[0])
print phone_list
print person
As far as I can see you are accessing your numbers wrong. Your contactlist is a list of lists. So you can access each entry like the way you did. If you need the number take the second entry of that data like this:
for data in contactlist:
name = data[0]
number = data[1]
print 'Name: %s, Number %s' %(name, number)
Instead of printing the entries you can do whatever you need to do with your variables to add them into your database.
Looking at this: Inserting multiple rows in a single SQL query?, one way would be to create a string that you insert in your command
contactlist = [
['Siemens, Harper', '323-4149'],
['Smith, Patti', '239-1212'],
['Jackson, Janet', '313-1352'],
['Manfredi, Ralph','872-2221'],
['Thompson, Bobby', '365-2622'],
['James, Lebron', '457-6223'],
['Ziegler, Zig', '667-1101'],
['Robbins, Tony', '329-2310']
]
str([tuple(i) for i in contactlist])[1:-1]
Prints
"('Siemens, Harper', '323-4149'), ('Smith, Patti', '239-1212'), ('Jackson, Janet', '313-1352'), ('Manfredi, Ralph', '872-2221'), ('Thompson, Bobby', '365-2622'), ('James, Lebron', '457-6223'), ('Ziegler, Zig', '667-1101'), ('Robbins, Tony', '329-2310')"
But looking at the Python docs that is not necessary. Consider this
full example: (https://docs.python.org/2/library/sqlite3.html):
import sqlite3
contactlist = [
['Siemens, Harper', '323-4149'],
['Smith, Patti', '239-1212'],
['Jackson, Janet', '313-1352'],
['Manfredi, Ralph','872-2221'],
['Thompson, Bobby', '365-2622'],
['James, Lebron', '457-6223'],
['Ziegler, Zig', '667-1101'],
['Robbins, Tony', '329-2310']
]
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Delete table
c.execute('drop table if exists employees')
# Create table
c.execute('''CREATE TABLE employees
(name, number)''')
# Insert a row of data
c.executemany('INSERT INTO employees VALUES (?,?)', contactlist)
# or
# c.execute("INSERT INTO employees VALUES {}".format(str([tuple(i) for i in contactlist])[1:-1]))
conn.commit() # Commit the changes
conn.close()
The database now contains a table called employees with the following rows:
('Siemens, Harper', '323-4149')
('Smith, Patti', '239-1212')
('Jackson, Janet', '313-1352')
('Manfredi, Ralph', '872-2221')
('Thompson, Bobby', '365-2622')
('James, Lebron', '457-6223')
('Ziegler, Zig', '667-1101')
('Robbins, Tony', '329-2310')
Just a suggestion but in your comment of Kallz's post you said "The same output Im getting in my code posted above also but my problem i want to extract individual names and phone numbers".
For this you might want to use a dictionary.
Contacts={}
Contacts['Barry']="184788174"
Contacts['Stewart']="1515174"
Contacts['Sarah']="1252358174"
print(Contacts['Barry'])
Returns:
184788174
Or to display all:
Contacts={}
Contacts['Barry']="184788174"
Contacts['Stewart']="1515174"
Contacts['Sarah']="1252358174"
for data in Contacts:
print(data, ":")
print(Contacts[data], """
""")
Returns:
Barry :
184788174
Stewart :
1515174
Sarah :
1252358174
This question already has answers here:
Generating natural schedule for a sports league
(2 answers)
Closed 5 years ago.
I would like to write a League Fixture generator in python, but I can't. Here is the details:
There is a dynamic list of teams like teams = ["Team1", "Team2", "Team3", "Team4"].
How can I generate a fixture_weekx list from the teams list? For example:
fixture_week1 = ["Team1", "Team2", "Team3", "Team4"]
fixture_week2 = ["Team1", "Team3", "Team2", "Team4"]
fixture_week2 = ["Team1", "Team4", "Team2", "Team3"]
#Return matches:
fixture_week1 = ["Team2", "Team1", "Team4", "Team3"]
fixture_week2 = ["Team3", "Team1", "Team4", "Team2"]
fixture_week2 = ["Team4", "Team1", "Team3", "Team2"]
Any idea?
Fixture scheduling is a well known problem. This is python implementation of algorithm given at: http://en.wikipedia.org/wiki/Round-robin_tournament
# generation code - for cut and paste
import operator
def fixtures(teams):
if len(teams) % 2:
teams.append('Day off') # if team number is odd - use 'day off' as fake team
rotation = list(teams) # copy the list
fixtures = []
for i in range(0, len(teams)-1):
fixtures.append(rotation)
rotation = [rotation[0]] + [rotation[-1]] + rotation[1:-1]
return fixtures
# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]
# for one match each - use this block only
matches = fixtures(teams)
for f in matches:
print zip(*[iter(f)]*2)
# if you want return matches
reverse_teams = [list(x) for x in zip(teams[1::2], teams[::2])]
reverse_teams = reduce(operator.add, reverse_teams) # swap team1 with team2, and so on ....
#then run the fixtures again
matches = fixtures(reverse_teams)
print "return matches"
for f in matches:
print f
This generates output:
[('Team1', 'Day off'), ('Team2', 'Team5'), ('Team3', 'Team4')]
[('Team1', 'Team5'), ('Day off', 'Team4'), ('Team2', 'Team3')]
[('Team1', 'Team4'), ('Team5', 'Team3'), ('Day off', 'Team2')]
[('Team1', 'Team3'), ('Team4', 'Team2'), ('Team5', 'Day off')]
[('Team1', 'Team2'), ('Team3', 'Day off'), ('Team4', 'Team5')]
I wanted to comment that the code from #MariaZverina doesn't quite work. I tried it as is, but I didn't get the right pairings. The modification that I made below works with her code. The difference is that I do a rainbow style pairing of each fixture by zipping the first half of the fixture f
with the reversed second half.
# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]
# for one match each - use this block only
matches = fixtures(teams)
for f in matches:
# This is where the difference is.
# I implemented "rainbow" style pairing from each fixture f
# In other words:
# [(f[0],[f[n-1]), (f[1],f[n-2]), ..., (f[n/2-1],f[n/2])],
# where n is the length of f
n = len(f)
print zip(f[0:n/2],reversed(f[n/2:n]))
The code from #MariaZverina hasn't worked, I have implemented this code using Round-robin_tournament as well:
teams = ["Team1", "Team2", "Team3", "Team4", "Team5", "Team6"]
if len(teams) % 2:
teams.append('Day off')
n = len(teams)
matchs = []
fixtures = []
return_matchs = []
for fixture in range(1, n):
for i in range(n/2):
matchs.append((teams[i], teams[n - 1 - i]))
return_matchs.append((teams[n - 1 - i], teams[i]))
teams.insert(1, teams.pop())
fixtures.insert(len(fixtures)/2, matchs)
fixtures.append(return_matchs)
matchs = []
return_matchs = []
for fixture in fixtures:
print fixture
Output:
[('Team1', 'Team6'), ('Team2', 'Team5'), ('Team3', 'Team4')]
[('Team1', 'Team5'), ('Team6', 'Team4'), ('Team2', 'Team3')]
[('Team1', 'Team4'), ('Team5', 'Team3'), ('Team6', 'Team2')]
[('Team1', 'Team3'), ('Team4', 'Team2'), ('Team5', 'Team6')]
[('Team1', 'Team2'), ('Team3', 'Team6'), ('Team4', 'Team5')]
[('Team6', 'Team1'), ('Team5', 'Team2'), ('Team4', 'Team3')]
[('Team5', 'Team1'), ('Team4', 'Team6'), ('Team3', 'Team2')]
[('Team4', 'Team1'), ('Team3', 'Team5'), ('Team2', 'Team6')]
[('Team3', 'Team1'), ('Team2', 'Team4'), ('Team6', 'Team5')]
[('Team2', 'Team1'), ('Team6', 'Team3'), ('Team5', 'Team4')]