I have a problem while creating an empty list in Python3.6.3 with list() (I work with Pycharm - community 2019.3)
The strange thing is that if I do it in terminal (or if I clear all the script code but this line) it works, but if I use the same line in a script, it's highlighted as an error and running it would lead to TypeError: 'list' object is not callable
The code up to the point it fails is is:
list = open('intro.txt').read().split()
alph = "abcdefgjklmn'opqrstuvwxyz"
alph = alph + alph.upper()
clean_list = list()
The 4th line is the one that fails, and it does the same everytime I generate an empty list this way..
Btw I have no problem by doing clean_list = [ ] but this errror annoys me a lot
Thanks!
You already have a variable called "list" which Python won't like if used in the same script as list(). So try renaming the the first variable.
Related
I'm trying to open a txt file and get all the words in it, assign it to a list, and filter the list of duplicates. The code in the snippet is what I thought would work, but I'm getting a traceback error and it says list index is out of range. How can I modify my code to avoid that error?
Any help is appreciated.
fname = input("Enter file name: ")
fh = open("romeo.txt")
lst = list()
for line in fh:
nlst = (line.rstrip()).split()
lst = lst + nlst
for i in [*range(len(lst))]:
if lst.count(lst[i]) > 1:
while lst.count(lst[i]) > 1:
print(lst[i])
lst.remove(lst[i])
else:
continue
print(lst)
edit 1:
Okay so I thought the cause of the problem was lst.count(lst[i]) having a value of one or more and the inequality is (>1) so it's saying that it's out of range, but I tried 0 instead of 1, and it still gave me the same error.
vsc snippet
but i'm getting a traceback error and it says list index is out of range
First, whenever you're asking about a traceback, include the actual error message in your question. In this case, that looks like:
Traceback (most recent call last):
File ".../countwords.py", line 9, in <module>
if lst.count(lst[i]) > 1:
~~~^^^
IndexError: list index out of range
There are several issues that get us into this situation. Broadly, you're iterating over the number of words in the document. For each word, you're using lst.count to find occurrences of the word in lst...and then removing duplicates. Whenever you find a duplicate, lst gets shorter, but your outer loop doesn't know this. Eventually, you ask for lst[i] when i is larger than the length of the list and your code explodes.
We can fix this while preserving your current logic by making a couple of changes.
First, let's fix that outer loop; you've written for i in [*range(len(lst)], but that's operationally equivalent to for i in range(lst), which is simpler to read.
Instead of trying to update lst, let's populate a new variable lst_no_dupes; this avoids issues with modifying lst while at the same time trying to iterate over it:
Instead of using lst.count, we can use the in operator to check for the presence of a word in a list.
Those changes get us:
lst_no_dupes = []
for i in range(len(lst)):
if lst[i] not in lst_no_dupes:
lst_no_dupes.append(lst[i])
print(lst_no_dupes)
This works, but it's inefficient, because checking to see if a word is contained in the list is an O(n) operation. As the number of words grows larger, it will take longer and longer to look up items in the list.
There's a much simpler way to produce a list of unique items from a list as long as you don't care about their order: just turn them into a set:
fh = open("romeo.txt")
lst = list()
for line in fh:
nlst = (line.rstrip()).split()
lst = lst + nlst
lst = set(lst)
print(lst)
A set is "an unordered collection of distinct objects"; lookups in a set are O(1) so the time required to check for a duplicate is independent of the number of words.
Ultimately, you could simplify everything down to this:
with open('romeo.txt') as fh:
lst = set(word for line in fh for word in line.split())
print(lst)
Here, we're iterating over lines in the file (for line in fd) and then words in each line (for word in line.split()). This is an example of a generator expression (which is like a list comprehension).
I'm new bee in python 3 and stuck here to remove \n while compiling code as given below, want to return two random lines with out printing \n and w/o square bracket [ ], what should i do?
code is
import random
def head():
f = open("quotes.txt")
quotes = f.readlines()
f.close()
last=18
print(random.sample(quotes,2))
if __name__== "__main__":
head()
And executed this file the result returned as selected two random lines it is fine for me, but in the format like this included \n
['IMPOSSIBLE says itself I M POSSIBLE\n', 'Never stops to Learning till dead end\n']
You are getting results like ['IMPOSSIBLE says itself I M POSSIBLE\n', 'Never stops to Learning till dead end\n'] is because it is list and you directly printing list as it is.
Solution
Remove print(random.sample(quotes,2)) and add following code
tmp = random.sample(quotes,2)
for i in tmp:
print(i,end="")
This will solve your problem and end in print is because your quotes already has newline so we are preventing print from inserting extra \n.
It's resolved!!!
I ran the code by typing command python which it was taken as python 2.7 and returned as this type of junk result, but it works fine as executed with python3 command.
I am trying to create a function that will add two list such that if list1 is [9,1,2] and list2 is [8,5,3] then the two lists added together would produce a list yielding. ]1,7,6,5] since 912+853= 1765.
The following is the code I have written:
def list_addition(list1,list2):
otlist1=''
otlist2=''
for items1 in list1:
otlist1+= items1
for items2 in otlist2:
otlist2+= items2
strinum = int(otlist1)+ int(otlist2)
return strinum
print(list_addition(['3','6','7'], ['4','9','0']))
I keep getting this error:
Traceback (most recent call last):
File "C:/Users/Chuck/PycharmProjects/arrayaddition/Arrays.py", line 13, in <module>
list_addition(['3','6','7'], ['4','9','0'])
File "C:/Users/Chuck/PycharmProjects/arrayaddition/Arrays.py", line 10, in list_addition
strinum = int(otlist1)+ int(otlist2)
ValueError: invalid literal for int() with base 10: ''
I obviously know my code even if it did work as written wouldn't be complete as I would still need to put in the final codes to convert the integer variable 'strinum' back to a list, but I can't get there if my code is failing to properly add the two converted lists. When I break down the code and write the two for loops separately, convert them to integers and add them everything works perfectly fine. So the code below was good:
list1=['7','9','6']
otlist1=''
for items1 in list1:
otlist1+= items1
print(otlist1)
ist1=['5','7','0']
otlist2=''
for items1 in ist1:
otlist2+= items1
print(otlist2)
print(int(otlist1) + int(otlist2))
But for some reason when I try to put the two for loops inside a single function I get the error. I am a complete newbie to programming and I want to know what I am not understanding about function syntax. Any help will be greatly appreciated.
I am new to Python coding. I am able to create the output XML file. I want to use a variable which holds a string value and pass it to 'predicate' of 'find()'. Is this achievable? How to make this work?
I am using LXML package with Python 3.6. Below is my code. Area of problem is commented at the end of the code.
import lxml.etree as ET
# Create root element
root = ET.Element("Base", attrib={'Name': 'My Base Node'})
# Create first child element
FirstElement = ET.SubElement(root, "FirstNode", attrib={'Name': 'My First Node', 'Comment':'Hello'})
# Create second child element
SecondElement = ET.SubElement(FirstElement, "SecondNode", attrib={'Name': 'My Second Node', 'Comment': 'World'})
# Create XML file
XML_data_as_string = ET.tostring(root, encoding='utf8')
with open("TestFile.xml", "wb") as f:
f.write(XML_data_as_string)
# Variable to substitute in second portion of predicate
NewValue = "My Second Node"
# #### AREA OF PROBLEM ###
# Question. How to pass variable 'NewValue' in the predicate?
# Gives "SyntaxError: invalid predicate"
x = root.find("./FirstNode/SecondNode[#Name={subs}]".format(subs=NewValue))
# I commented above line and reexecuted the code with this below line
# enabled. It gave "ValueError: empty namespace prefix must be passed as None,
# not the empty string"
x = root.find("./FirstNode/SecondNode[#Name=%s]", NewValue)
As Daniel Haley said - you're missing a single quotes in #Name={subs}.
The following line works for me:
x = root.find("./FirstNode/SecondNode[#Name='{subs}']".format(subs=NewValue))
Since you use Python 3.6, you can utilize f-strings:
x = root.find(f"./FirstNode/SecondNode[#Name='{NewValue}']")
The "proper" way to solve this would be to use XPath variables, which are not supported by find() (and consequently, aren't supported by xml.etree from the standard library either) but are supported by xpath().
NewValue = "AJL's Second Node" # Uh oh, that apostrophe is going to break something!!
x_list = root.xpath("./FirstNode/SecondNode[#Name=$subs]", subs=NewValue)
x = x_list[0]
This avoids any sort of issue you might otherwise run into with quoting and escaping.
The main caveat of this method is namespace support, since it doesn't use the bracket syntax of find.
x = root.find("./{foobar.xsd}FirstNode")
# Brackets are doubled to avoid conflicting with `.format()`
x = root.find("./{{foobar.xsd}}FirstNode/SecondNode[#Name='{subs}']".format(subs=NewValue))
Instead, you must specify those in a separate dict:
ns_list = {'hello':'foobar.xsd'}
x_list = root.xpath("./hello:FirstNode/SecondNode[#Name=$subs]", namespaces=ns_list , subs=NewValue)
x = x_list[0]
I am new to Python. I am here reading several lines in a file having a format
121212-pr-ba-su_re;m1;1;10000
I have extracted (pr-ba-su) content using split function. Please see the code which i wrote. I wish to store the Prefix-base-suffix as a key and its value as ID (which is extracted in the first line of loop) in a dictionary. (I want to replace the key with value in another list. Therefore, i am trying to create a dictionary.)
data ={}
f = open('var_test.txt')
for line in f:
part,m,t,ID = line.split(';',3)
partnumb,r = part.split('_',1)
item,prefix,base,suffix =partnumb.split('-',3)
data[prefix'-'base'-'suffix] =ID
But i am getting an error as Syntax Error
data(getattr([prefix'-'base'-'suffix])) =PID
^SyntaxError: invalid syntax
I have tried using getattr()function,and update function but no avail.Can anybody help me? Thanks a lot.
The line data[prefix'-'base'-'suffix] =ID is your problem. It is not valid Python syntax.
You could get the results you expect by substituting for this line
data["{}-{}-{}".format(prefix, base, suffix)] = ID