小编use*_*636的帖子

基于多个属性选择数据 d3

我有一个使用 d3 和 svg 表示的网格。我正在尝试选择网格上任何特定图块的相邻(相邻)图块。这些图块通过它们在网格上的 x 和 y 坐标进行访问。我感觉相当凌乱,并没有完全按照我的意愿去做,我不希望点击的瓷砖被选中,或者瓷砖对角线。

var w = 960,
    h = 500,
    z = 20,
    x = w / z,
    y = h / z;

var svg = d3.select("body").append("svg")
    .attr("width", w)
    .attr("height", h);

svg.selectAll("rect")
    .data(d3.range(x * y))
  .enter().append("rect")
    .attr("width", z)
    .attr("height", z)
    .attr("clicked", false)
    .attr('x', horizontalpos)
    .attr('y', verticalpos)
    .on("click", test)
    .style("stroke", "rgb(6,120,155)")
    .style("stroke-width", 2)
    .style("fill", "rgb(255, 255, 255)")

function translate(d) {
  return "translate(" + (d % x) * z + "," + Math.floor(d / x) * …
Run Code Online (Sandbox Code Playgroud)

javascript svg d3.js

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

迭代两个字符串,当相同索引处的字符匹配时返回计数

我有两个单词,一个是用户输入字符串,另一个是从文本文件中随机选择的单词.我想返回两个stings中相等的计数值并共享相同的字符串索引.例如,word1 ='bleed',word2 = slice:similar = 1.

word1 = 'frog'
word2 = 'friend'
correct = 0

if len(word1) > len(word2):
    for i in range(len(word2)):
        if word1[i] == word2[i]:
            correct =+ 1
        else:
            correct == 0

else:
   for i in range(len(word1)):
       if word1[i] == word2[i]:
           correct =+ 1
       else:
           correct == 0
Run Code Online (Sandbox Code Playgroud)

我是编程的新手,不幸的是我的尝试最大限度地输出了正确= 1.对于我在示例青蛙和朋友中使用的单词我希望看到正确= 2,我的代码产生正确= 1.我如何添加到正确,超过1?谢谢

python

3
推荐指数
1
解决办法
1639
查看次数

计算字符串python中的字母

我必须编写一个函数,countLetters(word)它接受一个单词作为参数并返回一个列表,该列表计算每个字母出现的次数.字母必须按字母顺序排序.

这是我的尝试:

def countLetters(word):
    x = 0
    y = []
    for i in word:
        for j in range(len(y)):
            if i not in y[j]:
                x = (i, word.count(i))
                y.append(x)
    return y
Run Code Online (Sandbox Code Playgroud)

我没先试过它 if i not in y[j]

countLetters("google")
Run Code Online (Sandbox Code Playgroud)

结果是

[('g', 2), ('o', 2), ('o', 2), ('g', 2), ('l', 1), ('e', 1)] 
Run Code Online (Sandbox Code Playgroud)

当我想要的时候

[('e', 1), ('g', 2), ('l', 1), ('o', 2)]
Run Code Online (Sandbox Code Playgroud)

当我添加if i not in y[j]过滤器时,它只返回一个空列表[].

有人可以在这里指出我的错误吗?

python counter dictionary list count

2
推荐指数
1
解决办法
5194
查看次数

在字符串中查找特定单词,Python

创建一个函数,给定两个参数:文本作为字符串,单词作为一组字符串,返回字符串中包含的字数.

例如.count_words("How aresjfhdskfhskd you?",{"how","are","you","hello"})== 3

我的尝试:

import re
def count_words(text, words):
count = 0
for i in words:
    x = re.compile('.*'+i+'.*')
    if x.search(text):
        count +=1
return count
Run Code Online (Sandbox Code Playgroud)

我认为之前已经多次回答,但我找不到相关的答案,所以我道歉.任何帮助将不胜感激,谢谢

python regex

2
推荐指数
1
解决办法
809
查看次数

标签 统计

python ×3

count ×1

counter ×1

d3.js ×1

dictionary ×1

javascript ×1

list ×1

regex ×1

svg ×1