dictionary

How can write a program in scheme to find factors of a list of numbers

倖福魔咒の 提交于 2021-02-05 12:22:35
问题 This is the code for a single integer, how can it extends to list of function? (define (factors n) (define (*factors d) (cond ((> d n) (list)) ((= (modulo n d) 0) (cons d (*factors (+ d 1)))) (else (*factors (+ d 1))))) (*factors 1)) (display (factors 1111111)) (newline) 回答1: You can use for-each to iterate over a list. (define (factors n) (define (*factors d) (cond ((> d n) (list)) ((= (modulo n d) 0) (cons d (*factors (+ d 1)))) (else (*factors (+ d 1))))) (*factors 1)) (define arbitarily

Ranging over map keys of array type and slicing each array gives the same array for each iteration

强颜欢笑 提交于 2021-02-05 12:16:34
问题 When trying to add int array keys of a map to a slice of int slices, ranging and using arr[:] to slice array doesn't work as expected. The resultant slice contains only duplicates of the "first" key in the map(commented out for loop). However, copying the array key to another variable and slicing the new variable works, and the resultant slice contains distinct map key values. I wonder why the copying is necessary. Isn't k , the array key, copied from the map as a new array at each iteration?

Ranging over map keys of array type and slicing each array gives the same array for each iteration

两盒软妹~` 提交于 2021-02-05 12:14:17
问题 When trying to add int array keys of a map to a slice of int slices, ranging and using arr[:] to slice array doesn't work as expected. The resultant slice contains only duplicates of the "first" key in the map(commented out for loop). However, copying the array key to another variable and slicing the new variable works, and the resultant slice contains distinct map key values. I wonder why the copying is necessary. Isn't k , the array key, copied from the map as a new array at each iteration?

Taking a list of data frames and grouping by a variable and using that variable as the key to a dictionary

佐手、 提交于 2021-02-05 11:21:25
问题 I am relatively new to python programming. I have a list of pandas dataframes that all have the column 'Year'. I am trying to group by that column and convert to a dictionary where the dictionary key is the variable 'Year' and values is a list of dataframes of that year. Is this possible in python? I tried this: grouped_dict = list_of_csv_files.groupby(by = 'Year').to_dict() I believe I will have to loop through each dataframe? I did not provide any data because I am hoping it is a somewhat

python: concatenating strings of a list within a list

[亡魂溺海] 提交于 2021-02-05 11:16:25
问题 Is it possible to take each separate string from each list and combine it into one string, then have a list of strings? Instead of a list of strings within a list? names = ['red', 'barn'], ['barn'], ['front', 'porch'], ['white', 'farm', 'house']] Expected output below: names = ['red barn', 'barn', 'front porch', 'white farm house'] Here is what I have tried for name in names: names = " ".join(name) print(names) the output of this code is white farm house Why does this only concatenate the

create dict with multiple values out of two lists. group multiple keys into one

旧城冷巷雨未停 提交于 2021-02-05 10:53:07
问题 I have two list: lists = ['a','b','c','d','e'] keys = [18,18,3,4,5] what I want is a dictionary like this: {18:['a','b'],3:'c',4:'d',5:'e'} I keep getting this: {18: ['a', 'b', 'c', 'd', 'e'], 3: ['a', 'b', 'c', 'd', 'e'], 4: ['a', 'b', 'c', 'd', 'e'], 5: ['a', 'b', 'c', 'd', 'e']} I appreciate any advice!! 回答1: You can try this: dicts = {key: [] for key in keys} for k, v in zip(keys, lists): dicts[k].append(v) or from collections import defaultdict dicts = defaultdict(list) for k, v in zip

How to get values from a Map with different value types? (Java)

好久不见. 提交于 2021-02-05 10:48:22
问题 I have a utility function that returns Map<String, Object> , but that Map always has this structure: { "a": <something of type1>, "b": <something of type2> } In outer function, where I call this utility function, I want to get the value associated with key a . I'm trying to do something such as: Map<String, Object> data = findData(input); // findData is the utility function Type1 type1obj = data.get("a"); // error here But then I get the Incompatible Types error: Required: com.blah.blah.Type1

Create Dict within list of dict

十年热恋 提交于 2021-02-05 09:41:38
问题 I'm trying to create a dict within a list of dicts. How do I build the data structure and later to fetch the data via jinja2? Here is an example: var = { 'site': '', 'listofiles': [ {'time': '', 'name': ''} ] } exampledata = { 'site': 'DC1', 'listofiles': [ {'time': 'Thu Oct 3 22:26:40 2019', 'name': 'file1'}, {'time': 'Thu Oct 3 20:26:40 2019', 'name': 'file2'}, {'time': 'Thu Oct 3 21:26:40 2019', 'name': 'file3'} ] } How to populate data within the var? I have tried doing the following, but

Tkinter Create OptionMenus With Loop

倾然丶 夕夏残阳落幕 提交于 2021-02-05 09:29:16
问题 I have created a code to create tkinter OptionMenus with values in a dictionary using a for loop. The code seems to work successfully, with OptionMenus appearing with keywords on a window as desired... import tkinter as tk from tkinter import * class Example: def __init__(self): #Dictionary with categories and their relative keywords self.categorykeywords={"Category 1":["Keyword 1", "Keyword 2", "Keyword 3"], "Category 2":["Keyword A","Keyword B","Keyword C"], "Category 3":["Another Keyword"]

Reading multiple lines in a file, and separating their values at the same time, to store it in dictionary

半世苍凉 提交于 2021-02-05 09:26:32
问题 Hello I am beginner in python. I have following file as "test.txt" 1 2 5 2 6 7 as so on ..... I want to read the values and store it to a dictionary I have come up with following code but its not working as [x.split(' ') for x in edges.readline().rstrip().split(' ')] can access only one line of the file. Also this returns list on which int() typecasting can't be applied. It can access multiple line in a loop but I would like to know pythonic way to do this. connection = {(int(source),int(dest