How to pass command line to save into list - python

How to pass command line to save into list
Disclaimer Please dont say print (ARGV1[0])
import sys
ARGV1 = []
for ARGV in sys.argv[2:]:
ARGV1.append(sys.argv[2:])
print (ARGV1)
My Out
[['2', '3', '4'], ['2', '3', '4'], ['2', '3', '4']]
My Expected out
['2', '3', '4']
Disclaimer Please dont say print (ARGV1[0])

Just do
ARGV1 = sys.argv[2:]
Since you have 3 values in input its adding same list 3 times.

Related

how to save parsed data into two different lists

I have this code:
lokk = []
nums = 7
for _ in range(nums):
inner = driver.find_element_by_xpath(
"/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
"innerHTML")
lokk.append(inner)
time.sleep()
print(lokk)
which provides me with this data:
['1', '2', '3', '4', '5', '6', '7']
what I want to do is to save that data into two different lists, the first list containing the first six values e.g. ['1', '2', '3', '4', '5', '6'] and the second lists containing the the whole seven values e.g. ['1', '2', '3', '4', '5', '6', '7'] how ever i want it be so that the next sample of data collected contains the last value of the second list as the first value of the list pair of lists like so ['7', '8', '9', '10', '11', '12', '13']
i thought this was the code that would somewhat enable be to get the data in the different lists like i wanted but then soon realized that by the time it goes to fetch the second set of data for the second list of seven values, the data would have changed and that's not what I want
lok = []
num = 6
for _ in range(num):
inner = driver.find_element_by_xpath(
"/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
"innerHTML")
lok.append(inner)
time.sleep(10)
print(lok)
lokk = []
nums = 7
for _ in range(nums):
inner = driver.find_element_by_xpath(
"/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
"innerHTML")
lokk.append(inner)
time.sleep()
print(lokk)
Another flaw i saw in this is that when it was time to run the process again later, the seventh data would not be the first data for the new set of lists.
Meaning that instead of:
listA = ['1', '2', '3', '4', '5', '6']
listB = ['1', '2', '3', '4', '5', '6', '7']
ListC = ['7', '8', '9', '10', '11', '12']
listD = ['7', '8', '9', '10', '11', '12', '13']
it would be:
listA = ['1', '2', '3', '4', '5', '6']
listB = ['1', '2', '3', '4', '5', '6', '7']
ListC = ['8', '9', '10', '11', '12', '13']
listD = ['8', '9', '10', '11', '12', '13', '14']`
I really hope I was clear enough in what I am looking for assistance in, if not please let me know.
Please help :(
You can achieve this in many ways, try this:
For ListC:
lok_c = []
num_c = 13
for _ in range(num_c):
inner = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").
get_attribute("innerHTML")
if num_c > 7:
lok_c.append(inner)
time.sleep()
print(lok_c)
For ListD:
lok_d = []
num_d = 14
for _ in range(num_d):
inner = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").
get_attribute("innerHTML")
if num_d > 7:
lok_c.append(inner)
time.sleep()
print(lok_d)
Correct the indentation and variable name, if I missed anything.

Can't modify list of lists to have a customized length

I've written a script to make the length of all the lists at least 3 no matter what are their individual length at this moment.
Currently the list of lists I have:
item_list = [['1','2'],['3','4','5'],['2','4','5'],['1']]
I've tried with:
item_list = [['1','2'],['3','4','5'],['2','4','5'],['1']]
for item in item_list:
if len(item)<3:
item.extend([""])
elif len(item)<2:
item.extend([""]*2)
print(item_list)
Output I'm getting:
[['1', '2', ''], ['3', '4', '5'], ['2', '4', '5'], ['1', '']]
Desired output:
[['1', '2', ''], ['3', '4', '5'], ['2', '4', '5'], ['1', '','']]
How can I make the length of all the lists at least 3 irrespective of their current length?
for item in item_list:
item += ['']*(3-len(item))
You have written the order in reverse
item_list = [['1','2'],['3','4','5'],['2','4','5'],['1']]
for item in item_list:
if len(item)<2:
item.extend([""]*2)
elif len(item)<3:
item.extend([""])
print(item_list)

Appending temporary lists from loop ends up with multiple copies of the same list

I am trying to practice to make a method.
s = '132'
lis = list(s)
result = []
lisc = lis[:]
for item in lis:
for i in range(1,len(lis)):
lisc.remove(item)
lisc.insert(i,item)
print("lis : ", lisc)
result.append(lisc)
print(result)
The result is :
lis : ['3', '1', '2']
lis : ['3', '2', '1']
lis : ['2', '3', '1']
lis : ['2', '1', '3']
lis : ['1', '2', '3']
lis : ['1', '3', '2']
[['1', '3', '2'], ['1', '3', '2'], ['1', '3', '2'], ['1', '3', '2'], ['1', '3', '2'], ['1', '3', '2']]
I don't get why the result is appending the original lisc instead of the modified lisc in the loop.
I tried result.append(lisc[:]) and it works.
for item in lis:
for i in range(1,len(lis)):
lisc.remove(item)
lisc.insert(i,item)
print("lis : ", lisc)
result.append(lisc[:])
print(result)
Can anyone answer my question?
Thank you in advance.
In the first version of your code, you append the same list that it's being updated at every iteration. In the end, all the lisc you appended will be a reference to the same list
In the second version, you append a new copy of the modified list. Those copies are all different object

python slice set in list

i would like to slice a set within a list, but every time i do so, i get an empty list in return.
what i try to accomplish (maybe there is an easier way):
i got a list of sets
each set has 5 items
i would like to compare a new set against the list (if the set already exists in the list)
the first and the last item in the set is irrelevant for the comparison, so only the positions 2-4 are valid for the search of already existing sets
here is my code:
result_set = ['1', '2', '3', '4', '5']
result_matrix = []
result_matrix.append(result_set)
slicing the set is no problem:
print result_set[1:4]
['2', '3', '4']
print result_matrix[:][1:4]
[]
i would expect:
[['2', '3', '4']]
I think this is what you want to do:
>>> target_set = ['2', '3', '4']
>>> any([l for l in result_matrix if target_set == l[1:-1]])
True
>>> target_set = ['1', '2', '3']
>>> any([l for l in result_matrix if target_set == l[1:-1]])
False
Generalising and making that a function:
def is_set_in_matrix(target_set, matrix):
return any(True for l in matrix if list(target_set) == l[1:-1])
>>> result_matrix = [['1', '2', '3', '4', '5']]
>>> is_set_in_matrix(['1', '2', '3'], result_matrix)
False
>>> is_set_in_matrix(['2', '3', '4'], result_matrix)
True
# a quirk - it also works with strings...`
>>> s = '234'
>>> is_set_in_matrix(s, result_matrix)
True
Note that I have used l[1:-1] to ignore the first and last elements of the "set" in the comparison. This is more flexible should you ever need sets of different lengths.
>>> result_set = ['1', '2', '3', '4', '5']
>>> print result_set[1:4]
['2', '3', '4']
>>> result_matrix.append(result_set[1:4])
>>> result_matrix
[['2', '3', '4']]
Using result_matrix[:] returns the whole matrix as it is. You need to treat the result you want as a part of the array.
>>> result_matrix.append(result_set)
>>> result_matrix[:]
[['1', '2', '3', '4']]
>>> result_matrix[:][0]
['1', '2', '3', '4']
>>> result_matrix[0][1:4]
['2', '3', '4']
Also, as pointed out by falsetru:
>>> result_matrix.extend(result_set)
>>> result_matrix
['1', '2', '3', '4']
>>> result_matrix[1:4]
['2', '3', '4']

