我必须在表格中列出大量的单词:
['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) 我一直在查看教程和书籍,但我没有提到内置的产品功能,即与sum()类型相同,但我找不到任何类似的东西prod().
我是唯一能通过导入mul()运营商找到列表中项目的产品的方法吗?
我一直在创建一些条形图,我想知道是否可以在图表上显示颜色条,这取决于它们是位于x轴的上方还是下方?
为了澄清,这是我所说的条形图类型:

理想情况下,我希望能够将单独颜色上方的条形颜色设置为下方,以便图形看起来更具吸引力,我一直在寻找,但我找不到任何方法这样做,任何人都可以帮忙吗?
提前致谢.:)
我正在尝试编写一个数字猜测程序,如下所示:
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'也不起作用.
我试图将两个列表添加在一起,因此一个列表的第一项添加到另一个列表的第一项,第二项添加到第二项,依此类推以形成新列表.
目前我有:
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().
我正在尝试使用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,说它没有为这种类型实现,任何人都可以解释这是如何工作的?
任何帮助将不胜感激,再次感谢.
我有两个清单:
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],但我不相信它会如何起作用.
我试图基本上采用包含句子的字符串列表,例如:
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,但输出的内容如下:
Run Code Online (Sandbox Code Playgroud)word_list = ['H', 'e', 'r', 'e', ' ', 'i', 's' .....etc]
我知道我犯了一个愚蠢的错误,但我无法弄清楚为什么,任何人都可以帮忙吗?
我有一个整数百分比列表,我需要使用以下模式打印:
Run Code Online (Sandbox Code Playgroud)The index of a value, a tab (8 spaces), a '*' printed for each percentage point
如果索引的值为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)
但是我一直收到错误代码:列表索引超出范围.
我根本不明白这一点,有人能指出我做错了什么吗?
我正在阅读一个关于定义函数的教程,其中有一个关于创建新行的示例,您必须编写的代码是:
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)
任何人都能解释为什么它在'再见'之前包含'无'?