我正在开发过程add_to_index,它需要3个输入:
如果关键字已经在索引中,则将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) 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)