社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Adam Smith

Adam Smith 最近创建的主题
Adam Smith 最近回复了
6 年前
回复了 Adam Smith 创建的主题 » python版本困境[待定]

一般来说,当开始一个新项目时,您应该在 所有依赖项都支持的最新版本的python。

因为我们不知道你的项目是什么,我们只能根据你如何标记问题来猜测。包括 让我觉得你可能会 Tensorflow (机器学习库)只支持Python3.6。

6 年前
回复了 Adam Smith 创建的主题 » python中用逐位查询法生成长度为l的三元数列表

itertools.product 似乎给了你想要的东西。它经常用来代替嵌套 for 循环,但有一个方便的 repeat 这让你在这里的生活更轻松。

l = 3  # that's a lower-case L. Never use that in code, though, it looks like a 1.

digits = itertools.product(range(3), repeat=l)

# is equivalent to

def my_product():
    """the same as above itertools.product if l==3"""
    for i in range(3):
        for j in range(3):
            for k in range(3):
                yield (i, j, k)
my_digits = my_product()  # YUCK!

这会产生 发电机 (注意:不是列表!)它产生了你想要的所有价值观 (0, 0, 0) (2, 2, 2) . 要列出一个列表,只需将其转换为一个。

digits = list(itertools.product(range(3), repeat=l))  # still a lower-case L. Still don't do this.

然后要比较数字,只需像任何二维列表一样使用索引。

first_value = digits[0]
first_digit = first_value[0]
assert first_digit == digits[0][0]

second_value = digits[1]
first_digit_of_second_value = second_value[0]
assert first_digit_of_second_value == digits[1][0]

if digits[0][0] == digits[1][0]:
    # that's these two:  v          v
    # digits ==         (0, 0, 0), (0, 0, 1), (0, 0, 2), ...
    do_whatever_you_want()

如果你想特别输出 (0, 0, 0) 作为 000 ,您可以为此编写函数:

def sprint_tuple(tup):
    """Takes a tuple of digits and pretty Sprints them.

    >>> sprint_tuple((0, 0, 1))
    '001'
    """

    return ''.join([str(i) for i in tup])

然后把你的 digits 并分别打印:

>>> for tup in digits:
...     print(sprint_tuple(tup))
000
001
002
010
...
222
6 年前
回复了 Adam Smith 创建的主题 » python测试程序-读取csv并解析问题/答案

这里有一个stdlib库: csv .

import csv

with open("path/to/your/quizfile.csv") as f:
    reader = csv.reader(f)
    quiz_qas = list(reader)  # this could get *very* large -- take care if you have a large file.

q, a = random.choice(quiz_qas)
print(q)
answer = input(">>> ")
if answer == a:
    # user got the right answer
else:
    # user failed

random.sample 甚至可以让你随机选择一个k大小的样本 quiz_qas .

quiz = itertools.sample(quiz_qas, num_questions)
for q, a in quiz:
    # etc
6 年前
回复了 Adam Smith 创建的主题 » python:如何迭代一个n维矩阵?

如果我理解正确:

def give_counts(out):
    """Count the fourth element of each cell if it is >0.5

    >>> x = [0, 0, 0, 3]  # should show up in counts1
    >>> o = [0, 0, 0, 0]  # should show up in counts2
    >>> out = [[x, o, o],
    ...        [x, x, x],
    ...        [o, o, x]]
    >>> counts1, counts2 = give_counts(out)
    >>> assert counts1 == 5
    >>> assert counts2 == 4
    """

    values = (True if col[3] > 0.5 else False for row in out for col in row)
    counts = collections.Counter(values)
    return counts[True], counts[False]
6 年前
回复了 Adam Smith 创建的主题 » 在这种情况下,python如何避免无限递归?

蟒蛇 import 语句很聪明,可以跟踪已经导入的内容。

在您的程序是几个需要stdlib模块的文件的普通情况下( os 例如,这节省了开销,因为您的程序在不重新导入所有重复模块的情况下导入自己。

foo3 ,巨蟒知道 三氧化二铝 已导入,不再执行导入操作。

事实上,它被加载两次,一次作为 __main__ 一次 三氧化二铝 .