list

Scheme zip function with possible uneven lists

懵懂的女人 提交于 2021-02-10 17:26:39
问题 I know this question has been asked before, and my solution is the same as many of the answers but I have a special test case that won't work correctly with the common solution to this problem. The solution that I have found for the zip problem like many others is (define (zip l1 l2)(map list l1 l2)) . . .which works great with given arguments such as (zip '(a b c) '(1 2 3)) => ((a 1) (b 2) (c 3)) but I also want the zip function to work for cases where my arguments do not match length like

Converting a list of lists of strings to a data frame of numbers in R

蹲街弑〆低调 提交于 2021-02-10 16:02:33
问题 I have a list of lists of strings as follows: > ll [[1]] [1] "2" "1" [[2]] character(0) [[3]] [1] "1" [[4]] [1] "1" "8" The longest list is of length 2, and I want to build a data frame with 2 columns from this list. Bonus points for also converting each item in the list to a number or NA for character(0). I have tried using mapply() and data.frame to convert to a data frame and fill with NA's as follows. # Find length of each list element len = sapply(awards2, length) # Number of NAs to fill

Removing points from list if distance between 2 points is below a certain threshold

时光总嘲笑我的痴心妄想 提交于 2021-02-10 15:58:21
问题 I have a list of points and I want to keep the points of the list only if the distance between them is greater than a certain threshold. So, starting from the first point, if the the distance between the first point and the second is less than the threshold then I would remove the second point then compute the distance between the first one and the third one. If this distance is less than the threshold, compare the first and fourth point. Else move to the distance between the third and fourth

flatten list of lists and scalars

[亡魂溺海] 提交于 2021-02-10 15:58:01
问题 So for a matrix, we have methods like numpy.flatten() np.array([[1,2,3],[4,5,6],[7,8,9]]).flatten() gives [1,2,3,4,5,6,7,8,9] what if I wanted to get from np.array([[1,2,3],[4,5,6],7]) to [1,2,3,4,5,6,7] ? Is there an existing function that performs something like that? 回答1: With uneven lists, the array is a object dtype, (and 1d, so flatten doesn't change it) In [96]: arr=np.array([[1,2,3],[4,5,6],7]) In [97]: arr Out[97]: array([[1, 2, 3], [4, 5, 6], 7], dtype=object) In [98]: arr.sum() ...

Removing points from list if distance between 2 points is below a certain threshold

纵然是瞬间 提交于 2021-02-10 15:52:12
问题 I have a list of points and I want to keep the points of the list only if the distance between them is greater than a certain threshold. So, starting from the first point, if the the distance between the first point and the second is less than the threshold then I would remove the second point then compute the distance between the first one and the third one. If this distance is less than the threshold, compare the first and fourth point. Else move to the distance between the third and fourth

Hot to get a bold odd-numbered list in LaTeX?

烈酒焚心 提交于 2021-02-10 15:28:30
问题 \begin{enumerate}[label=\textbf{\arabic*.}] \item Numbered 1 \addtocounter{enumi}{1} \item Numbered 3 \addtocounter{enumi}{1} \item Numbered 5 \addtocounter{enumi}{1} \item Numbered 7 \addtocounter{enumi}{1} \item and so on... \end{enumerate} Is there a better way to get enumerate to give you a list of just the odd numbers without having to add an \addtocounter between every item? 回答1: Reworking this reference from TeX.SX quickly enough, my first suggestion would be the following, using the

Print a list of tuples as table

梦想的初衷 提交于 2021-02-10 14:53:17
问题 I have a list o tuples: list = [(element1, element2, element3), (elementelel4, element5, elementelement6), (el7, el8, elel9)] I want to print it as a table (so the distance between elements should be the same): HEADER1 HEADER2 HEADER3 element1 element2 element3 elementelel4 element5 elementelement6 el7 el8 elel9 I was trying to use some examples which I found, but it was used to print a list of lists, and I have a list of tuples. 回答1: Using Format Specification Mini-Language print "HEADER1

Creating nested dictionaries from a list containing paths

空扰寡人 提交于 2021-02-10 14:50:49
问题 I have a list containing paths. For example: links=['main', 'main/path1', 'main/path1/path2', 'main/path1/path2/path3/path4', 'main/path1/path2/path3/path5', 'main/path1/path2/path3/path4/path6'] I want to create a nested dictionary to store these paths in order. Expected output: Output = {‘main’: {‘path1’: {‘path2’: {‘path3’: {‘path4’: {‘path6’: {} }},‘path5’:{}}}}} I am new to python coding (v 3.+) and I am unable to solve it. It gets confusing after i reach path 3 as there is path 4 (with

Creating nested dictionaries from a list containing paths

喜你入骨 提交于 2021-02-10 14:50:26
问题 I have a list containing paths. For example: links=['main', 'main/path1', 'main/path1/path2', 'main/path1/path2/path3/path4', 'main/path1/path2/path3/path5', 'main/path1/path2/path3/path4/path6'] I want to create a nested dictionary to store these paths in order. Expected output: Output = {‘main’: {‘path1’: {‘path2’: {‘path3’: {‘path4’: {‘path6’: {} }},‘path5’:{}}}}} I am new to python coding (v 3.+) and I am unable to solve it. It gets confusing after i reach path 3 as there is path 4 (with

pandas mapping list to dict items for new column

久未见 提交于 2021-02-10 14:41:05
问题 i have df like: col_A [1,2,3] [2,3] [1,3] and dict like: dd = {1: "Soccer", 2: "Cricket", 3: "Hockey"} how can i create a new column col_B like: col_A col_B [1,2,3] ["Soccer", "Cricket", "Hockey"] [2,3] ["Cricket", "Hockey"] [1,3] ["Soccer", "Hockey"] tried something like: df['sports'] = df['col_A'].map(dd) got error: TypeError: unhashable type: 'list' 回答1: You can use list comprehension with if for filter out not matched values: df['sports'] = df['col_A'].map(lambda x: [dd[y] for y in x if y