Python add a single string to a list - string

I have the following Problem:
I have a list of items, in which the first Word represents the type of something e. .g:
Wall DXU76542
Table Uxitr
Wall rT4
Mobile Tr2
.
.
.
I would like to create another list analogous to this list, extract the single letters at the beginning of every row and add them to the corresponding row of the second list until the space letter " " appears. Thus I can create a second list out of the first list with only types of the items. Here is part of the code in Python (the list: "elements" is a flat list):
for Element in elements:
list1.Add(Element.Name)
list2=[]
list2.append([])
for i in xrange(0,len(list1)):
for j in xrange(0,len(list1[i])):
l=0
while not (list1[i][l]==" "):
list2[i][j].append(list1[i][l])
l = l+1
Output=list2
Does anybody have any idea, why I get the error:
AttributeError: 'str' object has no attribute 'append'
Thank you.

Related

'list' object has no attribute 'split' in an NLP question

While running the following code snippet, I get the following error
'list' object has no attribute 'split'
for i in range(len(questions1)):
# Question strings need to be separated into words
# Each question needs a unique label
questions_labeled.append(TaggedDocument(questions1[i].split(), df[df.index == i].qid1))
questions_labeled.append(LabeledSentence(questions2[i].split(), df[df.index == i].qid2))
if i % 10000 == 0:
progress = i/len(questions1) * 100
print("{}% complete".format(round(progress, 2)))```
Because list has no split() only string objects have split.
The questions1 and questions2 objects seem to hold lists of strings (e.g., questions1 = [['this is a sample text', 'this is another one'],['this is some other text],...]), and not just strings (e.g., questions1 = ['this is a sample text', 'this is another one',...]). Hence the error (i.e., 'list' object has no attribute 'split'), as you are trying to split a list instead of a string. One way to solve this is to create a flast list out of each list of lists, before iterating over them, as described here. For example:
questions1 = [item for sublist in questions1 for item in sublist]
questions2 = [item for sublist in questions2 for item in sublist]

Python remove word from bigram in list without returning a new list

Just quick side question. Is there a way and if, how to remove/delete a specific word from a bigram in a list (must be the same list!) that also contains just words. E.g.
In:
x = ['Peter Parker', 'Hugo', 'Fischerman']
Task, delete Parker from that same list:
Expected output:
x as ['Peter', 'Hugo', 'Fischerman']
I tried to use xx = [x.replace('Parker, '') for x in xx]but it seems to give me a new list in the sack.
Any ideas?
list = ['Peter Parker', 'Hugo', 'Fischerman'] # initialize list
for item in range(len(list)): # loop
list[item] = list[item].replace("Parker", "").strip() # replace item nu=umber "item" with its fixed result, replacing "Parker" with nothing and stripping - this just does nothing if "Parker" is not in item number "item".
That should work, just omit the list initialization to add it wherever (and don't forget to fix the variable names!)

python selenium find_elements incorrect output

I am new to python+selenium. I am trying to return some stuff with find_elements* functions - see code below. When I ask on length of the list, I can see that the number of items is correct, however when I print the content of elements I can see that each element contains the same values and it should contains different values.
elems = browser.find_elements_by_xpath("//div[#class='some_class_name')]")
print(len(elems)) # returns correct number of items
for elem in elems:
print(elem.find_element_by_xpath("//span[#class='another_class_name']").text)
print(elem.find_element_by_xpath("//div[starts-with(#href, 'https://some_web_page_name.com/')]").get_attribute(
'data'))
print(elem.find_element_by_xpath("//div[#class='other_class_name']").text)
You should use . to reduce the scope of xpath to the current element children/grandchildren. In your case, the XPath is pointing to the first element on the page rather finding under the current elem
change code as show below
for elem in elems:
print(elem.find_element_by_xpath(".//span[#class='another_class_name']").text)
print(elem.find_element_by_xpath(".//div[starts-with(#href, 'https://some_web_page_name.com/')]").get_attribute(
'data'))
print(elem.find_element_by_xpath(".//div[#class='other_class_name']").text)
While looping or using find_element inside anther selector you have to add "." inside selector otherwise it will print same element multiple times
first_element = browser.find_elements_by_xpath("//div[#class='some_class_name')]")[0].find_element_by_xpath(".//span[#class='another_class_name']").text
In your case use following code
elems = browser.find_elements_by_xpath("//div[#class='some_class_name')]")
print(len(elems)) # returns correct number of items
for elem in elems:
print(elem.find_element_by_xpath(".//span[#class='another_class_name']").text)
print(elem.find_element_by_xpath(".//div[starts-with(#href, 'https://some_web_page_name.com/')]").get_attribute('data'))
print(elem.find_element_by_xpath(".//div[#class='other_class_name']").text)

how to separate the list into nested list ,with the average of all elements in the list?

i need to separate the list into nested list with its average value.
a =[ 0.6140781, 0.61407846, 0.6930427, 0.6930429, 0.7213439, 0.72134393, 0.7333274, 0.73332757]
the average of the list is 0.05515
if the difference between two elements is not more than 0.5515 ,i need to join the elements in the list and if the next element in list exceeds 0.05515 then form the another list in python
desired output:
output : [[0.6140781, 0.61407846],[0.6930427, 0.69460429],[0.7213439, 0.72334393], [0.7333274, 0.73532757]]
Any suggestions would be helpful!
Since the list is sorted, you can iterate through, if the current element is greater than target value, add a new list to the output, and add the current element to the most recently added list.
Your output doesn't make sense with the average number you provided because 0.7213439 - 0.69460429 is not greater than 0.05515, neither is 0.7333274 - 0.72334393 so I used 0.01 instead.
a = [0.6140781, 0.61407846, 0.6930427, 0.6930429, 0.7213439, 0.72134393, 0.7333274, 0.73332757]
output = [a[:1]]
for i in range(1, len(a)):
if a[i] - a[i-1] > 0.01:
output.append([])
output[-1].append(a[i])
print(output)

Function to iterate through a nested list and append other lists isn't functioning properly

I am currently trying to write a function to iterate through a nested list and check if one item from the list, 'team', is already in a separate list 'teams'.
If it is not, I want to append a nested list, 'player_values' with a different item from the original nested list that was examined, in the form of a new list in the nested list.
If it is, I want to append the nested list 'player_values' with the item from the original nested list, but I want to add it to the most recent list in the nested list 'player_values' instead of creating a new list.
Currently, my code looks like this :
def teams_and_games(list, player, idx):
teams = []
player_values = []
x = 0
y = -1
for rows in list:
if player == list[x][BD.player_id] and list[x][BD.team] not in teams:
teams.append(list[x][BD.team])
player_values.append([list[x][idx]])
x += 1
y += 1
elif player == list[x][BD.player_id]:
player_values[y].append(list[x][idx])
x += 1
return player_values, teams
However, when I run the code in my main, using
values, teams = teams_and_games(NiceRow, name, BD.games)
print(values)
print(teams)
It only prints empty lists. The fact that it prints empty lists shows that it is returning the correct variables, but I can't figure out why the code in the function is failing to add anything to the lists. I have tried switching the .append with a more simple list += statement, but the result has been the same so far.
Ideally, I would be getting a nested list, containing an amount of lists equal to the number of items added to the other 'teams' list, and the list of teams in the order they were added.
The data I am working with is a nested list pulled from a .csv file, which has been formatted slightly using the .strip() and .split() commands. Each number has been converted to an int, and strings left as they are. The .CSV file it is from has 19 columns and ~80,000 rows, with each column always being either a string or an int.

Resources