tuples

sort tuples in dictionary based on time value

北战南征 提交于 2019-12-13 08:57:26
问题 I have a dictionary looking like this: dict1 = {'key1': ['x1', [(time1,value1), (time3,value3), (time2,value2)]], 'key2': ['x2', [(time6,value6), (time4,value4), (time5,value5)]], ...} For each key in the dictionary, I wish to sort the tuples based on the time. Looking at the example above, assume that time1 < time2 < time3 time4 < time5 < time6 The output I wish is then dict1 = {'key1': ['x1', [(time1,value1), (time2,value2), (time3,value3)]], 'key2': ['x2', [(time4,value4), (time5,value5),

how to convert a text into tuples in a list in python

て烟熏妆下的殇ゞ 提交于 2019-12-13 08:08:10
问题 I am a beginner in python and desperately need someone's help. I am trying to convert a text into tuples in a list. The original text was already tokenized and each pos was tagged as below: The/DT Fulton/NNP County/NNP Grand/NNP Jury/NNP said/VBD Friday/NNP an/DT And the desired output looks as below: [('The', 'DT'), ('Fulton', 'NNP'), ('County', 'NNP'), ...)] So, if anyone can offer me a help, it would be so awesome! Thanks in advance! 回答1: You can use list comprehension like below: >>> s =

How to correct this for loop over tuple function in Python?

佐手、 提交于 2019-12-13 07:03:31
问题 Here is a program where each line was split into pairs using tuples, such that every alphabet had a corresponding numeric as A:6, B:6, C:35 ..etc If a value for less than 10, then the alphabets were converted to N. The following is the code. I find that my code does not loop over the tuple function in the last part of the code. It takes in only a single sequence and does not loop over the other tutorial = open('c:/test/z.txt','r') ## Input file looks like >qrst ABCDE-- 6 6 35 25 10 >qqqq

Concatenate items in two nested lists to pairs in tuples

人走茶凉 提交于 2019-12-13 06:24:41
问题 I have two nested lists: ls1 = [["a","b"], ["c","d"]] ls2 = [["e","f"], ["g","h"]] and I'd like the following result [(a,e), (b,f), (c,g), (d,h)] I've tried zip(a,b), how do I zip nested lists into a list with tupled pairs? 回答1: You can also use itertools.chain.from_iterable and zip : >>> ls1 = [["a","b"], ["c","d"]] >>> ls2 = [["e","f"], ["g","h"]] >>> >>> zip(itertools.chain.from_iterable(ls1), itertools.chain.from_iterable(ls2)) [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', 'h')] 回答2: You

Need to remove duplicates from a nested list preserving the order

穿精又带淫゛_ 提交于 2019-12-13 04:58:59
问题 I have a list like below L = [[1,2],[1,3],[1,4],[2,1],[2,5],[3,1],[3,2]] The output should be [[1,2],[1,3],[1,4],[2,5],[3,2]] Please note that the order for the pairs and the elements has to be preserved. In other words, I cannot sort the list because the elements order needs to be preserved for the pairs as well. For example, I need the last element [3,2] to be preserved. If i sort them and remove duplicates this will be changed to [2,3] which I do not want. Also when I say I need to remove

How to get Tuple property by property index in c#?

拥有回忆 提交于 2019-12-13 04:31:06
问题 I need to implement Comparison delegate for Tuple. And I have a column number by which I want to compare Tuples. For now I got to: int sortedColumn; Comparison<Tuple<T1, T2, T3>> tupleComparison = (x, y) => { // I want to access x.Item2 if sortedColumn = 2 // x.Item3 if sortedColumn = 2 etc }; How can I do this in c#? Can I do it without using switch ? 回答1: I'd probably go with if / else or switch , but if you want to avoid that (e.g. so you can use it with any Tuple ), you can use reflection

MVC Passing Four Pieces of Data to the Controller and subsequently to the View

风格不统一 提交于 2019-12-13 04:29:37
问题 This question is part B of my other question here: Inefficient MVC ViewModel Making Multiple Calls to the Database? I knew multiple calls to the db was a bad idea. But, I guess my real question is if I use my last chunk of code or speti43's example let's say like this in my model constructor to eliminate multiple db calls: Public Class OrdersSummary{ ... Public ???? GetOrders(){ var ordersInMemory = orders.ToList(); decimal? GrossProfitTotal = ordersInMemory.Sum(s => s.Profit); decimal?

Is there a container that can store items of different types?

ε祈祈猫儿з 提交于 2019-12-13 04:26:09
问题 Storing items of the same type is trivial, but I need a container that can store items of different types. Here's an example showing what I'd like to do: Class C { }; C c1; C c2; C c3; std::tuple<C> tup1(c1); std::tuple<C, C> tup2(c1, c2); std::tuple<C, C, C> tup3(c1, c2, c3); container_type container(tup1, tup2, tup3); Is there any container that works in this way? If not, is there a way to create one? I'd like something that overloads operator[] for fast random access. 回答1: It won't work.

Cout from a map with std::tuple

匆匆过客 提交于 2019-12-13 04:17:49
问题 I have made a small map that I call BMW . It contains the keys Usage and Diesel , as shown below. #include <iostream> #include <bits/stdc++.h> #include <map> #include <vector> using namespace std; int main() { // initialize container std::map<string, std::tuple<string, string>> BMW; // insert elements BMW.insert({"Usage", {"1", "2"}}); BMW.insert({"Disel", {"2", "3"}}); std::cout << "Usage => " << BMW.find('Usage')->second << '\n'; return 0; } What I want to do is to find the key Usage in the

Python convertion of list of strings to list of tuples

自作多情 提交于 2019-12-13 03:33:59
问题 How to convert following list ['abc,cde,eg,ba', 'abc,cde,ba'] in to list of tuples? [('abc','cde','eg','ba'), ('abc','cde','ba')] What I have tried output = [] for item in my_list: a = "','".join((item.split(','))) output.append(a) 回答1: In your loop, you are splitting the string (which will give you a list), but then you are joining it back with a , , which is returning to you the same string: >>> 'a,b'.split(',') ['a', 'b'] >>> ','.join('a,b'.split(',')) 'a,b' You can convert a list to a