using split for appending to multiple lists [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to combine the two statements inside 'for' loop.
num_pro=raw_input("ENTER THE NUMBER OF PRODUCTIONS: ")
right=[];left=[];
for i in range(int(num_pro)):
l,r=raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->')
right.append(r);left.append(l)
sample input: E->abc

Append tuples to one list, then split out the lists using zip():
entries = []
for i in range(int(num_pro)):
entries.append(raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->'))
left, right = zip(*entries)
zip(*iterable) transposes the nested list; columns become rows. Because you have two 'columns' (pairs of values), you end up with two rows instead.

Not without making it more complex. Each method needs to be called individually, and the only way to do that is either explicitly, as you have done, or in a loop.
If you are willing to store the whole production (which isn't necessarily a bad idea, since it keeps both sides synchronized) then just append the split result instead.
productions = []
for ...
productions.append(....split('->'))

Related

How do you sort a Python list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a list in Python of scores that are integers. I want to sort them from lowest to highest. I have tried looking it up, but I find that I don’t understand what most of them mean. I have heard of sorted(), but I’m not sure how it works. How can I sort a list of numbers? Will sorted() help?
Try sorted(scoreList).
Get the reference here.
Some problems with your approach:
You're not defining a scoreList variable in the code you posted. Maybe you want to pass it as parameter:
def sortList(scoreList):
You access the index of a list using [], and not ():
if scoreList[w] < scoreList[all]:
You don't check if all elements are greater than an element using [all]. Instead, use the all function:
if all(scoreList[w] < element for element in scoreList):
You assign a variable using =, and not ==:
scoreList[w] = scoreList[1]
Your logic is also wrong, there are some basic algorithms you can look up if you want to do it by hand, such as bubble sort or insertion sort.
Of course, there's always the sorted function.
I'm not sure what scoreList is since you used brackets after it.
If it's a list it should be done as [].
Assuming scoreList is a list:
scoreList = [3,5,67,83,555,38,22,1]
sortedList = []
for i in range(len(scoreList)):
sortedList.append(max(scoreList))
scoreList.remove(max(scoreList))
print(sortedList[::-1])
Remove [::-1] if you want in descending order.
This is of course you don't want to use scoreList.sort()

How To Remove an Element from a List Using Another List's Placeholder in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num] from everyone.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list because it is reset multiple times as everyone in list everyone takes a turn.
I tried this question, but it only worked from removing entire lists from eachother. Is there a way I could change it to work for this case?
Thanks for your time!
everyone.remove(enemy_list[num])

how to find element that has no duplicate number? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need your help here.
by use Python, I want to find a way to filter out element in list that has no double integer(at least 2 continuous number).
like in [314120,420423,432192,444689,112345], I want filter out 314120 and 420423,432192.
because 444689 has 444 and 112345 has 11. so they shall not be filter out as a expected result.
thank you
You could simply use something like this:
import re
result = [x for x in numbers if !re.search(r"(.)\1", str(x))]
This document represents a way to check the uniqueness of characters. Suppose isUniqueChars is the method and numbers is your list of integers, you can find the one which all its numbers are unique by following piece of code:
unique = [n for n in numbers if isUniqueChars(str(n))]

List comprehension example I don't understand [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm having trouble understanding a particular list comprehension example. In the sample code below a list comprehension is used for the parameter of the set method. Most examples I've seen usually look like this "x for x in y". I've read documentation but it isn't clearing it up for me. What exactly is [marks for name] doing. Shouldn't it be [marks for marks]?
marksheet = []
for _ in range(0,int(input())):
marksheet.append([input(), float(input())])
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
Your code is using Python's list comprehension to create a list from another list based on the condition.
See more about list comprehensions here:
https://www.programiz.com/python-programming/list-comprehension
The [1] is accessing the item at index 1 (2nd item) of your sorted list.
As for .join, each line in the sorted list is being printed out with a newline (\n) between each one.

Python, How do I concatenate Strings and Array of arrays? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The following is what I want to do (but I know it does not work).
msg["X-RECIP-ID"] = emailData['campaignId'] + "-" emailData['listId'] + "-" emailData['emailId'])
I know join(array, "-"), can do it for an array [campaignId, listId, emailId], but what I have currently have afaik is an array in an array.
What would be Pythonic way to do what I'm trying to do?
What you have with emailData isn't an array, or an array of arrays (or even a list of lists), but instead it's a Dictionary (see https://docs.python.org/2/tutorial/datastructures.html#dictionaries )
What I think you want to do is pull out certain values out of this dictionary into a new list, and then use the join method to put them all together with a "-" between them.
msgElems = [emailData[i] for i in ['campaignId', 'listId', 'emailId']]
msg["X-RECIP-ID"] = "-".join(msgElems)

Categories