小编Cha*_*ith的帖子

在布尔列表中获取True值的索引

我有一段我的代码,我应该创建一个交换机.我想返回所有开关的列表.这里"开"将等于True和"关"相等False.所以现在我只想返回所有True值及其位置的列表.这就是我所有但它只返回第一次出现的位置True(这只是我代码的一部分):

self.states = [False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False]

def which_switch(self):
    x = [self.states.index(i) for i in self.states if i == True]
Run Code Online (Sandbox Code Playgroud)

这只返回"4"

python list

63
推荐指数
3
解决办法
10万
查看次数

AWS:引擎名称 postgres 的存储大小无效

我在亚马逊有一个 RDS 实例,我想修改分配的存储空间,它目前为 500GB,但我希望它为 50GB。但是,当我尝试在控制台中更改此设置时,我收到以下错误:Invalid storage size for engine name postgres and storage type standard: 60

我有一个没有与之关联的 RDS 实例的 Elasticbeanstalk 应用程序,在阅读之后我读到有些人需要一个与您尝试修改的 RDS 具有相同规格的实例,但是这也不起作用.

这里有什么帮助吗?

amazon-web-services amazon-rds

9
推荐指数
2
解决办法
7560
查看次数

Django OAuth Toolkit为我提供了"未提供身份验证凭据"错误

我已经尝试了一段时间让django Oauth工具包工作,但不管我得到401错误,当我检查细节时,它是标题中的错误.即使我显然传递凭证

views.py:

class PostCarFax(viewsets.ModelViewSet):

    #authentication_classes = [authentication.TokenAuthentication, authentication.SessionAuthentication, authentication.BaseAuthentication]
    permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
    queryset = CarFax.objects.all()
    serializer_class = CarFaxSerializer
Run Code Online (Sandbox Code Playgroud)

serializers.py:

class CarFaxSerializer(serializers.ModelSerializer):

    class Meta:
        model = CarFax
        fields = ('vin', 'structural_damage', 'total_loss',
                  'accident', 'airbags', 'odometer', 'recalls',
                  'last_updated', 'origin_country')

    def create(self, validated_data):
        return CarFax.objects.create(**validated_data)
Run Code Online (Sandbox Code Playgroud)

settings.py

import os
import django_heroku
from django.conf import settings
settings.configure()

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY …
Run Code Online (Sandbox Code Playgroud)

python django

7
推荐指数
2
解决办法
297
查看次数

如何阻止 Pandas 自动转换我的日期格式?

我有一个 Excel 文件,日期的存储方式如下: Saturday November 30, 2019

但是,当我将 Excel 文件读入数据帧时,日期将转换为以下格式:

Date Deer Lake Dartmouth Grand Falls 2019-11-30 0.7917 0.7663 0.7805

为什么会发生这种情况?pandas 是否可以选择阻止任何重新格式化?

我如何读取Excel文件:df = pd.read_excel("RunningTotals_test.xlsx", keep_default_na=False, sheet_name=sheet_name)

python dataframe pandas

7
推荐指数
1
解决办法
9110
查看次数

“{%”在 HTML 中有什么作用?

我已经使用官方文档向我的应用程序添加了 Django 消息。它说在我的模板中添加这样的东西:

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

我不知道百分比符号是做什么的,它们不是真正的 HTML,对吗?

python django django-templates

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

如何将raw_input()与while循环一起使用

只是尝试编写一个程序,将用户输入并将其添加到列表'数字':

print "Going to test my knowledge here"
print "Enter a number between 1 and 20:"

i = raw_input('>> ')
numbers = []

while 1 <= i <= 20 :
    print "Ok adding %d to numbers set: " % i 
    numbers.append(i)

    print "Okay the numbers set is now: " , numbers
Run Code Online (Sandbox Code Playgroud)

但是,当我执行程序时,它只运行到raw_input()

Going to test my knowledge here
Enter a number between 1 and 20:
>>> 4
Run Code Online (Sandbox Code Playgroud)

我在这里缺少一些基本规则吗?

python raw-input while-loop

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

使用带有while循环的字典

我试图在while-loops条件语句中使用字典键,程序将不会将任何输入识别为"正确"

class Gymnast_room(KeyCode):    
    def enter(self):
        print "This is the gymnastics room, we got foam pits and rings!"
        print "Before you can enter, you have to enter the code"
        guess = int(raw_input('What is the code? >>'))

        passcodes = {'Gymnast_code': 1234,
        'Powerlifting_code': 1234
        }
        while guess != passcodes['Gymnast_code']:
            guess = int(raw_input('What is the code? >>'))
            if guess == passcodes['Gymnast_code']:
                print "Correct!"
                return Powerlifting_room()
            else:
                print "Incorrect!"
Run Code Online (Sandbox Code Playgroud)

python dictionary while-loop

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

返回一行中的单词列表但忽略某些空格

说我有线:

235Carling             Robert         140 Simpson Ave     Toronto        Ont M6T9H1416/247-2538416/889-6178
Run Code Online (Sandbox Code Playgroud)

你看到每个角色的集合?我希望那些代表数据文件中的列.我遇到的问题是"街道地址"栏目.

for i in master_file:
    #returns a list of the words, splitting at whitespace
    columns = i.split()
Run Code Online (Sandbox Code Playgroud)

问题是虽然这会分成140 Simpson Ave三个"单词".是否有一种方法可以用来说只有单词如果被一定数量的空格或某些东西包围?

python string

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

退出for循环并在文件末尾返回

我想遍历一个文件并返回每一行:

for i in master_file:
        columns = re.split('\s{2,}', i)
        last_name = columns[1]
        print(last_name)
Run Code Online (Sandbox Code Playgroud)

如果我更换printreturn它只会返回columns[1]的第一线,但我想返回它的所有行.所以我可以在我的函数中的其他地方使用该信息.

python for-loop

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

不断变换的排列

我正在阅读这篇文章,试图了解有关排列的更多信息:在python中查找给定字符串的所有可能排列

我被困在这段代码上:

def permutations(string, step = 0):

    # if we've gotten to the end, print the permutation
    if step == len(string):
        print "".join(string)

    # everything to the right of step has not been swapped yet
    for i in range(step, len(string)):

        # copy the string (store as array)
        string_copy = [character for character in string]

        # swap the current index with the step
        string_copy[step], string_copy[i] = string_copy[i], string_copy[step]

        # recurse on the portion of the string that has not been …
Run Code Online (Sandbox Code Playgroud)

python permutation

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