私信  •  关注

Daniel

Daniel 最近回复了
3 年前
回复了 Daniel 创建的主题 » Python中使用缩写的正则表达式

可以将所有月份放在一个正则表达式中:

def find_all_positions(text):
    for match in re.finditer('(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([012][0-9]|30|31)', text):
        yield math.start()
3 年前
回复了 Daniel 创建的主题 » 安装Wordcloud for Python等软件包:需要安装哪些工具?

您是否遵循了Python found安装说明中的信息 here ?

这表明您应该更新setuptools pip install --upgrade setuptools 在做其他事情之前。

5 年前
回复了 Daniel 创建的主题 » 如何用python计算列表中的单词列表

怎么样?

def count_the_fruits(fruits, fruits_to_check_for):

    # initialize variables:
    fruit_count = {}

    # iterate over each fruit in fruit list:
    for fruit in fruits_to_check_for:

        # count number of occurences:
        number_of_fruits = len([x for x in fruits if x==fruit])

        # add to dictionary:
        fruit_count[fruit] = number_of_fruits

    return fruit_count



if __name__ == '__main__':

    fruits = ['apple', 'banana', 'grape',       'kiwi', 
              'banana',  'apple', 'apple', 'watermelon', 
                'kiwi', 'banana', 'apple',]

    fruits_to_check_for = ['apple', 'banana']

    result = count_the_fruits(fruits, fruits_to_check_for)

    print(result)
5 年前
回复了 Daniel 创建的主题 » 在python中,如何基于关键字提取txt文件的一部分?

像这样的怎么样?

# initialize variables:
lines = []
read_lines = False

with open('file.txt', 'r') as file:

    # iterate over each line:    
    for line in file.readlines():

        # append line to lines list:
        if read_lines: lines.append(line)

        # set read_lines to True:
        if '<DOCNO>abc4567890</DOCNO>' in line: read_lines = True

        # set read_lines to Flase:
        if '</DOC>' in line: read_lines = False


# print each line:
for line in lines:
    print(line, end='')

根据您的输入,它将输出:

contents 
more contents
<BODY> 
even more contents 
</BODY>
</DOC> 
5 年前
回复了 Daniel 创建的主题 » python不理解简单的数学问题[closed]

您共享的内容不是有效的python代码。下面是一个代码示例,可以实现您的要求:

# define function:
def CalculateMortgage(p, i, n):

    # calculate numerator:
    numerator = p * (i *(1+i) ** n)

    # calculate denominator:
    denominator = ((1+i) ** n - 1)

    # calculate mortgage:
    mortgage = numerator/denominator 

    # return result:
    return mortgage


# set variables:
p = 1
i = 1
n = 1

# call function:
mortgage = CalculateMortgage(p, i, n)

# print result:
print('Your mortgage is: ' + str(mortgage))
5 年前
回复了 Daniel 创建的主题 » Python陷入无限While循环?

你的程序应该是这样的。使用python时需要注意缩进。

Query = 'Y'

while Query == 'Y':
    Subject = input("Enter the Subject: \n> ")
    CatalogNbr = input("Enter the CatalogNbr: \n> ")
    if Subject == 'LIBS' and CatalogNbr == '150':
        print(f"The title of {Subject, CatalogNbr} is Introduction to Research")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'SDEV' and CatalogNbr == '400':
        print(f"The title of {Subject, CatalogNbr} is Secure Programming in the Cloud")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'PHIL' and CatalogNbr == '348':
        print(f"The title of {Subject, CatalogNbr} is Religions of the East")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'BEHS' and CatalogNbr == '320':
        print(f"The title of {Subject, CatalogNbr} is Disability Studies")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'PSYC' and CatalogNbr == '354':
        print(f"The title of {Subject, CatalogNbr} is Cross-Cultural Psychology")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'SPCH' and CatalogNbr == '482':
        print(f"The title of {Subject, CatalogNbr} is Intercultural Communication")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'WMST' and CatalogNbr == '200':
        print(f"The title of {Subject, CatalogNbr} is Introduction to Womens Studies Women and Society")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'HYST' and CatalogNbr == '482':
        print(f"The title of {Subject, CatalogNbr}is History of Japan to 1800")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'ASDT' and CatalogNbr == '370':
        print(f"The title of {Subject, CatalogNbr} is Interpreting Contemporary China")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

    elif Subject == 'JAPN' and CatalogNbr == '333':
        print(f"The title of {Subject, CatalogNbr} is DJapanese Society and Culture")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")
    else:
        print(f"I'm sorry {Subject, CatalogNbr} is not an avalible option.")

if Query == 'N':
    print("Thank you for using the Catalog Search!")
6 年前
回复了 Daniel 创建的主题 » JavaScript:为MongoDB开发搜索查询

所以以上的答案是最好的解决方案,因为它是ES6和更优雅。我想和大家分享我在得到这个答案后学到的ES5老校风,并对它进行更深入的研究。

module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
  // write a query that will follow sort, offset, limit options only
  // do not worry about criteria yet
  const sortOrder = {};

  Artist.find({})
    .sort()
    .skip(offset)
    .limit(limit);
};

我把它变成了一个空的物体,然后看着它并添加 sortProperty 等于 1 就像这样:

module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
  // write a query that will follow sort, offset, limit options only
  // do not worry about criteria yet
  const sortOrder = {};
  sortOrder[sortProperty] = 1;

  Artist.find({})
    .sort()
    .skip(offset)
    .limit(limit);
};

sortOrder 就像这样:

module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
  // write a query that will follow sort, offset, limit options only
  // do not worry about criteria yet
  const sortOrder = {};
  sortOrder[sortProperty] = 1;

  Artist.find({})
    .sort(sortOrder)
    .skip(offset)
    .limit(limit);
};

好吧,这就是ES5方法,如果有人对它感兴趣的话,但是正如你在上面看到的,ES6解决方案看起来更好。

创建一个对象并同时向其添加一个属性的整个问题是,它在ES5中不受支持,我们必须首先声明一个对象,然后向其添加一个属性并将该属性设置为1。

方括号表示 变量:

sortOrder[sortProperty] = 1;

这是一个字符串,我试图添加 财产 name 设置为 因此,如果在Chrome控制台或代码片段中运行此命令,最终结果是 { name: 1 } .

.sort({ [sortProperty]: 1 })

这里发生的是在运行时查看 排序属性 变量及其等于的值,将该属性添加到此对象,并将其值设置为 1个 .

所以说得很清楚,是的,最初给出的答案是我采用的优雅的解决方案:

module.exports = (criteria, sortProperty, offset = 0, limit = 20) => {
  // write a query that will follow sort, offset, limit options only
  // do not worry about criteria yet
  Artist.find()
    .sort({ [sortProperty]: 1 })
    .skip(offset)
    .limit(limit);
};

我只是分享我在学习和实施过程中学到的东西。

6 年前
回复了 Daniel 创建的主题 » 使用jquery向form追加一个值,并为AJAX Post序列化该表单

问题是你在 价值 您的EmailOrPhoneNumber输入到表单,而不是输入元素本身。

这个 .serialize() 函数只能序列化窗体元素的值。

尝试删除 .val(); 从你的电话号码声明。

6 年前
回复了 Daniel 创建的主题 » 使用python的postgresql连接

您的错误与当前代码无关。 在当前目录中有一个文件 numbers.py 有错误。 不能像python标准模块那样命名文件。重命名 数字.py 以其他名称删除任何 numbers.pyc 文件。

6 年前
回复了 Daniel 创建的主题 » 使用python flask读取多个文件[重复]
length = request.headers["Content-Length"]
data=request.stream.read()

现在,数据是请求主体