How do I split a tuple in python - python-2.7

How do I split a tuple?
I want to split the tuple, but an error occurs.
error : File "", line 1, in
How do I turn this...
a = ( '1', '2abc3', '4', '5')
into this?
a = ( '1', '2a', 'bc3', '4', '5')

Tuple is immutable,
If you want to do like you mentioned you can add all elements of tuple to list and then you can do your operation and then again you can add all elements of list to tuple

It is not possible to change a tuple using codes like tuple.pop() or tuple.remove() that you can use in lists.So I recommend editing your code directly.

Related

Pandas create histogram with custom sort order

I am trying to create a histogram of education. However, my data is such that the edu variable is categorical: '10', '11', '12', 'GED', etc.. I want a histogram that goes in that order '10', '11', '12', 'GED', '13', etc.
My two approaches are:
1: Use pd.DataFrame.hist with a new variable that maps edu to a numeric edunum variable. However, I am then having trouble getting the original edu lables on the histogram
2: Use pd.Series(list(profile.['edu'])).value_counts().plot(kind="bar"). However, I am having trouble sorting the bar order correctly.
Any suggestions? Thanks!

Creating a new list from an old list

Okay, so I've been wracking my brain trying to think of how to go about this and this question is just a simplified version of what I'm really trying to do but it all boils down to this.
I have a list:
lst = ['0', '53', '2', '4', '44', '5', '8', '11']
and '0' is the first item. I want the output to be:
lst = ['0', '1', '2', '3', '4', '5', '6', '7']
so I want the '2' from the first list, to become '1' in the second list.
'4' in the first list to become '2' in the second list.
'5' in the first list to become '3' in the second list.
'8' in the first list to become '4' in the second list.
'11' in the first list to become '5' in the second list.
'44' in the first list to become '6' in the second list.
and '53' in the first list to become '7' in the second list.
but I can't just manually change each one, it has to be automated and change each one by itself, like by using a loop or something
help i've literally been trying to figure this out for hours now and it seems so simple but i can't figure it out
def function(x):
lstnew = []
count = 0
for e in x:
lst.append(count)
count +=1
return lstnew
function(lst)

Generate different permutations/combinations in a string

I am given a string (eg "12345678").
I want to generate different combinations using +,-,*,/.
Like :
'1+2+3+4+5+6+7+8'
'1+2*3-4+5+6-7+8'
'1-2+3+4*5+6-7*8'
'1-2-3-4+5*6+7+8'
'1+2+3+4+5+6*7*8'
'1-2+3-4+5-6+7-8'
Any idea how do i generate all different combinations like above?
this is one way to achieve this:
from itertools import product
numbers = "123456"
for operators in product('+-*/', repeat=len(numbers)-1):
ret = numbers[0]
for op, n in zip(operators, numbers[1:]):
ret += op+n
print(ret)
zip creates pairs of elements of two iterators. the rest is just string manipulation (and not in a very good way).
this is a little more compact (and pythonic?) with some more itertools magic:
from itertools import product, zip_longest, chain
numbers = "123456"
operators = '+-*/'
for ops in product(operators, repeat=len(numbers)-1):
print(''.join(chain(*zip_longest(numbers, ops, fillvalue=''))))
product is well documented. with zip_longest i create an iterator that will yield the pairs ('1', '+') , ('2', '*'), ... , ('6', '') (the last item is filled with the fillvalue; ops is one element shorter than numbers). the chain(*...) idiom is a simple way to flatten the tuples to get an iterator over the strings '1', '+', '2', '*', ..., '6', ''. then i simply join these strings.
if you don't like the chain(*...) part, you can replace it with chain.from_iterable(...) (this time without the * which may be a bit cleaner).

How to get a unique list of objects from AWS S3 bucket

the following code connecting to the AWS S3 bucket and returning the list of objects from S3 bucket. I’m trying to create a unique list out of original list, by selecting partial value of the object (i.e. batchID = str((s3_file.name).split("/"))[32:-13]). I have declared “batchID" as an array. When I use set() to return unique value it returns unique numbers within each value. example: ['1', '0', '3', '2', '5', '4', '9', '8’], ['1', '0', '3', '2', '5', '4', '7', '9', '8’] etc. So it is de-duping horizontally verses vertically in the list. I’m expecting the value to be unique. See below expected output. I also tried to use nested "for loops" and used "not in” to return the unique values but it didn’t work, it is still removing duplicates vertically and not horizontally. Can anyone please help. Thank you in advance.
def __init__(self, aws_access_key_id, aws_secret_access_key, aws_bucket_to_download, use_ssl):
self.run_id = []
self.batchID = []
self._aws_connection = S3Connection(aws_access_key_id, aws_secret_access_key, is_secure = use_ssl)
self._runId(aws_bucket_to_download)
def _runId(self,aws_bucket_to_download):
if not self._bucketExists(aws_bucket_to_download):
self._printBucketNotFoundMessage(aws_bucket_to_download)
else:
bucket = self._aws_connection.get_bucket(aws_bucket_to_download)
for s3_file in bucket.list(prefix='Download/test_queue1/'):
batchID = str((s3_file.name).split("/"))[32:-13]
#a = set(batchID)
#batchID = list(a)
print batchID
#newList = list(set(batchID))
#print newList`
Output:
144019080231459
144019080231459
144019800231759
144019800231759
Expected output:
144019080231459
144019800231759
I think you're asking how to remove duplicate batch IDs. Why don't you add each batch ID to a list as you retrieve it, ignoring it if it's already in the list, for example:
batchIDlist = []
for s3_file in bucket.list(prefix='Download/test_queue1/'):
batchID = str((s3_file.name).split("/"))[32:-13]
if batchID not in batchIDlist:
batchIDlist.append(batchID)
This will also keep items in the same order they were first found.

Zipping two lists won't make a tuple?

weekinfo=[]
for k in sats:
weekinfo.append(get_weekly(k, satdict))
yearend=get_weekly('end_year', satdict)
weektuples=zip(sats, weekinfo)
Sats is a list with 52 items. [ date, date, date, etc]
Weekinfo is a nested list (thus 52 lists in a list) with in each nested list with in each list 100 tuples.
I am trying to make a tuples (weektuples) in the last line via zip(sats, weekinfo). However, the output are not tuples but a lists.
Is there a solution to create tuples with Sats and Weekinfo?
Try:
x=52
sats = np.linspace(1,x,x)
print sats
weekinfo=[]
satdict= ['m', 't', 'w', 'u', 'f', 'sa', 'su']
for k in range(x):
weekinfo.append((sats[int(k)], satdict[int(k)%7]))
print weekinfo
Not sure that's the exact answer you're looking for, but something along those lines might work