小编aks*_*pal的帖子

将元素追加到嵌套列表内的列表中-python

我正在开发过程add_to_index,它需要3个输入:

  • 索引:[[,[url1,url2,...]],...]
  • 关键字:字符串
  • 网址:字符串

如果关键字已经在索引中,则将url添加到与该关键字关联的url列表中。

如果关键字不在索引中,则索引中将包含一个新元素:

[keyword,[url]]
Run Code Online (Sandbox Code Playgroud)

index = []

def add_to_index(index,keyword,url):
    flag = 0
    count = 0
    for lists in index:
        count += 1
        if(lists[0]==keyword): 
            index[count][1].append(url)

    if(flag ==0):
        index.append([keyword,url])   

#calling the function below

add_to_index(index,'google','http://google.com')
print index
Run Code Online (Sandbox Code Playgroud)

输出-> [['google', 'http://google.com']]

add_to_index(index,'computing','http://acm.org')
print index
Run Code Online (Sandbox Code Playgroud)

输出-> [['google', 'http://google.com'], ['computing', 'http://acm.org']]

add_to_index(index,'google','http://gmail.com') 
print index
Run Code Online (Sandbox Code Playgroud)

错误->

index[count][1].append(url)
AttributeError: 'str' object has no attribute 'append'
Run Code Online (Sandbox Code Playgroud)

预期产量:

 [['google', ['http://google.com', 'http://gmail.com']], 
 ['computing', ['http://acm.org']]]
Run Code Online (Sandbox Code Playgroud)

python list nested-lists

5
推荐指数
1
解决办法
5851
查看次数

Why is CompletableFuture join/get faster in separate streams than using one stream

For the following program I am trying to figure out why using 2 different streams parallelizes the task and using the same stream and calling join/get on the Completable future makes them take longer time equivalent to as if they were sequentially processed).

public class HelloConcurrency {

    private static Integer sleepTask(int number) {
        System.out.println(String.format("Task with sleep time %d", number));
        try {
            TimeUnit.SECONDS.sleep(number);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return -1;
        }
        return number;
    }

    public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream completable-future

4
推荐指数
1
解决办法
86
查看次数