List circulation in Python for Project Euler 37

So, I was doing Project Euler 37
I need to circulate a list
input: 2345 # converted to list inside function
expected output: [[3,4,5,2],[4,5,2,3],[5,2,3,4],[2,3,4,5]]
Here is my function for that
def circulate(n): #2345
lst=list(str(n)) #[2,3,4,5]
res=[]
for i in range(len(lst)):
temp=lst.pop(0)
lst.append(temp)
print lst #print expected list
res.append(lst) #but doesn't append as expected
return res
print circulate(2345)
My output is:
['3', '4', '5', '2']
['4', '5', '2', '3']
['5', '2', '3', '4']
['2', '3', '4', '5']
[['2', '3', '4', '5'], ['2', '3', '4', '5'], ['2', '3', '4', '5'], ['2', '3', '4', '5']]
The function prints lst correct every time, but doesn't append as expected.
What I am doing wrong?
You need to append copies of your list to res:
res.append(lst[:])
You were appending a reference to the list being altered instead; all references reflect the changes made to the one object.
You may want to look at collections.deque() instead; this double-ended list object supports efficient rotation with a .rotate() method:
from collections import deque
def circulate(n):
lst = deque(str(n))
res = []
for i in range(len(lst)):
lst.rotate(1)
res.append(list(lst))
return res

Categories