Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a problem to get the value from first list based on second list. We can assume that we have an election. First list is the list of candidates, second list is the list of votes for this candidates.
candidatesList = [1,2,3,4]
voteList = [2,4,4,1]
One of the rules of election is that, if two or more candidates got same amount of votes then the winner is a candidate with lower number. In this case above output should be 2. I can change data structures but the output must be same.
Simplest way
candidatesList[voteList.index(max(voteList))]
max(voteList) gets you the max of the votes.
voteList.index(max(voteList)) gets you the index of the highest vote from the right hand side.
candidatesList[...] gets you the person
As far as I understand, this might be what you are looking for:
import numpy as np
candidates_list = [1,2,3,4]
vote_list = [2,4,4,1]
best_candidate_index = np.argmax(vote_list)
print("Best candidate", candidates_list[best_candidate_index])
Create dataframe, sort by ['voteList','candidatesList'] and use the top row.
d = dict(candidatesList = [1,2,3,4], voteList = [2,4,4,1])
pd.DataFrame(d).sort_values(by=['voteList','candidatesList'], ascending=[False,True]).candidatesList.iloc[0]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Given two lists, I need to count the frequency of the items in one list as they are found in the other list; and place the relative frequencies of each item inside frequencyList (where the
frequency of searchFor[0] is stored in frequencyList[0])
I am unable to import anything
textList=['a','b','a','c',...]
searchFor=['a','b']
frequencyList=[2,1]
Try:
[textList.count(i) for i in searchFor]
Or?
list(map(textList.count, searchFor))
The other answer is quite compact and very pythonic but this is an alternate solution that is slightly more efficient as it only requires one pass over the input list.
textList=['a','b','a','c']
output_dict = {}
for i in textList:
try:
output_dict[i] = d[i] + 1
except:
output_dict[i] = 1
print(output_dict['a'])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to sort a give list like shown below
input :-[0,0,1,0,2]
after sorting i want a list like this
[1,2,4,3,5]
As we can see if the list have same values it will compare according to the index.
An Another example of my question, input:[5,3,6,4,6] the output must be [3,1,4,2,5]
What is the best way to obtain the result ?? Thank you in advance.
Create a pair from [0,0,1,0,1] with index starting from 1 like [(1,0),(2,0), (3,1)...], after that sort by the seconds element ex: 0 in (1,0), then lastly take the first element.
l = [0,0,1,0,2]
[i[0] for i in sorted(enumerate(l,1), key=lambda x: x[1])]
[1, 2, 4, 3, 5]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If I have a list of elements such as:
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
I want to keep the elements with the smallest number after the second " - ". However, they must be compared with the elements which have the same first two numbers in the string.
For e.g. the strings which start with 058529-08704, the smallest number is 058529-08704-140200 and for 058529-08705, the smallest number is 058529-08705-140200
So the final list must end up with ["058529-08704-140200", "058529-08705-140200"].
What is the most pythonic way to achieve this instead of having to write multiple ifs or using string manipulation?
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
lst_3th_num = []
for item in items:
lst_3th_num.append(int(item.split('-')[2]))
result = []
for item in items:
if int(item.split('-')[2]) == min(int(s) for s in lst_3th_num):
result.append(item)
print(result)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have A huge data and this picture shows some sample of my data:
I want to make a edge list. If the row value of column1=column2=column3=column4=column6 are same, there is relation ship (edge) between the row value of column 5 And the result should be like below picture:
Is there a way to do this? Can postgressSQL or Python or R do that for me?
If I understand correctly, you want a self join:
select t1.col5 as vertex_1, t2.col5 as vertex_2
from t t1 join
t t2
on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3 and
t1.col4 = t2.col4 and t1.col6 = t2.col6 and
t1.col5 <> t2.col5;
I cannot tell if you want undirected or directed edges. If undirected, then change the last condition to: t1.col5 < t2.col5.
What you want is unique elements in your result list. Look at the SQL key words "unique" / "distinct", they can probably be used to generated unique rows.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I'm kinda new to python and was given a problem as follows:
Given the list names , find the largest element in the list and swap it with the last element. For example, the list ["Carlton", "Quincy" "Adam", "Bernard"] would become ["Carlton", "Bernard", "Adam", "Quincy"] . Assume names is not empty
I thought about doing list Comprehension but I don't know how I would write that out in code
EDIT: Largest in this case would be the length of the string (sorry for not clarifying!!!))
names = [foo, fooooo, bar, baaar]
a, b = i.index(max(name, key=len)), -1
i[b], i[a] = i[a], i[b]
Courtesy of this.
If by largest, you mean longest, then you could iterate over the list to find the longest name, and then swap them.
maxLen = 0
maxI = 0
for i in range(0, len(names)):
if len(names[i]) > maxLen:
maxLen = len(names[i])
maxI = i
temp = names[-1]
names[-1] = names[maxI]
names[maxI] = temp
This is an overly convoluted way of doing it, but it's so drawn out to make it more obvious as to what's going on. With that said, you really should be more specific about what "largest" means.