Make an edge list from data [closed] - python

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.

Related

How to count frequency of multiple items in a list and print relative frequencies [closed]

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'])

Sorting List according to values and if same value sorting it with index [closed]

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]

how to transform a flattened json data to a taxonomy string? [closed]

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 data like this, aka input data:
data = ['a-aa-aab', 'a-aa-aaa', 'b-ba', 'a-aa-aab-aaba', 'a-aa-aab-aabb']
And I want to transform that to the taxonomy string like this, aka output data:
root a b
a a-aa
a-aa a-aa-aab a-aa-aaa
a-aa-aab a-aa-aab-aaba a-aa-aab-aabb
b b-ba
I think there is a recursive solution in this sample, but I don't know how to achive this target. If you happen to know the answer, please tell me, god bless you!
from collections import defaultdict
data = ['a-aa-aab', 'a-aa-aaa', 'b-ba', 'a-aa-aab-aaba', 'a-aa-aab-aabb']
result = defaultdict(set)
for string in data:
parts = string.split('-')
for i in range(len(parts)):
key = '-'.join(parts[:i])
val = '-'.join(parts[:i+1])
result[key].add(val)
print(result)
for prefix, children in result.items():
print(prefix or 'root', *children)

Getting a value from list based on second list [closed]

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]

Efficient way of parsing string [closed]

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
How would you turn a string that looks like this
7.11,8,9:00,9:15,14:30,15:00
into this dictionary entry
{7.11 : [8, (9:00, 9:15), (14:30, 15:00)]}?
Suppose that the number of time pairs (such as 9:00,9:15 and 14:30,15:00 is unknown and you want to have them all as tuple pairs.
First split the string at the commas, then zip cluster starting from the 3rd element and put it into a dictionary:
s = "7.11,8,9:00,9:15,14:30,15:00"
ss = s.split(',')
d = {ss[0]: [ss[1]] + list(zip(*[iter(ss[2:])]*2))}
Output:
{'7.11': ['8', ('9:00', '9:15'), ('14:30', '15:00')]}
If you need to convert it from string to appropiate data types (you'll have to adapt it according to your needs), then after getting the ss list:
time_list = [datetime.datetime.strptime(t,'%H:%M').time() for t in ss[2:]]
d = {float(ss[0]): [int(ss[1])] + list(zip(*[iter(time_list)]*2))}

Categories