小编Geo*_*ows的帖子

从字符串列表的元素中删除尾随换行符

我必须在表格中列出大量的单词:

['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
Run Code Online (Sandbox Code Playgroud)

然后使用条带功能,将其转换为:

['this', 'is', 'a', 'list', 'of', 'words']
Run Code Online (Sandbox Code Playgroud)

我以为我写的东西会起作用,但我一直都会收到错误说:

"'list'对象没有属性'strip'"

这是我试过的代码:

strip_list = []
for lengths in range(1,20):
    strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
    strip_list.append(lines[a].strip())
Run Code Online (Sandbox Code Playgroud)

python list strip

111
推荐指数
4
解决办法
17万
查看次数

Python中是否有内置的product()?

我一直在查看教程和书籍,但我没有提到内置的产品功能,即与sum()类型相同,但我找不到任何类似的东西prod().

我是唯一能通过导入mul()运营商找到列表中项目的产品的方法吗?

python product list

78
推荐指数
4
解决办法
6万
查看次数

更改条形图中特定条形的颜色

我一直在创建一些条形图,我想知道是否可以在图表上显示颜色条,这取决于它们是位于x轴的上方还是下方?

为了澄清,这是我所说的条形图类型:

在此输入图像描述

理想情况下,我希望能够将单独颜色上方的条形颜色设置为下方,以便图形看起来更具吸引力,我一直在寻找,但我找不到任何方法这样做,任何人都可以帮忙吗?

提前致谢.:)

r colors bar-chart

17
推荐指数
3
解决办法
5万
查看次数

循环直到特定用户输入

我正在尝试编写一个数字猜测程序,如下所示:

def oracle():
n = ' '
print 'Start number = 50'
guess = 50 #Sets 50 as a starting number
n = raw_input("\n\nTrue, False or Correct?: ")
while True:
    if n == 'True':
        guess = guess + int(guess/5)
        print
        print 'What about',guess, '?'
        break
    elif n == 'False':
        guess = guess - int(guess/5)
        print
        print 'What about',guess, '?'
        break
    elif n == 'Correct':
        print 'Success!, your number is approximately equal to:', guess
Run Code Online (Sandbox Code Playgroud)

甲骨文()

我现在要做的是让这个if/elif/else命令序列循环,直到用户输入'Correct',即当程序声明的数字大约等于用户数时,但是如果我不知道用户编号我想不出我可以如何实现和if语句,我尝试使用'while'也不起作用.

python loops numbers

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

在Python中添加两个列表

我试图将两个列表添加在一起,因此一个列表的第一项添加到另一个列表的第一项,第二项添加到第二项,依此类推以形成新列表.

目前我有:

def zipper(a,b):
    list = [a[i] + b[i] for i in range(len(a))]
    print 'The combined list of a and b is'
    print list

a = input("\n\nInsert a list:")
b = input("\n\nInsert another list of equal length:")

zipper(a,b)
Run Code Online (Sandbox Code Playgroud)

在输入两个列表时,其中一个是整数列表,一个是字符串列表,我得到类型错误'不能cocanenate'str'和'int'对象.

我尝试使用以下命令将两个列表转换为字符串

list = [str(a[i]) + str(b[i]) for i in range(len(a))]
Run Code Online (Sandbox Code Playgroud)

但是在进入时:

a = ['a','b','c','d']
b = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

我得到的输出为:

[ 'A1', 'B2', 'C3', 'D4']

而不是我想要的是:

['a+1','b+2','c+3','d+4']
Run Code Online (Sandbox Code Playgroud)

有没有人对我做错了什么有任何建议?

注意我必须编写一个与zip(a,b)基本相同的函数,但我不允许在函数的任何地方使用zip().

python string int list

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

NumPy - 使用isnan(x)

我正在尝试使用numpy检查用户输入是否为数字,我尝试过使用:

from numpy import *

a = input("\n\nInsert A: ")

if isnan(a) == True:
    print 'Not a number...'
else:
    print "Yep,that's a number"
Run Code Online (Sandbox Code Playgroud)

它本身就可以正常工作,但是当我将它嵌入到一个函数中时,例如:

来自numpy import*

def test_this(a):

    if isnan(a) == True:
        print '\n\nThis is not an accepted type of input for A\n\n'
        raise ValueError
    else:
        print "Yep,that's a number"

a = input("\n\nInsert A: ")

test_this(a)
Run Code Online (Sandbox Code Playgroud)

然后我得到一个NotImplementationError,说它没有为这种类型实现,任何人都可以解释这是如何工作的?

任何帮助将不胜感激,再次感谢.

python numpy notimplementedexception

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

查找两个列表中匹配组件的总和

我有两个清单:

A = [1, 2, 3, 4, 5]
B = [6, 7, 8, 9, 10]
Run Code Online (Sandbox Code Playgroud)

我需要能够从两个列表中找到第n个术语的总和,即1 + 6,2 + 7,3 + 8等

有人可以告诉我如何同时参考两个列表中的项目吗?

我读到某个地方,我可以做Sum = a [i] + b [i],但我不相信它会如何起作用.

python sum list

4
推荐指数
2
解决办法
7501
查看次数

将字符串句子列表转换为单词

我试图基本上采用包含句子的字符串列表,例如:

sentence = ['Here is an example of what I am working with', 'But I need to change the format', 'to something more useable']
Run Code Online (Sandbox Code Playgroud)

并将其转换为以下内容:

word_list = ['Here', 'is', 'an', 'example', 'of', 'what', 'I', 'am',
'working', 'with', 'But', 'I', 'need', 'to', 'change', 'the format',
'to', 'something', 'more', 'useable']
Run Code Online (Sandbox Code Playgroud)

我试过用这个:

for item in sentence:
    for word in item:
        word_list.append(word)
Run Code Online (Sandbox Code Playgroud)

我认为它会占用每个字符串并将该字符串的每个项目附加到word_list,但输出的内容如下:

word_list = ['H', 'e', 'r', 'e', ' ', 'i', 's' .....etc]
Run Code Online (Sandbox Code Playgroud)

我知道我犯了一个愚蠢的错误,但我无法弄清楚为什么,任何人都可以帮忙吗?

python string sentence

4
推荐指数
2
解决办法
2万
查看次数

打印直方图

我有一个整数百分比列表,我需要使用以下模式打印:

The index of a value, a tab (8 spaces), a '*' printed for each percentage point
Run Code Online (Sandbox Code Playgroud)

如果索引的值为0,则打印'小于1%'

我试过这段代码:

for b in new_tally:
    if b > 0:
        print new_tally[b], \t, '*' * b
    else:
        print 'Less than 1% of words had this length'
Run Code Online (Sandbox Code Playgroud)

但是我一直收到错误代码:列表索引超出范围.

我根本不明白这一点,有人能指出我做错了什么吗?

python indexing list histogram traceback

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

这个结果出了什么问题?

我正在阅读一个关于定义函数的教程,其中有一个关于创建新行的示例,您必须编写的代码是:

def new_line():
    print

def three_lines():
    new_line()
    new_line()
    new_line()

def nine_lines():
    three_lines()
    three_lines()
    three_lines()

print "hello world"

print nine_lines()

print "goodbye"
Run Code Online (Sandbox Code Playgroud)

任何人都能解释为什么它在'再见'之前包含'无'?

python input delay

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